Question: write the following guessing game program modifying my classes to hide information. The program has to be simplified, cohesive, and more secure. Please add meaningful

write the following guessing game program modifying my classes to hide information. The program has to be simplified, cohesive, and more secure. Please add meaningful comments and explanations to any changes to the program so I can learn. Thank you!

 

Game.h file:

#ifndef GAME_H
#define GAME_H

class Game {
public:
    Game(int numIntegers, int maxRange);
    void playGame();

private:
    int* selectedIntegers;
    int numIntegers;
    int maxRange;

    void generateRandomIntegers();
    int getUserGuess();
    int countCorrectGuesses(int* userGuesses);
};

#endif
 

Game. cpp file

#include
#include
#include
#include "Game.h"
Game::Game(int numIntegers, int maxRange) {
   this->numIntegers = numIntegers;
   this->maxRange = maxRange;
   selectedIntegers = new int[numIntegers];
   generateRandomIntegers();
}
void Game::generateRandomIntegers() {
   std::srand(std::time(0));
   for (int i = 0; i < numIntegers; ++i) {
       selectedIntegers[i] = rand() % maxRange+1;
   }
}
void Game::playGame() {
   int* userGuesses = new int[numIntegers];
   do {
       std::cout << "Enter your guesses for the " << numIntegers << " integers in the range from 1 to " << maxRange << " that have been selected: ";
       for (int i = 0; i < numIntegers; ++i) {
           std::cin >> userGuesses[i];
       }
       int correctGuesses = countCorrectGuesses(userGuesses);
       if (correctGuesses == numIntegers) {
           std::cout << "You are correct! Play again? ";
           break;
       }
       else {
           std::cout << correctGuesses << " of your guesses are correct. Guess again.\n\n";
       }
   } while (true);
   delete[] userGuesses;
}
int Game::getUserGuess() {
   int guess;
   std::cin >> guess;
   return guess;
}
int Game::countCorrectGuesses(int* userGuesses) {
   int correctGuesses = 0;
   for (int i = 0; i < numIntegers; ++i) {
       for (int j = 0; j < numIntegers; ++j) {
           if (selectedIntegers[i] == userGuesses[j]) {
               ++correctGuesses;
               break;
           }
       }
   }
   return correctGuesses;
}
 

Driver.cpp file:

#include
#include "Game.h"
int main() {
   int n, m;
   std::cout << "Enter the Number of Integers (n): ";
   std::cin >> n;
   std::cout << "Enter the Number of Each Integers from 1 to (m): ";
   std::cin >> m;
   Game game(n, m); // It creates an instance of the game
   do {
       game.playGame(); // Start the game
       char playAgain;
       std::cout << "Play again? (Yes/No): ";
       std::cin >> playAgain;
       if (playAgain == 'No' || playAgain == 'N') {
           std::cout << "Good-bye!\n";
           break;
       }
   } while (true);
   return 0;
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Algorithms Questions!