// ========================================================================== // Implementation of the Hangman game. // A. Fischer, June 4, 2000 file: game.cpp // Modified by M. Fischer, October 8, 2009 //=========================================================================== #include "game.hpp" // ========================================================================== Game::Game(char* wordfile) { char file_name[80]; //cerr << "Constructing Game. "; // For debugging. if (wordfile) strcpy(file_name, wordfile); else strcpy(file_name, "vocab.in"); ifstream source(file_name); if (!source) fatal("Could not open %s.\nEnding Hangman.\n", file_name); //cerr << "File " <print( cerr ); // For debugging. wins = rounds = 0; } //--------------------------------------------------------------------------- int Game::playRound() { const char* puzzle_word = Vocab->randword(); // pick puzzle word. Player p(Alphabet, puzzle_word); // construct game board return p.play(); // play the round } //--------------------------------------------------------------------------- void Game::play() { char response; // For query, "Play again?" const char* timeword; // For grammatical output: time, times. const char* tryword; // For grammatical output: try, tries. cout << "\n---------- Welcome to Hangman ----------\n" "You win if you can guess the hidden word.\n" "You lose if you guess " << HANG_MAX << " wrong letters.\n"; do { wins += playRound(); // Play one round of game. rounds++; timeword = (wins == 1) ? "time" : "times"; tryword = (rounds == 1) ? "try" : "tries"; cout << "\nYou won " << wins << " " << timeword << " out of " << rounds << " " << tryword << ".\n\nType p to play another round, q to quit: "; cin >> response; } while (tolower(response) == 'p'); }