Hangman:
c=0; % initializes incorrect guess tally hangcount = 1; %this is used for the sprite positions of the hangman %% scene initialization scene = simpleGameEngine('spritesheet.png',16,16,2); % 38 is the blank square window = [linspace(38,38,14);linspace(38,38,14);linspace(38,38,14);linspace(38,38,14);linspace(38,38,14);linspace(38,38,14);linspace(38,38,14);linspace(38,38,14)]; window(1,7) = 30; % top left stand window(1,8) = 29; % top right stand window(2:5,8) = 32; % mid stand window(6,8) = 28; % bottom stand window(1,14) = 44 - c; % guess counter drawScene(scene, window) %% input check x = 0; while x == 0 % prompts player 1 to enter a word var1=input('Player 1 Enter a Word: ','s'); clc var2=lower(var1); % creates an array out of the word, makes it lower case for i = 1:length(var2) % ensures the word only contains letters if var2(i) >=97 && var2(i)<=122 x=1; else x = 0; end end if length(var2) > 12 %length check x = 0; end if x == 0 fprintf('Invlaid Response, Try Again\n') end end varL=linspace(0,0,length(var2)); % creates empty array to be filled with player 2's correct guesses %% draws the number of spaces based on word length for i = 1:length(var2) window(8,i+1) = 27; end drawScene(scene, window) y = 0; while y == 0 % prompts player 2 to enter a guess gar1=input('Player 2 Enter a Letter: ','s'); clc gar2=lower(gar1); % makes guess lower case if gar2 >=97 & gar2<=122 & length(gar2)==1 % ensures the guess is a letter y=1; else fprintf('Invlaid Response, Try Again\n') end end while c<5 b=0; for i=1:length(var2) if gar2==var2(i) % checks guess for correctness and fills any corresponding blanks fprintf('Correct guess!\n') varL(i)=char(gar2); window(7,i+1) = varL(i)- 96; drawScene(scene, window) b=1; end end if b~=1 c=c+1; % adds to tally if guess is incorrect window(1,14) = 44 - c; % updates the counter visuals window(1+hangcount,7) = 30 + c; if c ~= 2 && c ~= 3 && c ~= 5 hangcount = hangcount + 1; end drawScene(scene, window) fprintf('Incorrect guess.\n') end if char(varL)==var2 % if all blanks are filled, display P2 win message fprintf('Player 2 Won!!!\n') break end y = 0; while y == 0 % prompts player 2 to enter a guess gar1=input('Player 2 Enter a Letter: ','s'); clc gar2=lower(gar1); % makes guess lower case if gar2 >=97 & gar2<=122 & length(gar2)==1 % ensures the guess is a letter y=1; else fprintf('Invlaid Response, Try Again\n') end end end if c==5 % if incorrect guess tally reaches 5, display P1 win message window(1,14) = 38; % updates the counter visuals window(2,7) = 37; window(4,7) = 36; drawScene(scene, window) fprintf('Player 1 Won!!!\n') end
Over/Under Seven:
Script File-
% Start by reseting the dice. This isn't required to make the game work,
% but it's needed for the graphics.
dice1 = 1;
dice2 = 1;
%initializes graphics
scene = simpleGameEngine('dicesheet.png', 16, 16, 4, [0, 255, 0]);
spriteArray = [14,9,9,9,15;10,dice1,7,dice2,11;12,8,8,8,13];
drawScene(scene, spriteArray)
valid = 0;
% This while loop will continue to ask for an input until the user inputs
% a valid response
while (valid == 0)
guess = input('Will the total be over, under, or equal to 7?\n', 's');
% Allows any combination of capital letters
guess = lower(guess);
% makes sure the input is valid
if (guess == "over" || guess == "under" || guess == "equal")
valid = 1;
else
fprintf('Invalid response.\n')
end
end
%% Dice Roll
dice1 = randi(6,1);
dice2 = randi(6,1);
total = dice1 + dice2;
%% Dice animation
framecounter = 0;
while (framecounter < 20)
pause(0.1)
spriteArray(2,2) = randi(6,1);
spriteArray(2,4) = randi(6,1);
drawScene(scene, spriteArray)
framecounter = framecounter + 1;
end
spriteArray(2,2) = dice1;
while (framecounter < 40)
pause(0.1)
spriteArray(2,4) = randi(6,1);
drawScene(scene, spriteArray)
framecounter = framecounter + 1;
end
spriteArray(2,4) = dice2;
drawScene(scene, spriteArray)
%% Main Algorithm
win = 'The total is %.i. You win!\n';
lose = 'The total is %.i. You lose.\n';
% for each condition, the variable needs to be checked as a string because
%the length of some character arrays are different.
if (total > 7)
if (guess == "over")
fprintf(win, total)
else
fprintf(lose, total)
end
elseif (total < 7)
if (guess == "under")
fprintf(win, total)
else
fprintf(lose, total)
end
elseif (guess == "equal")
fprintf(win, total)
else
fprintf(lose, total)
end