Hangman Game

clc
clear

playAgain=’y’;
while playAgain==’y’
%%
%Ask each player for their name.
player1 = input(‘Welcome Player 1! You will choose a word to stump Player 2. What is your name? ‘,’s’);
player2 = input(‘Welcome Player 2! You will guess the word from Player 1. What is your name? ‘,’s’);
%Welcome player 1, enter a word
fprintf(‘\nWelcome %s!\n’,player1)
word=input(‘Please enter a word in all lowercase letters. Make sure Player 2 does not see. \n’,’s’);
%Clear screen so word is hidden.
clc

%Change word into numbers already associated in MATLAB.
numericWord=double(word);

%Make a vector that will keep track of when and where a letter
%goes. It will start with a vector of all ones, then with each correct
%letter, that spot will become that letter.
vectorOfLetters=ones(1,length(numericWord));

%Player 2
fprintf(‘\nHello %s!\n’,player2)
fprintf(‘\n\n The word you need to guess is %i letters long. \n’, length(word))
livesLeft=6;
%Show the empty hangman stand.
Pic=imread(‘1.png’);
imshow(Pic)
%Initialize the number of guesses correct. We will add 1 each time they
%guess a correct letter.
CorrectGuesses=0;

%While there are still blanks, we need to keep looping.
%Also, the counter for whether we keep going will be made, and reassessed at the
%end of each “round” in order to see if the word has been guessed yet.
keepGoing=1;

while keepGoing==1
% Count up each round as each letter is guessed
%Tell them how many lives are left at the beginning of each round.
fprintf(‘\n\n\n You currently have %i lives left.\n’,livesLeft)
%Ask for their guess.
letter=input(‘ Please enter a new lowercase letter you would like to guess. ‘, ‘s’);
%Coverts guess to a letter
numericLetter=double(letter);

%Check if letter is in word. This function will create a new vector that
%has a 1 every time the letter appears in the word, and 0 where it does
%not.
[LetterInWord]=ismember(numericWord,numericLetter);

%Next, we’re making a value that will only be 1 if the letter was present in the
%word, just so it would be easier to reference whether the letter is
%contained in the word or not.
wordYes=0;

%For each index in the vector, we are seeing if the letter is present
%or not. If it is at that space, the player is told so. If it is not, then
%we will see whether they already guessed that letter, in which case
%that will be output. If not, we take away a point.
fprintf(‘\n\n\n\n’)
for k=1:length(numericWord)
if LetterInWord(k)==1
fprintf(‘%s ‘,letter);
wordYes=1; %Means they got this round correct.
%We will store this letter in the vector of player guesses.
vectorOfLetters(k)=letter;

% Add a point each time they guess a correct letter, so that we
% can count up and see when their number of correct letters
% matches the letters in the word.
CorrectGuesses=CorrectGuesses+1;

elseif vectorOfLetters(k)~=1
%If their letter was already stored in the correct vector, it
%will show up.
fprintf(‘%s ‘,vectorOfLetters(k))

else
%If it was incorrect, they get a blank space.
fprintf(‘_ ‘);
end
end

%Tell them whether the letter was there or not.
if wordYes==1
fprintf(‘\n\n Great job! This letter was in the word. \n’)
else %If incorrect, lose a life.
livesLeft=livesLeft-1;
%Show a progressively hung man each time they lose a point.
if livesLeft==5
Pic=imread(‘2.png’);
imshow(Pic)
elseif livesLeft==4
Pic=imread(‘3.png’);
imshow(Pic)
elseif livesLeft==3
Pic=imread(‘4.png’);
imshow(Pic)
elseif livesLeft==2
Pic=imread(‘5.png’);
imshow(Pic)
elseif livesLeft==1
Pic=imread(‘6.png’);
imshow(Pic)
elseif livesLeft<=0
Pic=imread(‘7.png’);
imshow(Pic)
end

fprintf(“\n\n Since this letter was not in the word, you’ve lost a life. You have %i lives left.\n”,livesLeft)

end
%If they have filled in all of the letters, then they win. If not, they
%can still guess the word.
if CorrectGuesses==length(numericWord)%Since we’ve added a point to correct guesses each time a letter is correct, when this sum is equal to the length of the word, they have all of the letters.
keepGoing=0; %Ends the game
fprintf(‘\nOh %s, YOU GOT THE WORD! \n’,player2)
fprintf(‘\n You won hangman! Congratulations!\n’)
else
% Ask them if they want to guess.
Guess=input(‘\n\nWould you like to guess what the word is now? Enter “y” for yes or “n” for no. \n’,’s’);
%If they want to guess…
if Guess==’y’
guessedWord=input(‘What word would you like to guess? ‘,’s’);
guessedWord=contains(word,guessedWord);
%If the guess matches, they win and keepGoing goes to zero, ending the
%game.
if guessedWord==1
keepGoing=0;
fprintf(‘\nOh %s, YOU GOT THE WORD! \n’,player2)
fprintf(‘\n You won hangman! Congratulations!\n’)
else
%If it’s wrong, they just keep going.
keepGoing=1;
fprintf(‘\n Sorry, that is incorrect.\n’)
if livesLeft<=0
%If they run out of lives, keepGoing turns to zero, ending
%the game.
fprintf(‘\n Oh well… sorry %s!\n’,player2)
fprintf(‘\n You have run out of lives. You lose. The word was “%s.” \n’, word)
keepGoing=0;
else
end
end
elseif keepGoing == 1
% If they didn’t want to guess a word, then they keep going, unless
% they run out of lives, in which case they lose.
if livesLeft<=0
%If they run out of lives, keepGoing turns to zero, ending
%the game.
fprintf(‘\n Oh well… Sorry %s!\n’,player2)
fprintf(‘\n You have run out of lives. You lose. The word was “%s.” \n’,word)
keepGoing=0;
else
end
end
end
end

%Play again?
playAgain=input(‘\n Would you like to play again? Enter “y” for yes or “n” for no. \n’ ,’s’);
end