# yahtzee

does anyone know a basic layout or any really helpful hints how to make the yahtzee game in matlab. my professor has not mentioned anything on how to even do the basics, and the book can only help my group very little.

• It's not so difficult. You have to:
1. Generate 5 random numbers in the interval [1,6]: you can use the [u]rand[/u] matlab function.
[italic]Generate a uniform distribution of random numbers on a specified interval [a,b].
To do this, multiply the output of rand by (b-a),
2. Display the 5 numbers.
3. Ask the user what he/she want to do (you can do it with the [u]input[/u] function)
4. Write some simple rules to display the result and the score. i.e.:
if a==b==c==d==e==f
display('Yahtzee!');
score=sum(a,b,c,d,e);
end
5. Put the code in a [u]while[/u] loop to check the number of tries.

Let me know!
• so far we have what i have attached, it's not done yet either, anymore help would be appreciated
• Please, explain what kind of help do you want. There are problems in your code? There is something that you can't implement? What is missing in the code?
• we could use some help with about how to do the scoring, we need some way to store the score for player and would like to know a good way to setup up the scoring also, along with where this should be placed in the code.

-how do we setup the code in the while loop?
• First of all you have to fix some code errors.
1. Have you tried to answer "2" to the question 'do you want to roll again?'? It gives an error because it doesn't find a b c d and e because you defined it into the first if statement. So, move the definitions out of the if statement and you will fix it.

2. Even if you can write a==b==c, you can't write a<b<c. It gives as result 1 even if the condition is not satisfied. You have to write this kind of statements in this way: a<b && b<c ... and so on.

3. Substitute all the & with && and the "or" with ||.

4. You can write sum(a+b+c+d+e) as sum(a,b,c,d,e) or a+b+c+d+e otherwise is has no sense.

5. In this piece of code:
elseif find(n==3)
k=3;
disp('threes')
score=(sum(n==3)*k)
it is not necessary to define k because in the score you can use n.

6. In the same piece of code, find(n==3) doesn't return a true/false result. It returns the position of the elements of the vector that are =3. So you have to write something like:
elseif length(find(n==3))~=0
This returns 1 if there are 3s in the vector and 0 if there aren't 3s.

• we're just having trouble setting up this code, we still can't figure out how to get this to run in loop so the dice can only be rolled 3 times. we also need a system for asking what the player would like to use the final dice for:ones, twos, threes, yahtzee...etc.

• I attach an example on how I rewrite your code so that it can run.
There is also an example of a while loop so that you can loop also the whole code using another count.

I suggest you to try your code several times to understand if there are errors and where they are.
• the errors we ran into was not being able to score 3 and 4 of a kind,and full house. it won't score after the first or second roll. we also need some way to store the scores into a scorecard, which will only allow each possibility to be used once until the game is completed.

we edited the code so it will only allow a maximum of 3 rolls, and fixed some of the scoring that we could figure out.
• One more error in the while declaration. It has to be in this way:

while strcmp(R,'yes') && count<2

because if you as first answer say "no" it enter in the loop also if it has not to do so.

Then, to "save" the score: i think that the user has 3 (or more, or less) possibilities to launch the dice. So, put all the code in a for loop and index the score variable and finally display the total score. i.e:

for i=1:3
display(strcat('launch:',num2str(i)))
% code
if ..
score(i)= ...
else if ...
score(i)= ...
% code
end
total_score=sum(score);
display(strcat('total score:',num2str(total_score)))
end