I created a simple guessing game where the player can choose whether the player is guessing the number or the computer.
If the player is guessing the number, then the computer will generate a random number between 1 to 100. Then, the player must guess the computer's number.
First, the player will type their guessed number. If it is too high than the computer's number, then the program will print out that the player's number is too high if it is too low, vice versa.
If it is correct, then the computer will congratulate the player and ask if the player wants to play again or no. If the player wants to play again, the program will restart, but if the player does not want to play again, the program will exit.
If the computer is guessing the number, then the player will think of a number. The computer will print out a number and ask if the player's number is higher or lower. The computer will keep doing this until it finds the number.
I'm looking for feedback on absolutely everything that could make me a better programmer, especially a better C++ programmer, such as:
- Optimization
- Bad practice and good practice
- Code structure
- Functions and variable naming (to be honest, I'm not really good at naming, lol)
- Bugs
- etc
Thank you very much!
I'm using Visual Studio Community 2019 ver 16.7.6
Globals.h
#ifndef GUARD_GLOBALS_H
#define GUARD_GLOBALS_H
static const char COMPUTER_GUESSER = 'c';
static const char PLAYER_GUESSER = 'p';
static const char QUIT = 'q';
static const char ANSWER_IS_YES = 'y';
static const char ANSWER_IS_NO = 'n';
static const int MAX_NUMBER = 100;
static const int MIN_NUMBER = 1;
#endif
BracketingSearch.h
#ifndef GUARD_BRACKETINGSEARCH_H
#define GUARD_BRACKETINGSEARCH_H
int randomNumGenerator(const int max, const int min);
int rangeNumToGuess(const int max, const int min);
int rangeNum(const int max, const int min);
bool startGame();
bool computerOrPlayer(const char userchoice);
bool computerGuesser();
bool playerGuesser();
bool restart();
#endif
BracketingSearch.cpp
#include <iostream>
#include "Globals.h"
#include "BracketingSearch.h"
int randomNumGenerator(const int max, const int min)
{
return rand() % max + min;
}
int rangeNumToGuess(const int max, const int min)
{
return ((max - min) / 2) + min;
}
int rangeNum(const int max, const int min)
{
return max - min;
}
bool startGame()
{
char userChoice{};
std::cout <<
"Who will be the guesser?\n"
"C - for computer\n"
"P - for player\n"
"Q - for quit\n"
"Type one of the choice: ";
std::cin >> userChoice;
computerOrPlayer(tolower(userChoice));
restart();
return true;
}
bool computerOrPlayer(const char userchoice)
{
if (userchoice == COMPUTER_GUESSER)
{
return computerGuesser();
}
else if (userchoice == PLAYER_GUESSER)
{
return playerGuesser();
}
else if (userchoice == QUIT)
{
std::cout << "Thank you for playing\n";
}
}
bool computerGuesser()
{
char userInput{};
int maxNum = MAX_NUMBER;
int minNum = MIN_NUMBER;
int guessNum{};
int guessCount{ 1 };
int range;
std::cout << "Think of a number between 1 to 100\n";
while(maxNum != minNum)
{
++guessCount;
range = rangeNum(maxNum, minNum);
if (range == 1)
{
guessNum = maxNum;
}
else
{
guessNum = rangeNumToGuess(maxNum, minNum);
}
std::cout << "Is your number less than: " << guessNum << "?(y/n): ";
std::cin >> userInput;
switch (userInput)
{
case ANSWER_IS_YES:
maxNum = guessNum - 1;
break;
case ANSWER_IS_NO:
minNum = guessNum;
break;
default:
std::cout << "That is a wrong option\n";
guessCount -= 1;
break;
}
if (maxNum == minNum)
{
std::cout << "Your number is: " << maxNum << std::endl;
std::cout << "It took " << guessCount << " guesses for me to guess" << std::endl;
}
}
return true;
}
bool playerGuesser()
{
int userGuess{};
int guessCount{ 1 };
int number = randomNumGenerator(MAX_NUMBER, MIN_NUMBER);
std::cout << "Enter your guess number: ";
while (std::cin >> userGuess)
{
++guessCount;
if (userGuess > number)
{
std::cout << "Too high!\n";
}
else if (userGuess < number)
{
std::cout << "Too low!\n";
}
else if (userGuess == number)
{
std::cout <<
"Your guess is correct!\n"
"It took you: " << guessCount << " guesses\n";
break;
}
std::cout << "Guess another number: ";
}
return true;
}
bool restart()
{
char userChoice{};
std::cout << "Play again? (y/n): ";
std::cin >> userChoice;
char lowerUserChoice = tolower(userChoice);
if (lowerUserChoice == ANSWER_IS_YES)
{
startGame();
}
else if (lowerUserChoice == ANSWER_IS_NO)
{
computerOrPlayer(QUIT);
}
else
{
std::cout << "Please choose the available option\n";
restart();
}
return true;
}
main.cpp
#include "BracketingSearch.h"
#include <cstdlib>
#include <ctime>
int main()
{
srand((unsigned)time(0));
startGame();
return 0;
}