Hangman load
You can use
system("cls")on windows orsystem("clear")on linux/mac to clear the console.
Create a program that implements a two-player Hangman game. The first player selects a word, then the console is cleared, and the second player attempts to guess the word. The game should allow the second player to guess letters until they either guess the word or run out of guesses. If the player types "close", the game should save the current game state (the word, remaining lives, and guessed letters) to a file named hangman_save.txt. Upon starting the game, the program should check if a saved game exists and ask the player if they want to load the saved game or start a new one.
Example round:
A saved game was found. Do you want to load it? (y/n): n
Player 1, enter a word for Hangman: secret
console is cleared when enter is hit.
Word: ______
Remaining Lives: XXXXXX
Guessed letters:
Enter a letter to guess (or type 'close' to save and exit): e
Word: _e__e_
Remaining Lives: XXXXXX
Guessed letters: e
Enter a letter to guess (or type 'close' to save and exit): s
...
Hint
- Words and guessed letters should be handled in the same case (upper/lower).
std::transform(word.begin(), word.end(), word.begin(), ::tolower);can be used to make a string (word)lowercase.
- Seperate loading the game and saving the game into functions.
- Use a
std::stringor astd::vector<char>to store guessed characters. - Have the primary game loop continue untill the player is out of guesses, or the word is guessed.
Answer
This program uses the saveGame and loadGame functions to handle saving and loading the game. The loadGame function is called at the start and returns true if the game should resume (i.e. the setup process should be skipped).
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <algorithm>
void saveGame(const std::string& word, int lives, const std::string& guessedLetters) {
std::ofstream outFile("hangman_save.txt");
outFile << word << '\n' << lives << '\n' << guessedLetters;
outFile.close();
std::cout << "Game saved!" << std::endl;
}
// returns true if a saved game was found and loaded
bool loadGame(std::string& word, int& lives, std::string& guessedLetters) {
std::ifstream inFile("hangman_save.txt");
if (inFile) {
char choice;
std::cout << "A saved game was found. Do you want to load it? (y/n): ";
std::cin >> choice;
std::cin.ignore(); // Skip newline character
if (choice == 'y' || choice == 'Y') {
std::getline(inFile, word);
inFile >> lives;
inFile.ignore(); // Skip newline character
std::getline(inFile, guessedLetters);
inFile.close();
std::cout << "Game loaded!" << std::endl;
return true;
}
}
return false;
}
void clearConsole() {
#ifdef _WIN32
system("cls");
#else
system("clear");
#endif
}
std::string getDisplayWord(const std::string& word, const std::string& guessedLetters) {
std::string displayWord(word.size(), '_');
// Loop the guessed letters and add the correct ones to the display word
for (char c : guessedLetters) {
for (size_t i = 0; i < word.size(); ++i) {
if (word[i] == c) {
displayWord[i] = c;
}
}
}
return displayWord;
}
int main() {
std::string word = "";
std::string guessedLetters = "";
int remainingLives = 6;
// If game isn't loaded, prompt for a new game
if (!loadGame(word, remainingLives, guessedLetters)) {
std::cout << "Player 1, enter a word for Hangman: ";
std::getline(std::cin, word);
std::transform(word.begin(), word.end(), word.begin(), ::tolower); // Make word lower case
clearConsole();
}
std::string displayWord;
while (displayWord != word && remainingLives > 0) {
displayWord = getDisplayWord(word, guessedLetters); // Update the display word
std::cout << "Word: " << displayWord << std::endl;
std::cout << "Remaining lives: " << std::string(remainingLives, 'X') << std::endl;
std::cout << "Guessed letters: " << guessedLetters << std::endl;
std::cout << "Enter a letter to guess (or type 'close' to save and exit): ";
std::string guess;
std::cin >> guess;
// Save and exit game if user types 'close'
if (guess == "close") {
saveGame(word, remainingLives, guessedLetters);
break;
}
// Make sure the input is valid
if (guess.size() != 1 || !isalpha(guess[0])) {
std::cout << "Invalid input. Please enter a single letter." << std::endl;
continue;
}
char guessedChar = tolower(guess[0]);
if (guessedLetters.find(guessedChar) != std::string::npos) {
std::cout << "You have already guessed that letter." << std::endl;
continue;
}
// Add the guessed letter to the list of guessed letters and increment the number of guesses
guessedLetters += guessedChar;
// check if the letter is in the word
bool correctGuess = word.find(guessedChar) != std::string::npos;
if (correctGuess) {
std::cout << "Correct guess!\n\n";
}
else {
std::cout << "Incorrect guess!\n\n";
remainingLives--;
}
}
if (displayWord == word) {
std::cout << "Congratulations! You've guessed the word: " << word << std::endl;
}
else if (remainingLives <= 0) {
std::cout << "Game over! The word was: " << word << std::endl;
}
else {
std::cout << "Game saved. Goodbye!" << std::endl;
}
return 0;
}