Here's a shorter version of my 'game' :)
Code:
#include <iostream.h>
#include <windows.h>
#include <conio.h>
#include <stdlib.h>
#include <time.h>
// function declerations
void maingame();
void newgame();
void help();
void exitgame();
void calculate(int gamer, int computer);
void infinite();
void points();
void sessionnumber();
void personal();
void pointdetails();
void gametype();
void title();
void loading();
void checkinput();
void choice();
void draw();
void userwin();
void AIwin();
void exitingame();
void update();
// global variables
int ingamechoice, AIchoice, userscore, AIscore, menuchoice, pointwin, pointdraw, pointloss, sessions, session, sessionchoice, pointslimit,i;
char username[12];
char AIname[12];
char name[3][10] = { "Scissors" , "Paper" , "Stone" };
char scorevis,quit;
// main function
int main()
{
maingame();
return 0;
}
// maingame() function
void maingame()
{
userscore = 0;
AIscore = 0;
cout<<flush;
do
{
system("CLS");
title();
cout<<"\n\n\t[ BETA V3.00 ]";
cout<<"\n\n\t[ M E N U ]";
cout<<"\n\n\t[1] Start New Game";
cout<<"\n\t[2] Help";
cout<<"\n\t[3] Exit Game";
cout<<"\n\n\tChoice :: [ ]\b\b";
cout<<flush;
menuchoice = getche();
} while (!( menuchoice == '1' || menuchoice == '2' || menuchoice == '3'));
switch(menuchoice)
{
case '1':
newgame();
case '2':
help();
maingame();
case '3':
exitgame();
break;
default: maingame();
}
}
// newgame() function
void newgame()
{
personal();
cout<<"\n\n\t\tYour first name [ max: 10 char ]";
cout<<"\n\n\t\tPlayer 1:\t[ ]\b\b\b\b\b\b\b\b\b\b\b\b"<<flush;
cin.getline(username,12);
cout<<"\n\n\t\tOpponent's (computer) name [ max: 10 char ]";
cout<<"\n\n\t\tPlayer 2:\t[ ]\b\b\b\b\b\b\b\b\b\b\b\b";
cin.getline(AIname,12);
while (strcmp(username,AIname) == 0)
{
personal();
cout<<"\n\n\t[ Please re enter - Names may not be identical. ]";
cout<<"\n\n\t\tYour first name [ max: 10 char ]";
cout<<"\n\n\t\tPlayer 1:\t[ ]\b\b\b\b\b\b\b\b\b\b\b\b"<<flush;
cin.getline(username,12);
cout<<"\n\n\t\tOpponent's (computer) name [ max: 10 char ]";
cout<<"\n\n\t\tPlayer 2:\t[ ]\b\b\b\b\b\b\b\b\b\b\b\b";
cin.getline(AIname,12);
}
for(i=1; i>0; i--)
{
do
{
pointdetails();
cout<<"\n\n\tPoints for a win [ between 1 and 100 ]...";
cout<<"\n\n\t\t>> ";
cin>>pointwin;
checkinput();
} while (pointwin < 1 || pointwin > 100 );
}
for(i=1; i>0; i--)
{
do
{
pointdetails();
cout<<"\n\n\tPoints for a win [ between 1 and 100 ]...";
cout<<"\n\t\t>> "<<pointwin;
cout<<"\n\n\tPoints for a draw [ between 0 and 100 ]...";
cout<<"\n\n\t\t>> ";
cin>>pointdraw;
checkinput();
} while (pointdraw < 0 || pointdraw > 100 );
}
for(i=1; i>0; i--)
{
do
{
pointdetails();
cout<<"\n\n\tPoints for a win [ between 1 and 100 ]...";
cout<<"\n\t\t>> "<<pointwin;
cout<<"\n\n\tPoints for a draw [ between 0 and 100 ]...";
cout<<"\n\t\t>> "<<pointdraw;
cout<<"\n\n\tPoints for a loss [ between 0 and 100 ]...";
cout<<"\n\n\t\t>> ";
cin>>pointloss;
checkinput();
} while (pointloss < 0 || pointloss > 100 );
}
pointdetails();
cout<<"\n\n\tPoints for a win [ between 1 and 100 ]...";
cout<<"\n\t\t>> "<<pointwin;
cout<<"\n\n\tPoints for a draw [ between 0 and 100 ]...";
cout<<"\n\t\t>> "<<pointdraw;
cout<<"\n\n\tPoints for a loss [ between 0 and 100 ]...";
cout<<"\n\t\t>> "<<pointloss;
cout<<flush;
Sleep(1000);
int gametypex;
do
{
gametype();
cout<<"\n\n\t[1] Play till an amount of points are won";
cout<<"\n\n\t[2] Play a number of sessions";
cout<<"\n\n\t[3] Play unlimited number of sessions";
cout<<"\n\n\tChoice :: [ ]\b\b";
cout<<flush;
gametypex = getche();
} while (!( gametypex == '1' || gametypex == '2' || gametypex == '3'));
switch (gametypex)
{
case '1':
{
do
{
gametype();
cout<<"\n\n\t[ Play till an amount of points are won ]";
cout<<"\n\n\tMax. Points... ";
cout<<"\n\n\t>> ";
cin>>pointslimit;
} while (pointslimit < 1 || pointslimit > 1000000);
do
{
gametype();
cout<<"\n\n\t[ Play till an amount of points are won ]";
cout<<"\n\n\tMax. Points... ";
cout<<"\n\n\t>> "<<pointslimit;
cout<<"\n\n\n\tScore visible while playing...YES (y) / NO (n)";
cout<<"\n\n\t>> ";
cin>>scorevis;
} while (scorevis != 'y' && scorevis != 'n');
gametype();
cout<<"\n\n\t[ Play till an amount of points are won ]";
cout<<"\n\n\tMax. Points... ";
cout<<"\n\n\t>> "<<pointslimit;
cout<<"\n\n\n\tScore visible while playing...YES (y) / NO (n)";
cout<<"\n\n\t>> ";
if (scorevis == 'y')
cout<<"Yes";
else cout<<"No";
break;
}
case '2':
{
do
{
gametype();
cout<<"\n\n\t[ Play a number of sessions ]";
cout<<"\n\n\tNo. of sessions [ between 1 and 1000000 ]...";
cout<<"\n\n\t>> ";
cin>>sessions;
} while (sessions < 1 || sessions > 1000000);
system("CLS");
title();
cout<<"\n\n\t[ N E W G A M E ]";
cout<<"\n\n\tGAME TYPE:";
cout<<"\n\n\t[ Play a number of sessions ]";
cout<<"\n\n\tNo. of sessions [ between 1 and 1000000 ]...";
cout<<"\n\n\t>> "<<sessions;
break;
}
case '3':
{
gametype();
cout<<"\n\n\t[ Play unlimited number of sessions ]";
break;
}
}
cout<<flush;
Sleep(500);
loading();
if (gametypex == '3')
{
session = 0;
infinite();
}
else if (gametypex == '1')
{
session = 0;
points();
}
else if (gametypex == '2')
{
session = 0;
sessionnumber();
}
}
// help() function
void help()
{
cout<<flush;
system("CLS");
title();
cout<<"\n\n\t[ H E L P ]";
cout<<"\n\n\tTo play, you must choose one of three objects.";
cout<<"\n\tYou may choose either scissors, paper or stone.";
cout<<"\n\tNext, the computer will randomly choose one.";
cout<<"\n\n\tThe winner is chosen after the two choices";
cout<<"\n\tare compared. Remember that:";
cout<<"\n\n\tScissors beats Paper (it cuts)";
cout<<"\n\tPaper beats Stone (it wraps)";
cout<<"\n\tStone beats Scissors (it blunts)";
cout<<"\n\n\t[ Press any key to return to the main menu ]";
cout<<flush;
getch();
}
void exitgame()
{
do
{
system("CLS");
cout<<"\n\n\tDo you really want to QUIT? (y or n)";
cout<<"\n\n\tChoice :: [ ]\b\b";
cout<<flush;
quit = getche();
} while (quit != 'y' && quit != 'n');
if (quit == 'y')
exit(0);
else
maingame();
}
// calculate() function
void calculate(int gamer, int computer)
{
if (gamer == 1)
{
if (computer == 1)
{
draw();
}
if (computer == 2)
{
cout<<"\n\n\n\t[ SCISSORS CUT PAPER! - "<<username<<" WINS! ]";
userwin();
}
if (computer == 3)
{
cout<<"\n\n\n\t[ STONE BRAKES SCISSORS! - "<<AIname<<" WINS! ]";
AIwin();
}
}
if (gamer == 2)
{
if (computer == 1)
{
cout<<"\n\n\n\t[ SCISSORS CUT PAPER! - "<<AIname<<" WINS! ]";
AIwin();
}
if (computer == 2)
{
draw();
}
if (computer == 3)
{
cout<<"\n\n\n\t[ PAPER COVERS STONE! - "<<username<<" WINS! ]";
userwin();
}
}
if (gamer == 3)
{
if (computer == 1)
{
cout<<"\n\n\n\t[ STONE BRAKES SCISSORS! - "<<username<<" WINS! ]";
userwin();
}
if (computer == 2)
{
cout<<"\n\n\n\t[ PAPER COVERS STONE! - "<<AIname<<" WINS! ]";
AIwin();
}
if (computer == 3)
{
draw();
}
}
}
void infinite()
{
for (;;)
{
session++;
do
{
cout<<flush;
system("CLS");
cout<<flush;
title();
choice();
cin>>ingamechoice;
} while (ingamechoice < 1 || ingamechoice > 3 );
system("CLS");
title();
cout<<"\n\n\t[ S E S S I O N "<<session<<" ]";
cout<<"\n\n\tYou have chosen [ "<<name[ingamechoice-1]<<" ]..."<<flush;
srand(time(NULL));
AIchoice = rand()%3;
cout<<"\n\n\tYour Opponent has chosen [ "<<name[AIchoice]<<" ]...";
cout<<flush;
Sleep(300);
calculate(ingamechoice,AIchoice+1);
cout<<"\n\n\n\t[1] Continue to next session";
cout<<"\n\t[2] Check scores";
cout<<"\n\t[3] Exit";
cout<<"\n\n\tChoice :: [ ]\b\b";
cout<<flush;
int state = 0;
for (;;)
{
sessionchoice = getch();
if (sessionchoice == '1' || sessionchoice == '2' || sessionchoice == '3')
{
cout<<sessionchoice;
state = 1;
}
if (state == 1)
break;
}
switch(sessionchoice)
{
case '1':
continue;
break;
case '2':
{
cout<<flush;
system("CLS");
title();
cout<<"\n\n\t[ S C O R E S ]";
update();
cout<<"\n\n\n\t[ Press any key to continue ]";
cout<<flush;
getch();
continue;
break;
}
case '3':
exitingame();
}
};
}
void points()
{
cout<<flush;
system("CLS");
cout<<flush;
title();
cout<<"\n\n\tFirst player to reach ["<<pointslimit<<"] points WINS the game!";
cout<<"\n\n\n\t[ Press any key to start ]";
cout<<flush;
getch();
for (;;)
{
session++;
do
{
cout<<flush;
system("CLS");
cout<<flush;
title();
choice();
cout<<flush;
cin>>ingamechoice;
} while (ingamechoice < 1 || ingamechoice > 3);
system("CLS");
title();
cout<<"\n\n\t[ S E S S I O N "<<session<<" ]";
cout<<"\n\n\tYou have chosen [ "<<name[ingamechoice-1]<<" ]..."<<flush;
srand(time(NULL));
AIchoice = rand()%3;
cout<<"\n\n\tYour Opponent has chosen [ "<<name[AIchoice]<<" ]...";
cout<<flush;
Sleep(300);
calculate(ingamechoice,AIchoice+1);
if (userscore > pointslimit && userscore > AIscore)
{
cout<<flush;
system("CLS");
title();
cout<<"\n\n\t[ WE HAVE A WINNER! ]";
update();
cout<<"\n\n\n\t"<<username<<" reached "<<pointslimit<<" first!";
cout<<"\n\n\tCongratulations "<<username<<", you have won the game!";
cout<<"\n\n\n\t[ Press any key to exit ]";
cout<<flush;
getch();
exitgame();
}
else if (AIscore > pointslimit && AIscore > userscore )
{
cout<<flush;
system("CLS");
title();
cout<<"\n\n\t[ WE HAVE A WINNER! ]";
update();
cout<<"\n\n\n\t"<<AIname<<" reached "<<pointslimit<<" first!";
cout<<"\n\n\tSorry "<<username<<", better luck next time!";
cout<<"\n\n\n\t[ Press any key to exit ]";
cout<<flush;
getch();
exitgame();
}
else if (AIscore > pointslimit && AIscore == userscore )
{
cout<<flush;
system("CLS");
title();
cout<<"\n\n\t[ WE HAVE A DRAW! ]";
update();
cout<<"\n\n\n\t"<<username<<" and "<<AIname<<" reached "<<pointslimit<<" at the same time!";
cout<<"\n\n\tNice try "<<username<<", better luck next time!";
cout<<"\n\n\n\t[ Press any key to exit ]";
cout<<flush;
getch();
exitgame();
}
cout<<"\n\n\n\t[1] Continue to next session";
if (scorevis == 'y')
{
cout<<"\n\t[2] Check scores";
cout<<"\n\t[3] Exit";
cout<<"\n\n\tChoice :: [ ]\b\b";
cout<<flush;
int state = 0;
for (;;)
{
sessionchoice = getch();
if (sessionchoice == '1' || sessionchoice == '2' || sessionchoice == '3')
{
cout<<sessionchoice;
state = 1;
}
if (state == 1)
break;
}
switch(sessionchoice)
{
case '1':
continue;
break;
case '2':
{
cout<<flush;
system("CLS");
title();
cout<<"\n\n\t[ S C O R E S ]";
update();
cout<<"\n\n\n\t[ Press any key to continue ]";
cout<<flush;
getch();
continue;
break;
}
case '3':
exitingame();
}
}
else if (scorevis == 'n')
{
cout<<"\n\t[2] Exit";
cout<<"\n\n\tChoice :: [ ]\b\b";
cout<<flush;
int state = 0;
for (;;)
{
sessionchoice = getch();
if (sessionchoice == '1' || sessionchoice == '2')
{
cout<<sessionchoice;
state = 1;
}
if (state == 1)
break;
}
switch(sessionchoice)
{
case '1':
continue;
break;
case '2':
exitingame();
}
}
}
}
void sessionnumber()
{
cout<<flush;
system("CLS");
cout<<flush;
title();
cout<<"\n\n\tThe game will end after "<<sessions<<" sessions!";
cout<<"\n\n\n\t[ Press any key to start ]";
cout<<flush;
getch();
for (;;)
{
session++;
do
{
cout<<flush;
system("CLS");
cout<<flush;
title();
choice();
cin>>ingamechoice;
} while (ingamechoice < 1 || ingamechoice > 3 );
system("CLS");
title();
cout<<"\n\n\t[ S E S S I O N "<<session<<" ]";
cout<<"\n\n\tYou have chosen [ "<<name[ingamechoice-1]<<" ]..."<<flush;
srand(time(NULL));
AIchoice = rand()%3;
cout<<"\n\n\tYour Opponent has chosen [ "<<name[AIchoice]<<" ]...";
cout<<flush;
Sleep(300);
calculate(ingamechoice,AIchoice+1);
if (session == sessions)
{
if (userscore > AIscore)
{
cout<<flush;
system("CLS");
title();
cout<<"\n\n\t[ WE HAVE A WINNER! ]";
update();
cout<<"\n\n\n\t"<<username<<" has more points than "<<AIname<<"!";
cout<<"\n\n\tCongratulations "<<username<<", you have won the game!";
cout<<"\n\n\n\t[ Press any key to exit ]";
cout<<flush;
getch();
exitgame();
}
else if (AIscore > userscore)
{
cout<<flush;
system("CLS");
title();
cout<<"\n\n\t[ WE HAVE A WINNER! ]";
update();
cout<<"\n\n\n\t"<<AIname<<" has more points than "<<username<<"!";
cout<<"\n\n\tSorry "<<username<<", better luck next time!";
cout<<"\n\n\n\t[ Press any key to exit ]";
cout<<flush;
getch();
exitgame();
}
else if (AIscore == userscore)
{
cout<<flush;
system("CLS");
title();
cout<<"\n\n\t[ WE HAVE A DRAW! ]";
update();
cout<<"\n\n\n\t"<<username<<" and "<<AIname<<" reached "<<pointslimit<<" at the same time!";
cout<<"\n\n\tNice try "<<username<<", better luck next time!";
cout<<"\n\n\n\t[ Press any key to exit ]";
cout<<flush;
getch();
exitgame();
}
}
cout<<"\n\n\n\t[1] Continue to next session";
cout<<"\n\t[2] Check scores";
cout<<"\n\t[3] Exit";
cout<<"\n\n\tChoice :: [ ]\b\b";
cout<<flush;
int state = 0;
for (;;)
{
sessionchoice = getch();
if (sessionchoice == '1' || sessionchoice == '2' || sessionchoice == '3')
{
cout<<sessionchoice;
state = 1;
}
if (state == 1)
break;
}
switch(sessionchoice)
{
case '1':
continue;
break;
case '2':
{
cout<<flush;
system("CLS");
title();
cout<<"\n\n\t[ S C O R E S ]";
update();
cout<<"\n\n\n\t[ Press any key to continue ]";
cout<<flush;
getch();
continue;
break;
}
case '3':
exitingame();
}
};
}
void personal()
{
system("CLS");
title();
cout<<"\n\n\t[ N E W G A M E ]";
cout<<"\n\n\tPERSONAL DETAILS:";
}
void pointdetails()
{
system("CLS");
title();
cout<<"\n\n\t[ N E W G A M E ]";
cout<<"\n\n\tPOINTS DETAILS:";
}
void gametype()
{
system("CLS");
title();
cout<<"\n\n\t[ N E W G A M E ]";
cout<<"\n\n\tGAME TYPE:";
}
void title()
{
cout<<"\n\n\t[[ Scissors, Paper, Stone 2002 ]] - [ Coded by mvb ]";
}
void loading()
{
system("CLS");
cout<<"\n\n\n\n\t\t[ Starting Game ]";
cout<<flush;
Sleep(300);
system("CLS");
cout<<"\n\n\n\n\t\t [ Starting Game ]";
cout<<flush;
Sleep(300);
system("CLS");
cout<<"\n\n\n\n\t\t [ Starting Game ]";
cout<<flush;
Sleep(300);
system("CLS");
cout<<"\n\n\n\n\t\t [ Starting Game ]";
cout<<flush;
Sleep(300);
}
void checkinput()
{
if (cin.fail())
{
cin.clear();
cin.ignore();
i++;
}
}
void choice()
{
cout<<"\n\n\t[ S E S S I O N "<<session<<" ]";
cout<<"\n\n\tPlease choose between...";
cout<<"\n\n\t\t[1] Scissors";
cout<<"\n\n\t\t[2] Paper";
cout<<"\n\n\t\t[3] Stone";
cout<<"\n\n\tChoice :: [ ]\b\b";
}
void draw()
{
cout<<"\n\n\n\t[ DRAW - NOBODY WINS! ]";
cout<<"\n\n\t"<<username<<": ";
if (pointdraw == 1)
cout<<"1 point";
else
cout<<pointdraw<<" points";
cout<<"\n\n\t"<<AIname<<": ";
if (pointdraw == 1)
cout<<"1 point";
else
cout<<pointdraw<<" points";
userscore += pointdraw;
AIscore += pointdraw;
}
void userwin()
{
cout<<"\n\n\t"<<username<<": ";
if (pointwin == 1)
cout<<"1 point";
else
cout<<pointwin<<" points";
cout<<"\n\n\t"<<AIname<<": ";
if (pointloss == 1)
cout<<"1 point";
else
cout<<pointloss<<" points";
userscore += pointwin;
AIscore += pointloss;
}
void AIwin()
{
cout<<"\n\n\t"<<username<<": ";
if (pointloss == 1)
cout<<"1 point";
else
cout<<pointloss<<" points";
cout<<"\n\n\t"<<AIname<<": ";
if (pointwin == 1)
cout<<"1 point";
else
cout<<pointwin<<" points";
userscore += pointloss;
AIscore += pointwin;
}
void exitingame()
{
do
{
system("CLS");
cout<<"\n\n\tDo you really want to QUIT? (y or n)";
cout<<"\n\n\tChoice :: [ ]\b\b";
cout<<flush;
quit = getche();
} while (quit != 'y' && quit != 'n');
if (quit == 'y')
exit(0);
}
void update()
{
cout<<"\n\n\t[ After ";
if (session == 1)
cout<<"1 session ]";
else
cout<<session<<" sessions ]";
cout<<"\n\n\n\t"<<username<<"...";
if (userscore == 1)
cout<<"1 point";
else
cout<<userscore<<" points";
cout<<"\n\n\t"<<AIname<<"...";
if (AIscore == 1)
cout<<"1 point";
else
cout<<AIscore<<" points";
}