it's a simple game, you've got to guess whats the number thats been randomly generated. I cant help having the feeling that this code could be cleaner, I'd like suggestions from more experienced programmers.
Code:#include <iostream> #include <cstdlib> #include <string> using namespace std; void game(); string turnString(int n); string clue(int n, int randNumber); string comments(int turn); int playAgain; int main() { do { game(); cout<<"\n\n\nPLAY AGAIN? (TYPE 1 FOR YES): "; cin>>playAgain; cout<<"\n\n\n"; } while(playAgain == 1); } void game() { srand(time(0)); int randNum = (rand() % 15) + 1; int chances = 5; int turn = 1; int input; cout<<"Random number has been generated (1 to 15),\nyou've got 5 chances to guess it. GOOD LUCK!"<<endl; cin.get(); while(turn <= 5 && input != randNum) { cout<<"\nGuess Number "<<turn<<": "; cin>>input; cout<<clue(input,randNum)<<endl; turn++; cin.get(); } turn--; if(input == randNum) { cout<<"\n\nCongratulations! You've guessed it!"<<endl; cin.get(); cout<<"You've done it on the "<<turnString(turn)<<" turn."; cin.get(); cout<<"\nComments: "<<comments(turn); cin.get(); } else { cout<<"\n\nYou've run outta chances!\nThe random number was "<<randNum<<".\nTRY AGAIN, SUCKER!"; cin.get(); cin.get(); } } string turnString(int n) { string stringReturn; switch(n) { case 1: stringReturn = "first"; break; case 2: stringReturn = "second"; break; case 3: stringReturn = "third"; break; case 4: stringReturn = "fourth"; break; case 5: stringReturn = "fifth"; break; } return stringReturn; } string clue(int n,int randNumber) { string stringReturn; if(randNumber != n && randNumber > n) { stringReturn = "Try higher."; } if(randNumber != n && randNumber < n) { stringReturn = "Try lower."; } return stringReturn; } string comments(int turn) { string comments[6]; comments[1]="FLAWLESS VICTORY!"; comments[2]="Good job!"; comments[3]="Hmmm... not bad."; comments[4]="You could've done better."; comments[5]="You saved your ass this time, pal."; return comments[turn]; }



LinkBack URL
About LinkBacks



