Posts

Sam’s Code

clc
clear
close all
warning(‘off’,’all’);

all_cat = [0 0 0 0 0 0 0 0 0 0 0 0 0];
scoring_pic = imread(‘yatzee_scoring.PNG’);
scoring_pic = scoring_pic(:,:,1);
row_step = length(scoring_pic)/13;
col_step = 583;
score = 0;
my_scene = simpleGameEngine(‘retro_simple_dice.png’, 16, 16, 16, [100,100,100]);

while sum(all_cat) < length(all_cat)

rolls = randi(6,1,5);
drawScene(my_scene,rolls);
title(‘Here are your original dice!’)
pause(3);
k = 1;

while k > 0 && k < 6
subplot(1,2,1)
drawScene(my_scene,rolls);
title(‘Turn 1: Select 1-5 to discard dice – Any other key will end turn.’)
subplot(1,2,2)
imshow(scoring_pic)
title(‘These are the categories you still have left’)
k = str2double(getKeyboardInput(my_scene));
if k == 1 || k == 2 || k == 3 || k == 4 || k == 5
rolls(k) = 10;
else
end
end

for i = 1:5
if rolls(i) == 10
rolls(i) = randi(6);
end
end

subplot(1,2,1)
drawScene(my_scene,rolls);
title(‘Turn 2: Select 1-5 to discard dice – Any other key will end turn.’)
subplot(1,2,2)
imshow(scoring_pic)
title(‘These are the categories you still have left’)

k = 1;

while k > 0 && k < 6
subplot(1,2,1)
drawScene(my_scene,rolls);
title(‘Turn 2: Select 1-5 to discard dice – Any other key will end turn.’)
subplot(1,2,2)
imshow(scoring_pic)
title(‘These are the categories you still have left’)
k = str2double(getKeyboardInput(my_scene));
if k == 1 || k == 2 || k == 3 || k == 4 || k == 5
rolls(k) = 10;
else
end
end

for i = 1:5
if rolls(i) == 10
rolls(i) = randi(6);
end
end

subplot(1,2,1)
drawScene(my_scene,rolls);
title(‘Turn 3: Select 1-5 to discard dice – Any other key will end turn.’)
subplot(1,2,2)
imshow(scoring_pic)
title(‘These are the categories you still have left’)

k = 1;

while k > 0 && k < 6
subplot(1,2,1)
drawScene(my_scene,rolls);
title(‘Turn 3: Select 1-5 to discard dice – Any other key will end turn.’)
subplot(1,2,2)
imshow(scoring_pic)
title(‘These are the categories you still have left’)
k = str2double(getKeyboardInput(my_scene));
if k == 1 || k == 2 || k == 3 || k == 4 || k == 5
rolls(k) = 10;
else
end
end

for i = 1:5
if rolls(i) == 10;
rolls(i) = randi(6);
end
end

subplot(1,2,1)
drawScene(my_scene,rolls);
title(‘Final Dice’)
subplot(1,2,2)
imshow(scoring_pic)
title(‘Which category would you like to pick?’)
cat = getKeyboardInput(my_scene);

A = aces(rolls);
B = twos(rolls);
C = threes(rolls);
D = fours(rolls);
E = fives(rolls);
F = sixes(rolls);
G = three_of_a_kind(rolls);
H = four_of_a_kind(rolls);
I = fullhouse(rolls);
J = smallstraight(rolls);
K = largestraight(rolls);
L = yahtzee(rolls);
M = chance(rolls);

if cat == ‘a’ & all_cat(1) ~= 1
if A == 0
fprintf(‘You selected a category that does not apply to you roll, your score is still %i!\n’,score)
else
score = score + aces(rolls);
fprintf(‘You just added %i points to your score! Your new score is %i.\n’,aces(rolls),score)
end
all_cat(1) = 1;
scoring_pic(1:row_step,1:col_step) = 0;
elseif cat == ‘a’ & all_cat(1) == 1
fprintf(‘You already chose this category, please pick again!\n’)
cat = getKeyboardInput(my_scene);
end

if cat == ‘b’ & all_cat(2) ~= 1
if B == 0
fprintf(‘You selected a category that does not apply to you roll, your score is still %i!\n’,score)
else
score = score + twos(rolls);
fprintf(‘You just added %i points to your score! Your new score is %i.\n’,twos(rolls),score)
end
all_cat(2) = 1;
scoring_pic(row_step:2*row_step,1:col_step) = 0;
elseif cat == ‘b’ & all_cat(2) == 1
fprintf(‘You already chose this category, please pick again!\n’)
cat = getKeyboardInput(my_scene);
end

if cat == ‘c’ & all_cat(3) ~= 1
if C == 0
fprintf(‘You selected a category that does not apply to you roll, your score is still %i!\n’,score)
else
score = score + threes(rolls);
fprintf(‘You just added %i points to your score! Your new score is %i.\n’,threes(rolls),score)
end
all_cat(3) = 1;
scoring_pic(2*row_step:3*row_step,1:col_step) = 0;
elseif cat == ‘c’ & all_cat(3) == 1
fprintf(‘You already chose this category, please pick again!\n’)
cat = getKeyboardInput(my_scene);
end

if cat == ‘d’ & all_cat(4) ~= 1
if D == 0
fprintf(‘You selected a category that does not apply to you roll, your score is still %i!\n’,score)
else
score = score + fours(rolls);
fprintf(‘You just added %i points to your score! Your new score is %i.\n’,fours(rolls),score)
end
all_cat(4) = 1;
scoring_pic(3*row_step:4*row_step,1:col_step) = 0;
elseif cat == ‘d’ & all_cat(4) == 1
fprintf(‘You already chose this category, please pick again!\n’)
cat = getKeyboardInput(my_scene);
end

