I'm learning C++ right now and one of the activity/tutorial things was to re-write the "High/Low Guess My Number" game - with a twist. I had to re-write it so that the computer had to guess the number.
Here's the exercise prompt:
Write a new version of the Guess My Number program in which the player and computer switch roles. That is, the player picks a number and the computer must guess what it is
And, here's what I wrote:
// Guess My Number - Computer vs Player
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
srand(static_cast<unsigned int>(time(0)));
int tries = 0;
// short stuff; // Waiting for solution
bool error = false;
enum statusTypes {NADA, HIGH, LOW, CORRECT};
int status = NADA;
int min = 0;
int max = 101;
int guess = rand() % 100 + 1;
//int previousGuess; // Only for duplicate guess checking
// ^ Not used currently, 1.1 release will fix duplicate PC guesses
int toGuess;
cout << "01010111 01100101 01101100 01100011 01101111 01101101 01100101 00100001\n";
cout << "Welcome to Guess My Number\n";
cout << "Computer vs Player Edition\n\n";
cout << "Please enter your number (between 1 & 100): ";
cin >> toGuess;
cin.get();
while (status != CORRECT)
{
++tries;
cout << "Computer's Guess: " << guess << "\n";
//previousGuess = guess; // Part of Duplicate PC guesses (1.1)
cout << "Press ENTER to continue..." << endl;
cin.get();
/*cout << "\n1st Debug Statements:\n" << endl;
cout << "Here's the current values: \n";
cout << "Status: " << status << endl;
cout << "toGuess: " << toGuess << endl;
cout << "guess: " << guess << endl;
cout << "tries: " << tries << endl;
// ^ Debugging Statements*/
if (guess < toGuess)
{
status = LOW;
//cout << "\nStatus: " << status << "\n"; // Debugging
}
else if (guess > toGuess)
{
status = HIGH;
//cout << "\nStatus: " << status << "\n"; // Debugging
}
else if (guess == toGuess)
{
status = CORRECT;
//cout << "\nStatus: " << status << "\n"; // Debugging
}
else
{
error = true;
cout << "Uh, something went wrong.\n";
cout << "Here's the current values: \n";
cout << "Status: " << status << endl;
cout << "toGuess: " << toGuess << endl;
cout << "guess: " << guess << endl;
cout << "tries: " << tries << endl;
}
/*cout << "\n2nd Debug Statements\n" << endl;
cout << "Here's the current values: \n";
cout << "Status: " << status << endl;
cout << "toGuess: " << toGuess << endl;
cout << "guess: " << guess << endl;
cout << "tries: " << tries << endl;
// ^ Debugging Statements*/
if (status == HIGH)
{
max = guess;
//cout << "\nReached status == HIGH" << endl; // Debugging
do
{
guess = rand() % 100 + 1;
} while (guess > max || guess < min);
}
else if (status == LOW)
{
min = guess;
//cout << "\nReached status == LOW" << endl; // Debugging
do
{
guess = rand() % 100 + 1;
} while (guess > max || guess < min);
}
else if (status == CORRECT)
{
cout << "Computer Guessed It!\n";
cout << "The guess was " << guess << "\n";
cout << "And it took " << tries << " tries!\n";
cout << "Thanks for playing!" << endl;
}
else
{
error = true;
cout << "Uh, something went wrong.\n";
cout << "Here's the current values: \n";
cout << "Status: " << status << endl;
cout << "toGuess: " << toGuess << endl;
cout << "guess: " << guess << endl;
cout << "tries: " << tries << endl;
}
/*cout << "\n3rd Debug Statements:\n" << endl;
cout << "Here's the current values: \n";
cout << "Status: " << status << endl;
cout << "toGuess: " << toGuess << endl;
cout << "guess: " << guess << endl;
cout << "tries: " << tries << endl;
// ^ Debugging Statements */
}
return 0;
}
Obviously, this isn't the most crucial of projects. I was just curious how my coding skills are!
LOW
,HIGH
,CORRECT
at each iteration. (so the user knows the computer actually guessed) \$\endgroup\$