-
-
Save XMB/af424b7c37c8bec651b0a17fa032d4e8 to your computer and use it in GitHub Desktop.
Rock paper scissors game
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <string> | |
using namespace std; | |
// Main program function | |
int main() | |
{ | |
// Players choice of item | |
char p1Choice; | |
char p2Choice; | |
char playerStartChoice; | |
while (true) | |
{ | |
cout << "~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~" << endl; | |
// Basic greeting to program | |
cout << "Welcome to Rock, Paper, Scissors" << endl; | |
cout << "~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~" << endl; | |
// Message requesting player 1 to input their choice | |
cout << "Player 1, Enter R, P or S" << endl; | |
// Player 1 inputs choice | |
cin >> p1Choice; | |
cout << string(100, '\n'); | |
// Message requesting player 2 to input their choice | |
cout << "Player 2, Enter R, P or S" << endl; | |
// Player 2 inputs choice | |
cin >> p2Choice; | |
// Both players enter the same choice, results in a draw | |
if (p1Choice == p2Choice) | |
cout << "Draw, Try again" << endl << endl; | |
else | |
{ | |
// Player 2's Paper beats Player 1's Rock | |
if (p1Choice == 'R' && p2Choice == 'P') | |
cout << "Player 2 wins!" << endl << endl; | |
// Player 1's Paper beats Player 2's Rock | |
else if (p2Choice == 'R' && p1Choice == 'P') | |
cout << "Player 1 wins!" << endl << endl; | |
// Player 1's Rock beats Player 2's Scissors | |
else if (p1Choice == 'R' && p2Choice == 'S') | |
cout << "Player 1 wins!" << endl << endl; | |
// Player 2's Rock beats Player 1's Scissors | |
else if (p2Choice == 'R' && p1Choice == 'S') | |
cout << "Player 2 wins!" << endl << endl; | |
// Player 1's Scissors beats Player 2's Paper | |
else if (p1Choice == 'S' && p2Choice == 'P') | |
cout << "Player 1 wins!" << endl << endl; | |
// Player 2's Scissors beats Player 1's Paper | |
else if (p2Choice == 'S' && p1Choice == 'P') | |
cout << "Player 2 wins!" << endl << endl; | |
// Gives the players the option the end the script | |
cout << "Do you want to play again?" << endl; | |
cin >> playerStartChoice; | |
// Conditions for break; (ending the code) | |
// != (not equals) | |
if (playerStartChoice != 'Y') | |
{ | |
cout << "Thank you for playing!" << endl; | |
break; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment