// // File: guesgame.cpp. Method definitions file for // GuessingGame class // #include "guesgame.h" // // Constructor to seed the random number generator // Pre: none // Post: The random number generator has been seeded. // GuessingGame::GuessingGame() { SeedRand(); } // // Function to allow the user to play the game // Pre: Constant positive integer HIGH has a value. // Post: Instructions have been displayed. // The target has been guessed by the user. // void GuessingGame::PlayGame() { int guess; // user's guess boolean_t GuessAgain = TRUE; // TRUE if user needs to guess cout << "Welcome to the GUESSING GAME!\n\n"; cout << "I'm thinking of a number between 0 and " << HIGH - 1 << ".\n"; cout << "Enter your guess.\n"; cout << "I'll tell you whether to guess higher or lower.\n\n"; target = random_int(HIGH); do { guess = ReadGuess(); if (guess < target) cout << "Guess higher\n\n"; else if (guess > target) cout << "Guess lower\n\n"; else { cout << "Congratulations! You guessed it!\n"; GuessAgain = FALSE; } } while (GuessAgain); // continue while GuessAgain is TRUE } // // Function to prompt for and read an integer to return to main // Pre: none // Post: The user's guess has been returned. // int GuessingGame::ReadGuess(void) { int guess; // user's guess cout << "Enter a guess: "; cin >> guess; return guess; }