Download:Rock Paper Scissors/Source
From LizardWiki, FastLizard4's wiki and website
This is the source code for the rudimentary rock paper scissors program. Return to the description page.
-
/* PROGRAM WRITTEN BY FastLizard4 (http://lizardwiki.dyndns.org)
-
Licensed under the GNU General Public License. */
-
#include <iostream>
-
#include <cstdlib>
-
#include <ctime>
-
using namespace std;
-
-
-
int main() {
-
char proceed = 'n', humanPlayerInput = 'x';
-
bool invalid = false;
-
int randLowest = 1, randHighest = 3, computerPlayerInput = 0;
-
int randRange = (randHighest - randLowest) + 1;
-
int statsWin = 0, statsLoss = 0, statsDraw = 0;
-
srand((unsigned)time(0));
-
-
do {
-
do {
-
invalid = false;
-
-
cout << "Please enter your choice, Rock, Paper, or Scissors (R/P/S): ";
-
cin >> humanPlayerInput;
-
-
cout << "The computer is now choosing Rock, Paper or Scissors...\n";
-
computerPlayerInput = randLowest + int(randRange * rand() / (RAND_MAX + 1.0));
-
-
switch(humanPlayerInput) {
-
/*
-
NOTE:
-
FOR THE COMPUTER, 1 IS ROCK, 2 IS PAPER, AND 3 IS SCISSORS
-
*/
-
case 'r':
-
case 'R':
-
switch(computerPlayerInput) {
-
case 1:
-
cout << "The computer chose Rock.\n"
-
<< "Nobody wins.\n";
-
statsDraw++;
-
break;
-
case 2:
-
cout << "The computer chose Paper.\n"
-
<< "Paper covers rock; computer wins.\n";
-
statsLoss++;
-
break;
-
case 3:
-
cout << "The computer chose Scissors.\n"
-
<< "Rock breaks scissors; you win!\n";
-
statsWin++;
-
}
-
break;
-
case 'p':
-
case 'P':
-
switch(computerPlayerInput) {
-
case 1:
-
cout << "The computer chose Rock.\n"
-
<< "Paper covers rock; you win!\n";
-
statsWin++;
-
break;
-
case 2:
-
cout << "The computer chose Paper.\n"
-
<< "Nobody wins.\n";
-
statsDraw++;
-
break;
-
case 3:
-
cout << "The computer chose Scissors.\n"
-
<< "Scissors cut paper; computer wins.\n";
-
statsLoss++;
-
}
-
break;
-
case 's':
-
case 'S':
-
switch(computerPlayerInput) {
-
case 1:
-
cout << "The computer chose Rock.\n"
-
<< "Rock breaks scissors; computer wins.\n";
-
break;
-
case 2:
-
cout << "The computer chose Paper.\n"
-
<< "Scissors cut paper; you win!\n";
-
break;
-
case 3:
-
cout << "The computer chose Scissors.\n"
-
<< "Nobody wins.\n";
-
}
-
break;
-
default:
-
invalid = true;
-
cout << "ERROR: You did not make a valid choice. Please try again.\n";
-
}
-
} while(invalid);
-
-
cout << "Play another round? (y/n) ";
-
cin >> proceed;
-
while(proceed != 'y' && proceed != 'Y' && proceed != 'n' && proceed != 'N') {
-
cout << "Please enter 'y' for yes or 'n' for no. Play another round? (y/n) ";
-
cin >> proceed;
-
}
-
} while(proceed == 'y' || proceed == 'Y');
-
-
return 0; //Indicate successful program execution to the O.S.
-
}