if cat == ‘e’ & all_cat(5) ~= 1
if E == 0
fprintf(‘You selected a category that does not apply to you roll, your score is still %i!\n’,score)
else
score = score + fives(rolls);
fprintf(‘You just added %i points to your score! Your new score is %i.\n’,fives(rolls),score)
end
all_cat(5) = 1;
scoring_pic(4*row_step:5*row_step,1:col_step) = 0;
elseif cat == ‘e’ & all_cat(5) == 1
fprintf(‘You already chose this category, please pick again!\n’)
cat = getKeyboardInput(my_scene);
end

if cat == ‘f’ & all_cat(6) ~= 1
if F == 0
fprintf(‘You selected a category that does not apply to you roll, your score is still %i!\n’,score)
else
score = score + sixes(rolls);
fprintf(‘You just added %i points to your score! Your new score is %i.\n’,sixes(rolls),score)
end
all_cat(6) = 1;
scoring_pic(5*row_step:6*row_step,1:col_step) = 0;
elseif cat == ‘f’ & all_cat(6) == 1
fprintf(‘You already chose this category, please pick again!\n’)
cat = getKeyboardInput(my_scene);
end

if cat == ‘g’ & all_cat(7) ~= 1
if G == 0
fprintf(‘You selected a category that does not apply to you roll, your score is still %i!\n’,score)
else
score = score + three_of_a_kind(rolls);
fprintf(‘You just added %i points to your score! Your new score is %i.\n’,three_of_a_kind(rolls),score)
end
all_cat(7) = 1;
scoring_pic(6*row_step:7*row_step,1:col_step) = 0;
elseif cat == ‘g’ & all_cat(7) == 1
fprintf(‘You already chose this category, please pick again!\n’)
cat = getKeyboardInput(my_scene);
end

if cat == ‘h’ & all_cat(8) ~= 1
if H == 0
fprintf(‘You selected a category that does not apply to you roll, your score is still %i!\n’,score)
else
score = score + four_of_a_kind(rolls);
fprintf(‘You just added %i points to your score! Your new score is %i.\n’,four_of_a_kind(rolls),score)
end
all_cat(8) = 1;
scoring_pic(7*row_step:8*row_step,1:col_step) = 0;
elseif cat == ‘h’ & all_cat(8) == 1
fprintf(‘You already chose this category, please pick again!\n’)
cat = getKeyboardInput(my_scene);
end

if cat == ‘i’ & all_cat(9) ~= 1
if I == 0
fprintf(‘You selected a category that does not apply to you roll, your score is still %i!\n’,score)
else
score = score + fullhouse(rolls);
fprintf(‘You just added %i points to your score! Your new score is %i.\n’,fullhouse(rolls),score)
end
all_cat(9) = 1;
scoring_pic(8*row_step:9*row_step,1:col_step) = 0;
elseif cat == ‘i’ & all_cat(9) == 1
fprintf(‘You already chose this category, please pick again!\n’)
cat = getKeyboardInput(my_scene);
end

if cat == ‘j’ & all_cat(10) ~= 1
if J == 0
fprintf(‘You selected a category that does not apply to you roll, your score is still %i!\n’,score)
else
score = score + smallstraight(rolls);
fprintf(‘You just added %i points to your score! Your new score is %i.\n’,smallstraight(rolls),score)
end
all_cat(10) = 1;
scoring_pic(9*row_step:10*row_step,1:col_step) = 0;
elseif cat == ‘j’ & all_cat(10) == 1
fprintf(‘You already chose this category, please pick again!\n’)
cat = getKeyboardInput(my_scene);
end

if cat == ‘k’ & all_cat(11) ~= 1
if K == 0
fprintf(‘You selected a category that does not apply to you roll, your score is still %i!\n’,score)
else
score = score + largestraight(rolls);
fprintf(‘You just added %i points to your score! Your new score is %i.\n’,largestraight(rolls),score)
end
all_cat(11) = 1;
scoring_pic(10*row_step:11*row_step,1:col_step) = 0;
elseif cat == ‘k’ & all_cat(11) == 1
fprintf(‘You already chose this category, please pick again!\n’)
cat = getKeyboardInput(my_scene);
end

if cat == ‘l’ & all_cat(12) ~= 1
if L == 0
fprintf(‘You selected a category that does not apply to you roll, your score is still %i!\n’,score)
else
score = score + yahtzee(rolls);
fprintf(‘You just added %i points to your score! Your new score is %i.\n’,yahtzee(rolls),score)
end
all_cat(12) = 1;
scoring_pic(11*row_step:12*row_step,1:col_step) = 0;
elseif cat == ‘l’ & all_cat(12) == 1
fprintf(‘You already chose this category, please pick again!\n’)
cat = getKeyboardInput(my_scene);
end

if cat == ‘m’ & all_cat(13) ~= 1
if M == 0
fprintf(‘You selected a category that does not apply to you roll, your score is still %i!\n’,score)
else
score = score + chance(rolls);
fprintf(‘You just added %i points to your score! Your new score is %i.\n’,chance(rolls),score)
end
all_cat(13) = 1;
scoring_pic(12*row_step:13*row_step,1:col_step) = 0;
elseif cat == ‘m’ & all_cat(13) == 1
fprintf(‘You already chose this category, please pick again!\n’)
cat = getKeyboardInput(my_scene);
end

end

close all
fprintf(‘Your final score is %i, thanks for playing!\n’,score)