0% found this document useful (0 votes)
126 views

Yahtzee Code

The document describes a Yahtzee scoring program that allows a user to roll 5 dice up to 3 times per turn, choose which dice to reroll, and then select a scoring category to apply the dice rolls to, with the program calculating the score and tracking it across 13 rounds of play. The program contains the logic to score standard Yahtzee categories such as ones, twos, three/four/five/six-of-a-kind, full house, small/large straights, chance, and Yahtzee bonuses.

Uploaded by

api-384662471
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
126 views

Yahtzee Code

The document describes a Yahtzee scoring program that allows a user to roll 5 dice up to 3 times per turn, choose which dice to reroll, and then select a scoring category to apply the dice rolls to, with the program calculating the score and tracking it across 13 rounds of play. The program contains the logic to score standard Yahtzee categories such as ones, twos, three/four/five/six-of-a-kind, full house, small/large straights, chance, and Yahtzee bonuses.

Uploaded by

api-384662471
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

clc

clear
scoresheet = zeros(13,13);
for rn=1:13
D = randi(6,5,1);
D
A = (input('Would you like to reroll? 1 for yes or 0 for no: '));
r=1;
while A==1 && r<3
r=r+1;
if A==0
exit while
end
x = str2num(input('Which dice would you like to keep?','s'));
Dx = D(x);
L = zeros(5,1);
S = randi(6,5,1);
for i = 1:length(x)
L(x(i)) = Dx(i);
S(x(i)) = 0;
end
D = S+L;
D
if r==3
break
else
A = input('Would you like to reroll? 1 for yes or 0 for no: ');
end
end
dice=sort(D);
point=zeros(13,1);
while 1
move=input('\n Choose your move.\n Moves are: \n
"ones","twos","threes","fours","fives","sixes","3ofakind","4ofakind","smallst
raight","largestraight", \n "fullhouse","yahtzee", and "chance": \n','s');
for i=1:6
n(i)=size(find(dice==i),1);
s=dice(2:end)-dice(1:end-1);
end
if
(move=="ones"||move=="twos"||move=="threes"||move=="fours"||move=="fives"||mo
ve=="sixes") && (~isempty(find(dice==1)) || ~isempty(find(dice==2)) ||
~isempty(find(dice==3)) || ~isempty(find(dice==4)) || ~isempty(find(dice==5))
|| ~isempty(find(dice==6)))
if move=="ones"
point(1)=1*n(1);
break
elseif move=="twos"
point(2)=2*n(2);
break
elseif move=="threes"
point(3)=3*n(3);
break
elseif move=="fours"
point(4)=4*n(4);
break
elseif move=="fives"
point(5)=5*n(5);
break
elseif move=="sixes"
point(6)=6*n(1);
break
end
elseif move=="3ofakind" && (max(n)==3)
point(7)=sum(dice);
break
elseif move=="4ofakind" && (max(n)==4)
point(8)=sum(dice);
break
elseif move=="fullhouse" && find(n==3) && find(n==2)
point(9)=25;
break
elseif move=="smallstraight" && (size(find(s==1),1)==3)
point(10)=30;
break
elseif move=="largestraight" && (size(find(s==1),1)==4)
point(11)=40;
break
elseif move=="yahtzee" && max(s)==0
point(12)=50;
break
elseif move=="chance"
point(13)=sum(dice);
break
else
fprintf('The required dice are not available. Try
again',move);
end

end
scoresheet(:,rn) = point;
end
for i=1:13
if scoresheet(12,i)==50 && scoresheet(12,i+1)==50
scoresheet(12,i+1)=100;
end
end
clc;
disp('');
disp('Scoresheet:')
disp(scoresheet)
disp('Total Score = ')
disp(sum(sum(scoresheet)))
disp('THE GAME IS OVER')

You might also like