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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started