#include <iostream>
#include <string>
using namespace std;
void create_numbers_to_guess(int guesses[], int arrSize);
void provide_assistance(int guess, int answer);
void clear_guessed_numbers(int guessed[], int arrSize);
int main ()
{
string prompt = "Enter a number: ";
string help_prompt = "Would you like some assistance?";
string help_prompt_2 = "You have used up available assistance. Good luck!";
string win_prompt = "Excellent! You win!";
string lose_prompt = "Unfortunately, probability has defeated you.";
string instructions = "Press Ctrl + Z or Ctrl + D to end the game. Enter (number) 0 to get assistance. Enter a number and press Enter to input your guess.";
int guessedInput = 0;
int answer = 0;
const int max_guesses = 1024;
int guessed_numbers[max_guesses];
int number_to_guess[max_guesses];
int guesses_used = 0;
const int total_lifelines = 256;
int lifelines_used = 0;
cout << instructions << endl;
create_numbers_to_guess(number_to_guess, max_guesses);
for (int i = 0; i < max_guesses; ++i)
{
answer = number_to_guess[i];
guesses_used = 0;
lifelines_used = 0;
clear_guessed_numbers(guessed_numbers, max_guesses);
cout << answer << endl;
while (cin.good())
{
if (guesses_used == max_guesses)
{
cout << lose_prompt << endl;
cin.eof(); // set flag for cin to close and make while loop break;
}
cout << prompt << endl;
cin >> guessedInput;
if (guessedInput == answer)
{
cout << win_prompt << endl;
break;
}
if (guessedInput == 0 && lifelines_used == total_lifelines)
{
cout << help_prompt_2 << endl;
}
else if (guessedInput == 0)
{
provide_assistance(guessedInput, answer);
++lifelines_used;
}
guessed_numbers[guesses_used] = guessedInput;
++guesses_used;
}
}
return 0;
}
void create_numbers_to_guess(int guesses[], int arrSize)
{
for (int i = 0; i < arrSize; ++i)
{
int aGuess = rand() % 5;
if (aGuess == 0) { aGuess = 1; }
guesses[i] = aGuess;
}
}
void provide_assistance(int guess, int answer)
{
string high_prompt = "Go higher.";
string low_prompt = "Go lower.";
if (guess > answer)
{
cout << low_prompt << endl;
}
else if (guess < answer)
{
cout << high_prompt << endl;
}
}
void clear_guessed_numbers(int guessed[], int arrSize)
{
for (int i = 0; i < arrSize; ++i)
{
guessed[i] = 0;
}
}
Comments
0 B
|👍
/👎