Game in MATLAB?

8 views (last 30 days)
Nick Haufler
Nick Haufler on 25 Jan 2016
Commented: Walter Roberson on 27 Jan 2016
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!
Nick Haufler
Nick Haufler on 25 Jan 2016
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
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 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?
Walter Roberson
Walter Roberson on 27 Jan 2016
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!