2025-02-20 13:54:56 +01:00

70 lines
2.0 KiB
C++

#include <iostream>
#include <string>
#include "hangman.h"
using std::string;
int main() {
// Word the player needs to guess (randomly selected)
string word = chooseWord();
//string word = "success";
// Initialise the "uncovered" word that is shown to the player: the uncovered
// word is obtained by replacing each letter from the original word (variable
// word) with an underscore (i.e. _).
string workingCopy = createWorkingCopy(word);
// This variable indicates whether or not the game is over
bool done = false;
int wrongGuesses = 0; // Number of wrong guesses
int maxWrongGuesses = 6; // Maximum number of wrong guesses (don't change)
// Draw the empty gallow
showHangman(0);
// Game loop (each iteration is a round of the game)
while (!done) {
printGameState(maxWrongGuesses, wrongGuesses);
printWorkingCopy(workingCopy);
/** Part 1: input next guess **********************************************/
char guess = '\0';
// TODO: replace the following line with your implementation
//PART1_readCharacter(guess);
std::cout << "Your guess: ";
std::cin >> guess;
/** Part 2: update working copy *******************************************/
bool found = false;
// TODO: replace the following line with your implementation
//PART2_updateWorkingCopy(word, guess, workingCopy, found);
for (int i = 0; i != word.length(); i++) {
if (guess == word.at(i)) {
found = true;
workingCopy.at(i) = guess;
}
}
/** Part 3: update game state *********************************************/
// TODO: replace the following line with your implementation
//PART3_updateGameState(word, workingCopy, found, maxWrongGuesses, done, wrongGuesses);
if (workingCopy == word) {
done = true;
printYouWon(word);
} else if (found == false) {
wrongGuesses += 1;
}
if (wrongGuesses == maxWrongGuesses) {
done = true;
printYouLost(word);
}
}
return 0;
}