// RPS.CPP
// By Ken Mathis (CheckMate)
// Rock Paper Scissors game
// Added score keeping 8/20/02
// Header files.
#include
#include
#include
// Function declarations.
int computer();
void win_lose(int, int);
void display();
void record(int);
void printstats();
// Main function.
int main()
{
// Integer declarations.
int choice, comp_choice;
// Game loop.
do
{
display(); // Calls the display function.
// Get's user's choice.
cout << ": ";
cin >> choice;
// Check if user wants to exit.
if (choice == 5)
{
cout << "Bye Bye! :)\n";
break;
}
// check if the user wants to view stats
else if (choice == 4)
{
printstats();
}
// If user doesn't wannt to exit it continues the program.
else
{
// Get computer choice
comp_choice = computer();
// Check if it's win lose or draw.
win_lose(comp_choice,choice);
}
} while(1);
return 0;
}
void display() // Display function.
{
cout << "1 = Rock... 2 = Paper... 3 = Scissors... 4 = Stats... 5 = Exit\n";
}
int computer() // Computer choice function.
{
int x = 1 + rand() % 3; // Random number from computer.
// Check if he chose Rock Paper or Scissors.
switch(x)
{
case 1:
cout << "Comp Chose: Rock.\n";
break;
case 2:
cout << "Comp Chose: Paper.\n";
break;
case 3:
cout << "Comp Chose: Scissors.\n";
break;
}
return (x);
}
void win_lose(int comp, int user) // Win/Lose/Draw function.
{
// If comp chose Rock.
if (comp == 1 && user == 1)
{
cout << "Tie!\n";
record(2); // update records
}
if (comp == 1 && user == 2)
{
cout << "You won!\n";
record(0);
}
if (comp == 1 && user == 3)
{
cout << "You lost!\n";
record(1);
}
// If comp chose Paper.
if (comp == 2 && user == 2)
{
cout << "Tie!\n";
record(2);
}
if (comp == 2 && user == 3)
{
cout << "You won!\n";
record(0);
}
if (comp == 2 && user == 1)
{
cout << "You lost!\n";
record(1);
}
// If comp chose Scissors.
if (comp == 3 && user == 3)
{
cout << "Tie!\n";
record(2);
}
if (comp == 3 && user == 1)
{
cout << "You won!\n";
record(0);
}
if (comp == 3 && user == 2)
{
cout << "You lost!\n";
record(1);
}
}
// update records
/*
0 - player win
1 - player lose
2 - tie
*/
void record(int result)
{
int wins, loses, ties;
ifstream infile;
ofstream outfile;
infile.open("stats.dat",ios::in); // get current stats
infile >> wins;
infile >> loses;
infile >> ties;
infile.close();
switch (result) // check results
{
case 0:
wins++;
break;
case 1:
loses++;
break;
case 2:
ties++;
break;
}
outfile.open("stats.dat",ios::out); // update the stats
outfile << wins << endl;
outfile << loses << endl;
outfile << ties << endl;
outfile.close();
}
// display stats
void printstats()
{
int wins, loses, ties;
ifstream infile; // get stats from file
infile.open("stats.dat",ios::in);
infile >> wins;
infile >> loses;
infile >> ties;
infile.close();
// display results
cout << "You won: " << wins << endl;
cout << "You lost: " << loses << endl;
cout << "You tied: " << ties << endl;
}
               (
geocities.com/hiddenbunkerlabs)