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 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
|
//This program is a Rock, Paper, Scissor game. It will ask you if you want to
//play the game, and if yes, will have you make a choice and determine the
//winner and keep score until the user quits.
#include <iostream>
#include <cstdlib>
#include <ctype.h>
#include <time.h>
using namespace std;
//*******************************************************
//Definition of function displayMenu *
//This function displays the menu choices. *
//*******************************************************
void displayMenu()
{
int userChoice;
cout << "1) Paper\n";
cout << "2) Rock\n";
cout << "3) Scissors\n";
cout << "Please make your choice(1-3):\n";
}
//*******************************************************
//Definition of function displayWinner *
//This function is used to determine the winner. *
//*******************************************************
void displayWinner (int)
{
int win, lose, tie, compChoice, userChoice;
compChoice = rand() % 3 + 1;
if (userChoice == 1 && compChoice == 2)
{
cout << "You have chosen paper and the computer has chosen rock. You win!\n";
win++;
}
else if (userChoice == 2 && compChoice == 3)
{
cout << "You have chosen rock and the computer has chosen scissors. You win!\n";
win++;
}
else if (userChoice == 3 && compChoice == 1)
{
cout << "You have chosen scissors and the computer has chosen paper. You win!\n";
win++;
}
else if (userChoice == 1 && compChoice == 3)
{
cout << "You have chosen paper and the computer has chosen scissors. You lose!\n";
lose++;
}
else if (userChoice == 2 && compChoice == 1)
{
cout << "You have chosen rock and the computer has chosen paper. You lose!\n";
lose++;
}
else if (userChoice == 3 && compChoice == 2)
{
cout << "You have chosen scissors and the computer has chosen rock. You lose!\n";
lose++;
}
else if (userChoice == 1 && compChoice == 1)
{
cout << "You have chosen paper and the computer has chosen paper. You tied!\n";
tie++;
}
else if (userChoice == 2 && compChoice == 2)
{
cout << "You have chosen rock and the computer has chosen rock. You tied!\n";
tie++;
}
else if (userChoice == 3 && compChoice == 3)
{
cout << "You have chosen scissors and the computer has chosen scissors. You tied!\n";
tie++;
}
}
//*******************************************************
//Definition of function displayEnd *
//This fuction is used to store stats *
//*******************************************************
void displayEnd (int, int, int)
{
int win, lose, tie, numGames, percent;
percent = win / numGames;
numGames = win + lose + tie;
cout << "So far your have played " << numGames << " sessions and have won " << win << ", tied " << tie << ", and lost " << lose << " for a winning percent of " << percent << "%.\n";
}
//******************************************************
//Main Function *
//******************************************************
int main ()
{
int win, lose, tie, numGames, percent, userChoice, compChoice;
win = 0;
lose = 0;
tie = 0;
char choice;
{main:
cout << "Welcome! Want to play a game of Paper Rock Scissors?\n";
cout << "Press 1 to play, and 2 to quit.\n";
cin >> choice;
switch (choice)
{
case '1':
displayMenu();
cin >> userChoice;
displayWinner(userChoice);
break;
case '2':
displayEnd(win, lose, tie);
break;
default:
goto main;
}
}
return 0;
}
|