-
Notifications
You must be signed in to change notification settings - Fork 0
/
J_checkGuessPt2.m
96 lines (69 loc) · 3.09 KB
/
J_checkGuessPt2.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
% AIM: remove repeated letters from vectors so that win message not displayed too early
% LINES 41-56
clear all; % clear workspace
clc; % clear command window
% get random word
dictionary = "hangmanDictEasy.txt";
chosenWord = generateWord(dictionary);
disp(chosenWord); % for checking
% hide letters of word
hiddenWord = hideWord(chosenWord);
disp(hiddenWord);
% initialise number of lives and guesses
numLives = 10;
fprintf("Lives left: %d \n ", numLives);
numGuesses = 0;
% empty vectors to store correct and incorrect letters
correctLetters = [];
incorrectLetters = [];
% check following statements while the number of lives has not reached 0
while (numLives>0)
guess = input("Guess a letter: ", 's'); % read in string
numGuesses = numGuesses + 1; % increment number of guesses
charIndex = strfind(chosenWord, guess); % index of the letter guessed
incorrectIndex = strfind(incorrectLetters, guess); % index of the incorrect letter guessed
correctIndex = strfind(correctLetters, guess); % index of the correct letter guessed
% if incorrect letter already guessed, inform user, make lives the
% same, and don't show in vector
if(guess==incorrectLetters(incorrectIndex))
fprintf("You've already guessed the letter %s. Keep guessing. \n", guess);
numLives = numLives + 1;
numGuesses = numGuesses - 1;
incorrectLetters(incorrectIndex)=[];
end
% if correct letter already guessed, remove letter from correctLetters
% vector
if(guess==correctLetters(correctIndex))
fprintf("You've already guessed the letter %s. Keep guessing. \n", guess);
numGuesses = numGuesses - 1;
correctLetters(correctIndex)=[];
end
% if letter correctly guessed, display letter
if(guess == chosenWord(charIndex))
hiddenWord(charIndex) = guess;
disp(hiddenWord);
fprintf("Lives left: %d \n ", numLives);
correctLetters = [correctLetters chosenWord(charIndex)]; % store in vector for checking
% else, continue hiding letters and decrease lives
else
disp(hiddenWord);
numLives = numLives-1;
fprintf("Lives left: %d \n ", numLives);
incorrectLetters = [incorrectLetters guess]; % add incorrect letter to incorrectLetters vector
end
% display correctly and incorrectly guessed letters
fprintf("Correct letters: %s \n", correctLetters);
fprintf("Incorrect letters: %s \n", incorrectLetters);
% display number of guesses
fprintf("Guesses: %d \n ", numGuesses);
% if all letters guessed without losing all lives, display
% congratulations and end the loop
if (length(correctLetters)==length(chosenWord))
disp("Congratulations, you guessed the word!");
break
end
end
% if no lives left and all letters have not been guessed, user loses
if numLives==0
fprintf("You lost. The word was %s. \n", chosenWord);
end