Below you can find the source of my small c++ guess the number game, with an AI!
If anyone knows what can be changed to make it better, please tell me.
Have fun!
First version:
#include <iostream> #include <string> #include <ctime> /* For time()*/ #include <cstdlib> /* For srand() and rand()*/ #include <cctype> using namespace std; main () { game: /*The Game start here ##################################################*/ srand(time(0)); /*Initialize random number generator*/ string again; string winner; int number=(rand() % 99) +1; int guess; int ncguess; int cguess; int lowest=1; int highest=100; int errors=0; int cerrors=0; int tries=0; int ctries=0; int score=1000; int cscore=1000; int lostscore; cout << "\x1b[32mGuess the number! It's somewhere between 1-100.\n"; guess: /*Gues the number between 1 and 100 ##################################################*/ cout << "\nYour guess:"; cin >> guess; if(guess>=100){ errors++; cout << "BETWEEN 1 and 100 I said!!!"; goto computerguess; } else if(guess<=0){ errors++; cout << "BETWEEN 1 and 100 I said!!!"; goto computerguess; } else if(guess==number){ tries++; winner="You"; cscore=cscore-150; goto gameover; } else if(guess>=number){ tries++; cout << "Too high.\n"; if(guess<=highest){ highest=guess; } lostscore=guess-number; score=score-lostscore; goto computerguess; } else if(guess<=number){ tries++; cout << "Too low.\n"; if(guess>=lowest){ lowest=guess; } lostscore=number-guess; score=score-lostscore; goto computerguess; } computerguess: /*The computers guess ##################################################*/ ncguess=highest-lowest-1; //cout << ncguess << ", " << highest << ", " << lowest << endl; cguess=(rand() % ncguess) +lowest+1; //cout << "Cguess = %" << ncguess << "+" << lowest << endl; cout << "\nI think the number is " << cguess << "." << endl; if(cguess==number){ ctries++; winner="The computer"; score=score-150; goto gameover; } else if(cguess>=number){ ctries++; highest=cguess; cout << "Too high.\n"; lostscore=guess-number; cscore=cscore-lostscore; goto guess; } else if(cguess<=number){ ctries++; lowest=cguess; cout << "Too low.\n"; lostscore=number-guess; score=score-lostscore; goto guess; } gameover: /*GAMEOVER ##################################################*/ cout << endl << winner << " found the number! (" << number << ")" << endl << endl; cout << "You:" << endl; cout << " " << tries << " tries; " << errors << " errors.\n"; score=score-errors*100-lose; cout << "Final Score: " << score << endl << endl; cout << "The computer:" << endl; cout << " " << ctries << " tries; " << cerrors << " errors.\n"; cscore=cscore-cerrors*100-close; cout << "Final Score: " << cscore << endl << endl; cout << "\n\nPlay again? (y/n):_\b"; again: cin >> again; if(again=="n"){ cout << "\x1b[0m"; return 0; } else if(again=="y"){ goto game; } else{ cout << "What did you say? (y/n)"; goto again; } return 0; }
Second version (choose number range up to 1-10000):
#include <iostream>
#include <string>
#include <ctime> /* For time()*/
#include <cstdlib> /* For srand() and rand()*/
#include <cctype>
using namespace std;
main ()
{
game: /*The Game start here
##################################################*/
srand(time(0)); /*Initialize random number generator*/
string again;
string winner;
int highestn;
int number;
int guess;
int ncguess;
int cguess;
int lowest=1;
int highest;
int errors=0;
int cerrors=0;
int tries=0;
int ctries=0;
int score=1000;
int cscore=1000;
int lostscore;
prepare: /*Preparations*/
cout « "What is the highest number? (>=10 and <=10000)" « endl;
cin » highestn;
highest=highestn;
number=(rand() % highestn-1) +1;
cout « "A random number between 1 and " « highestn « " has been chosen." « endl;
cout « "\x1b[32mGuess the number! It's somewhere between 1-"«highestn«".\n";
guess: /*Gues the number
##################################################*/
cout « "\nYour guess:";
cin » guess;
if(guess>=highestn){
errors++;
cout « "BETWEEN 1 and "«highestn«"I said!!!";
goto computerguess;
}
else if(guess<=0){
errors++;
cout « "BETWEEN 1 and "«highestn«"I said!!!";
goto computerguess;
}
else if(guess==number){
tries++;
winner="You";
cscore=cscore-150;
goto gameover;
}
else if(guess>=number){
tries++;
cout « "Too high.\n";
if(guess<=highest){
highest=guess;
}
lostscore=guess-number;
score=score-lostscore;
goto computerguess;
}
else if(guess<=number){
tries++;
cout « "Too low.\n";
if(guess>=lowest){
lowest=guess;
}
lostscore=number-guess;
score=score-lostscore;
goto computerguess;
}
computerguess: /*The computers guess
##################################################*/
ncguess=highest-lowest-1;
//cout « ncguess « ", " « highest « ", " « lowest « endl;
cguess=(rand() % ncguess) +lowest+1;
//cout « "Cguess = %" « ncguess « "+" « lowest « endl;
cout « "\nI think the number is " « cguess « "." « endl;
if(cguess==number){
ctries++;
winner="The computer";
score=score-150;
goto gameover;
}
else if(cguess>=number){
ctries++;
highest=cguess;
cout « "Too high.\n";
lostscore=guess-number;
cscore=cscore-lostscore;
goto guess;
}
else if(cguess<=number){
ctries++;
lowest=cguess;
cout « "Too low.\n";
lostscore=number-guess;
score=score-lostscore;
goto guess;
}
gameover: /*GAMEOVER
##################################################*/
cout « endl « winner « " found the number! (" « number « ")" « endl « endl;
cout « "You:" « endl;
cout « " " « tries « " tries; " « errors « " errors.\n";
score=score-errors*100;
cout « "Final Score: " « score « endl « endl;
cout « "The computer:" « endl;
cout « " " « ctries « " tries; " « cerrors « " errors.\n";
cscore=cscore-cerrors*100;
cout « "Final Score: " « cscore « endl « endl;
cout « "\n\nPlay again? (y/n):_\b";
again:
cin » again;
if(again=="n"){
cout « "\x1b[0m";
return 0;
}
else if(again=="y"){
goto game;
}
else{
cout « "What did you say? (y/n)";
goto again;
}
return 0;
}
Edit by Mapar007: put the code in a "collapsible"