Skip to content

Instantly share code, notes, and snippets.

@thomaswrenn
Last active December 13, 2015 17:49
Show Gist options
  • Save thomaswrenn/4951030 to your computer and use it in GitHub Desktop.
Save thomaswrenn/4951030 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
int main ()
{
unsigned seed;
seed = time( NULL );
srand( seed );
cout << "Welcome to the game of Rock, Paper, Scissors";
cout << endl;
int p1wins = 0;
int p2wins = 0;
while ( true )
{
string input;
int p1, p2;
cout << "\n( type 'end' to stop ) \nTo Play, please enter 0 for Rock, 1 for Paper, or 2 for Scissors : ";
cin >> input;
if (input == "end") break;
else p1 = atoi(input.c_str()); // stupid C++ not making a simple syntax for string to int casting.
if ( p1 < 3 && p1 >= 0 ) {
p2 = ( rand() % 3 );
cout << "Player 2 choses " << ( (p2 == 0) ? "ROCK" : ( (p2 == 1) ? "PAPER" : "SCISSORS" ) ) << endl;
int pairState = p1 + p2;
if ( p1 == p2 ) {
cout << "It's a Tie!" << endl;
} else {
bool winner = false;
if ( pairState == 1 ) {
cout << "PAPER covers ROCK";
winner = (p1 == 1);
} else if ( pairState == 2 ) {
cout << "ROCK crushes SCISSORS";
winner = (p1 == 0);
} else if ( pairState == 3 ) {
cout << "SCISSORS cuts PAPER";
winner = (p1 == 2);
}
cout << " - " << "Player " << (winner?"1":"2") << " wins" << (winner?"":"!") << endl;
if (winner) p1wins++;
else p2wins++;
}
cout << ( (p1wins>p2wins)?p1wins:p2wins ) << "-" << ( (p1wins<p2wins)?p1wins:p2wins ) << " " ;
if (p1wins == p2wins) cout << "TIED" << endl; else cout << "Player " << ((p1wins>p2wins)?"1":"2") << endl;
} else cout << "ERROR: Bad input, try again." << endl;
}
cout << "Thank you for playing! have a nice day!";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment