Code: Select all
// guessing_game.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <limits>
using namespace std;
int tries = 0;
int mynumber;
char response;
char second_response;
char third_response;
int guess();
int game();
int think_of_number();
int _continue();
int higherlower();
int _tmain(int argc, _TCHAR* argv[])
{
int think_of_number(); // asks user to think of a number and waits for input
{
cout << "think of a number between 1 and 100 \n";
cout << "press enter when ready \n";
cin.clear();
cin.get();
int guess();
}
int guess(); // randomizes computers guess at what number user is thinking of
{
srand(time(0));
mynumber = rand() % 100 + 1;
int game();
}
int game(); // main game loop
{
tries++; // incriments the number of attempts the computer has made at guessing users number
cout << " is your number " << mynumber << "\n";
cout << " <y/n> \n";
cin.clear(response);
cin >> response;
switch (response)
{
case 'y' : // if the computer guessed correctly prints victory message
cout << " the computer guessed right in " << tries << " tries \n";
int _continue(); // asks user if they would like to play again
{
cout << " would you like to play again ? <y/n> \n ";
cin.clear(second_response);
cin >> second_response;
switch (second_response)
{
case 'y' : // runs the game from the beginning
int think_of_number();
case 'n' : // exits the game
;
default : // prints that you have pressed an invalid key
cout << " invalid entry \n";
int _continue(); // runs the would you like to play functionality over again
}
}
case 'n' : // indicates that the computers guess was wrong
cout << " the computer guessed wrong \n";
int higherlower(); // asks the user if the number is higher or lower and waits for response
{
cout << " is your number higher or lower ? <h/l> \n";
cin.clear(third_response);
cin >> third_response;
switch (third_response)
{
case 'h' : // takes mynumber and incriments it by half its value and returns to the main game loop
mynumber = mynumber + (( 100 - mynumber) / 2);
int game();
case 'l' : // takes mynumber and halves it and returns to the main game loop
mynumber = mynumber - (mynumber / 2);
int game();
default : // indicates the user pressed a wrong key and restarts the higherlower function
cout << " invalid entry \n";
int higherlower();
}
}
}
}
int think_of_number(); // starts the game over again
return 0;
}