hw8

cpp

School

Dartmouth College *

*We aren’t endorsed by this school

Course

189

Subject

Computer Science

Date

Nov 24, 2024

Type

cpp

Pages

4

Uploaded by ProfWildcatMaster683

Report
/*********************************************** Author: Shreya Mishra Date: Nov. 19th, 2023 Purpose: We have improve the code for a random number in the guessing integer game and the user has to figure out which number the game is guessing (guesses < 10). Sources of Help: Dr. Lin's, Dr. Lin's lecture videos, Dr.Lin's slideshows, Intro to how to code in C++ Time Spent: 5-6 hours ***********************************************/ #include<iostream> #include<cstdlib> #include<ctime> using namespace std; /* This is the outline of how to implement a number guessing game to guess the correct number: 1) Define the Player class with a virtual function named getGuess( ). The implementation of Player::getGuess() can simply return 0. 2) Define a class named HumanPlayer derived from Player. The implementation of HumanPlayer::getGuess() should prompt the user to enter a number and return the value entered from the keyboard. 3) Define a class named ComputerPlayer derived from Player. The implementation of ComputerPlayer::getGuess() should randomly select a number from 0 to 100. 4) Finally, construct the main function that pits two human players, a human against a computer, and two computers against each other using play(Player &player1, Player &player2) function. 5) Enhance the computer players strategy. The computer player in step1 does not play the number guessing game very well, since it makes only random guesses. 6) Modify the program so that the computer plays a more informed game. The specific strategy is up to you, but you must add function(s) to the Player and ComputerPlayer classes so that the play (Player &player1, Player &player2) function can send the results of a guess back to the computer player. 7) In other words, the computer must be told if its last guess was too high or too low, and it also must be told if its opponents last guess was too high or too low.
The computer can then use this information to revise its next guess. 8) Fix the play function since it does not generate a real random number and please fix that problem. */ class Player { public: virtual int getGuess() = 0; virtual void informResult(bool tooHigh, bool tooLow) = 0; }; class HumanPlayer : public Player { public: int getGuess() override { int guess; cout << "Please type your guess: "; cin >> guess; return guess; } void informResult(bool tooHigh, bool tooLow) override { // Human player doesn't use result information } }; class ComputerPlayer : public Player { public: int getGuess() override { lastGuess = (rand() % 100); //cout << "The computer guesses " << lastGuess << endl; return lastGuess; } void informResult(bool tooHigh, bool tooLow) override { // Adjust guess based on opponent's result if (tooHigh) { lastGuess -= rand() % 3 + 1; } else if (tooLow) { lastGuess += rand() % 3 + 1; } } private: int lastGuess =0 ;
}; bool checkForWin(int guess, int answer) { if (answer == guess){ cout << "You're right! You win!" << endl; return true; } else if (answer < guess) cout << "Your guess is too high." << endl; else cout << "Your guess is too low." << endl; return false; } // The play function takes as input two Player objects. void play(Player& player1, Player& player2) { int answer = rand() % 100; bool win = false; while (!win) { cout << "Player 1's turn to guess." << endl; int guess1 = player1.getGuess(); win = checkForWin(guess1, answer); if (win) return; cout << "Player 2's turn to guess." << endl; int guess2 = player2.getGuess(); win = checkForWin(guess2, answer); if (win) return; //Inform players about the results player1.informResult(guess2 > answer, guess2 < answer); player2.informResult(guess1 > answer, guess1 < answer); } } // Main function for the guessing game int main(int argc, char* argv[]) { HumanPlayer humanPlayer1, humanPlayer2; ComputerPlayer computerPlayer1, computerPlayer2; cout << "Human vs. Human:" << endl;
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
play(humanPlayer1, humanPlayer2); cout << "\nHuman vs. Computer:" << endl; play(humanPlayer1, computerPlayer1); cout << "\nComputer vs. Computer:" << endl; play(computerPlayer1, computerPlayer2); return 0; } /* Computing III -- COMP.2010 Honor Statement The practice of good ethical behavior is essential for maintaining good order in the classroom, providing an enriching learning experience for students, and as training as a practicing computing professional upon graduation. This practice is manifested in the Universitys Academic Integrity policy. Students are expected to strictly avoid academic dishonesty and adhere to the Academic Integrity policy as outlined in the course catalog. Violations will be dealt with as outlined therein. All programming assignments in this class are to be done by the student alone. No outside help is permitted except the instructor and approved tutors. I certify that the work submitted with this assignment is mine and was generated in a manner consistent with this document, the coursepo+ academic policy on the course website on Blackboard, and the UMass Lowell academic code. Date: Nov. 17th, 2023 Name: Shreya Mishra */ k9o9