I'm making Yahtzee in Matlab, and I'm focusing on getting the scoring set up. I get scores to come back in an array, but they don't seem to be correct. I've looked over my code several times, but just cant seem to figure out what went wrong. I'm desperate at this point, and need help. I attached my code along with this post. Any help is appreciated. Thanks! You guys are awesome.

2 Comments

Stephen23
Stephen23 on 25 Jan 2016
Don't use a cell array for points, use a simple numeric array!
When I changed it my scoring just went out of wack, and im getting 0 in every column for each score.
diceValues = randi(6,[1 5])
for k=2:3
diceIndexesToReRoll = input('Which dice would you like to reroll?')
diceValues(diceIndexesToReRoll) = randi(6,[1 numel(diceIndexesToReRoll)])
end
points=zeros(1,13);
% upper
for i = 1:6
counts(i) = sum(find(diceValues == i), 2);
points(i) = i*counts(i)
end
upperpoints=points(i)
% lower
% Three of a Kind
if any(find(counts >= 3))
points(7) = sum(diceValues)
end
% Four of a Kind
if any(find(counts >= 4))
points(8) = sum(diceValues)
end
% Full House
if any(find(counts == 3)) & any(find(counts == 2))
points(9) = 25
end
% Small Straight
dif = diceValues(2:end) - diceValues(1:end-1);
if (sum(find(dif==1), 2) >= 3)
if (dif(2) ~= 2) & (dif(3) ~= 2)
points(10) = 30
end
end
% Large Straight
if (sum(find(dif==1), 2) == 4)
points(11) = 40
end
% Yahtzee
if any(find(counts == 5))
points(12) = 50
end
% Chance
points(13) = sum(diceValues)
lowerpoints=sum(points(7)+points(8)+points(9)+points(10)+points(11)+points(12)+points(13))

Sign in to comment.

 Accepted Answer

Walter Roberson
Walter Roberson on 25 Jan 2016

0 votes

Hint: instead of using size(something==value) you can use sum(something==value)
Your code is counting some things multiple times. For example, a full house is counted but so is its value as 3 of a kind. Likewise, Chance should only apply if nothing else matched.
For 3 of a kind or 4 of a kind, you are using a score of sum(diceValues), which is not correct: you should be counting only the dice that match.
Your points array could be numeric instead of a cell array.

8 Comments

Nick Haufler
Nick Haufler on 25 Jan 2016
Thanks for the tips. Much Appreciated. Im just not getting the 3 and 4 of a kind. It's still counting all the dice, and not only the ones that match.
which_kind = find(counts >= 3);
if ~isempty(which_kind)
points{7} = 3 * which_kind;
end
which_kind would be the dice value that is repeated, and with there being 3 matching dice values, the sum of those dice would be 3 times the dice value.
However, I just checked the Hasbero instructions, and adding all of the dice is correct, and I was wrong to say you should only be adding the matching values.
The rules get confusing when trying to incorporate in with all the coding. One more question if you don't mind. For my upper score that involves all the one's, two's, three's, etc. I get some of the score values correct, but then some are completely wrong. I think the rule for them are you only add up whatever values your trying to get. If you want to fill up the two's spot, then you add up only the two's. This is the section i'm talking about.
%upper
for i = 1:6
counts(i) = sum(find(diceValues == i), 2);
points{i} = i*counts(i)
end
counts(i) = sum(diceValues == i);
Sorry if im bothering you, but I think I have everything worked out. I added in an if statement to calculate a bonus if you score over 63 points for the upper section, and also a yahtzee bonus. Forget about counting things multiple times, but could you look over the code, and just give some feedback. You're a big help, and I thank you for everything.
diceValues = randi(6,[1 5])
for k=2:3
diceIndexesToReRoll = input('Which dice would you like to reroll?')
diceValues(diceIndexesToReRoll) = randi(6,[1 numel(diceIndexesToReRoll)])
end
points=zeros(1,13);
% upper
for i = 1:6
counts(i) = sum(diceValues == i);
points(i) = i*counts(i)
end
upperpoints=sum(points(1)+points(2)+points(3)+points(4)+points(5)+points(6))
% Bonus
if upperpoints>=63
upperpoints=upperpoints+35
end
% lower
% Three of a Kind
if any(find(counts >= 3))
points(7) = sum(diceValues)
end
% Four of a Kind
if any(find(counts >= 4))
points(8) = sum(diceValues)
end
% Full House
if any(find(counts == 3)) & any(find(counts == 2))
points(9) = 25
end
% Small Straight
dif = diceValues(2:end) - diceValues(1:end-1);
if (sum(find(dif==1), 2) >= 3)
if (dif(2) ~= 2) & (dif(3) ~= 2)
points(10) = 30
end
end
% Large Straight
if (sum(find(dif==1), 2) == 4)
points(11) = 40
end
% Yahtzee
if any(find(counts == 5))
points(12) = 50
end
% Chance
points(13) = sum(diceValues)
lowerpoints=sum(points(7)+points(8)+points(9)+points(10)+points(11)+points(12)+points(13))
TotalScore= upperpoints+lowerpoints;
fprintf('Your Upper Score Is: %i \n',upperpoints)
fprintf('Your Lower Score Is: %i \n',lowerpoints)
fprintf('Your Total Score Is: %i \n',TotalScore)
Well, Yatzee is a strategic game involving multiple rolls. In any one turn, the player rolls (and selectively rerolls), and at the end of the possibility of rerolls, has to choose which of the eligible boxes to enter the score into. On the turns that follow, the player is stuck with what they entered before and has to use a different box -- and if what they rolled does not match any of the boxes they have left, then they must choose a box and write 0 in it (and then they cannot use that box afterwards.) The bonus on upper points comes after all 13 boxes are filled, not on any one roll.
So, after each roll your program should scan through the table to find the boxes that the user has not already filled and should tell the user how many points they would get if they were to assign the current set of dice to that box, at which point the user should be allowed to reroll if they have rerolls left, or to choose one of those open boxes to fill in (if they want, or if they have no rerolls left.)
Now, the display of how many points they would have if they were to choose the box should include evaluating any bonuses, so you should create a "hypothetical" that contains all the existing boxes together with that one choice and with all of the remaining empty boxes as 0's, and you should evaluate the score of that "hypothetical", and either display that score or the difference between that score and the current score. Then when they accept, write into the one box for the master copy (do not copy the complete hypothetical because you have filled the empty slots with temporary 0's for it.)
Nick Haufler
Nick Haufler on 27 Jan 2016
I see what you're saying. My goal was to first complete the reroll and scoring sections first. I was going to wait, and add everything else in after I completed those two. When I want the program to let the user decide which box they want to put the score into, would I use loops after each roll and then an input statement that tells what place to put the score in the array?
If you do not do it graphically, consider using menu()

Sign in to comment.

More Answers (0)

Categories

Find more on Board games in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!