* Program: guess-that-number.c
* This program is an implementation of the "guess that number"
* game. The computer randomly chooses a number and the player
* attempts to guess it. (It should never take more than 7 guesses)
const int MAX_NUMBER = 100;
const int MAX_GUESSES = 7;
* Read an integer from the user
* @param prompt the message to show the user
* @returns the integer entered
int read_integer(string prompt)
string line = read_line();
while (!is_integer(line))
write_line("Please enter a whole number.");
* Print a line onto the Terminal.
* @param len the length of the line to print
for (int i = 0; i < len; i++)
* Perform the steps for the guess. Reads the value entered by the user,
* outputs a message, and then returns true if the got it otherwise it returns
* @param guess_number the sequence number of the guess, this should start at 1
* @param target the number the user is aiming to guess
* @return true if the user guesses the number, otherwise false.
bool perform_guess(int guess_number, int target)
guess = read_integer("Guess " + to_string(guess_number) + ": ");
write_line("The number is less than " + to_string(guess));
write_line("The number is larger than " + to_string(guess));
write_line("Well done... the number was " + to_string(guess));
* Implements a simple guessing game. The program generate
* a random number, and the player tries to guess it.
int my_number, guess_number;
my_number = rnd(MAX_NUMBER) + 1;
guess_number = 0; // Keep track of the number of guesses
write_line("I am thinking of a number between 1 and " + to_string(MAX_NUMBER) + "\n");
got_it = perform_guess(guess_number, my_number);
} while (guess_number < MAX_GUESSES && !got_it);
write_line("You ran out of guesses... the number was " + to_string(my_number) + "\n");
* Loops the guessing game until the user decided to quite.
write("Do you want to play again [Y/n]? ");
} while (again != "N" && again != "n");