1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
|
#include <iostream>
#include <ctime>
#include <cstdlib>
static void YouWin(int i, int guessnum); // If user won, they get an applause.
static void YouWin(int i, int guessnum)
{
if(i>1) // Nudge them to play again
{
std::cout<<"\n\nWINNER! You guessed my number, "<<guessnum<<", in "<<i<<" moves! Nice! Try to guess in "<<i-1<<" moves!";
}
else // If they somehow guessed the number in 1 move, they get praised.
{
std::cout<<"\n\nPRO! You guessed my number, "<<guessnum<<", in "<<i<<" moves! Genius! Either you are a magician or you are a mind-reader!";
}
}
main()
{
int guess,guessnum,i;
bool win;
win=false; //initialize checking value
std::cout<<"This is a number guessing game!\n";
std::cout<<"I will randomly choose a number between 100 and 0 and you will have to guess it in 10 guesses.\n"; //prompts user
std::cout<<"Okay, lets begin!";
std::cin.ignore();
std::cout<<"\n\nThinking of a number...";
srand(time(NULL)); //gets number
guessnum=rand()% 99 + 1;
std::cout<<"\nThought of a number!\n\n";
for(i=1;i<11;i++)
{
std::cout<<"Guess Number "<<i<<": "; //prompts user to guess a number (includes turn)
std::cin>>guess;
if(guess>guessnum)
{
std::cout<<"Too high!\nTry again.\n\n"; //if their guess is too high, we say too high
}
else if(guess<guessnum)
{
std::cout<<"Too low!\nTry again.\n\n"; //vice-versa if their guess it too low, we say too low
}
else
{
YouWin(i,guessnum); //otherwise, they guessed it and we praise them, check that they won, and break from the loop
win=true;
break;
}
}
if(i==10 && win==false) // if they lose (win==false), and their turns are over (i==10), we prompt them pur number and wish them luck
{
std::cout<<"You did not guess my number in 10 moves! The number was "<<guessnum<<". Better luck next time!";
}
}
|