필터 지우기
필터 지우기

Game in MATLAB?

조회 수: 4 (최근 30일)
Nick Haufler
Nick Haufler 2016년 1월 25일
댓글: Walter Roberson 2016년 1월 27일
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
Stephen23
Stephen23 2016년 1월 25일
Don't use a cell array for points, use a simple numeric array!
Nick Haufler
Nick Haufler 2016년 1월 25일
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))

댓글을 달려면 로그인하십시오.

채택된 답변

Walter Roberson
Walter Roberson 2016년 1월 25일
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
Nick Haufler
Nick Haufler 2016년 1월 27일
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 2016년 1월 27일
If you do not do it graphically, consider using menu()

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Board games에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by