// Add necessary includes
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
using namespace std;
// This is a simple game of "Rock, Paper, Scissors"
// It is also my first project ever in C++.
// Declare functions
void playGame();
bool playAgain();
string letUserChoose();
int genRandNum();
string letComputerChoose(int randomNumber, string computerChoice);
void compareChoices(string userChoice, string computerChoice);
bool playAgain() {
string stayOrGo;
cout << "Type 'y' to play again, 'n' to quit.\n" << endl;
cin >> stayOrGo;
return stayOrGo == "y";
}
int genRandNum() {
// Generate a pseudo-random number between 1 and 3
int randomNumber;
srand(time(0));
randomNumber = rand() % 3 + 1;
return randomNumber;
}
string letUserChoose() {
string userChoice;
// Let the user choose either rock, paper or scissors
cout << "\n\nPlease use only lower-case letters!" << endl;
cout << "\n\nDo you choose 'rock', 'paper' or 'scissors'?\n" << endl;
cin >> userChoice;
cout << "\nYou have chosen: " << userChoice << endl;
return userChoice;
}
string letComputerChoose(int randomNumber, string computerChoice) {
// 1 = Rock, 2 = Paper, 3 = Scissors, assign a choice to the computer.
if (randomNumber == 1) {
computerChoice = "rock";
}
else if (randomNumber == 2) {
computerChoice = "paper";
}
else {
computerChoice = "scissors";
}
cout << "\nThe computer chose: " << computerChoice << endl;
return computerChoice;
}
void compareChoices(string userChoice, string computerChoice) {
// Decide who wins by comparing the userChoice and computerChoice
// Choose what happens if userChoice is rock
if (userChoice == "rock") {
if (computerChoice == "paper") {
cout << "\nPaper beats rock, you lose!\n\n";
}
else if (computerChoice == "scissors") {
cout << "\nRock beats scissors, you win!\n\n";
}
else {
cout << "\nIt's a tie!\n\n";
}
}
// Choose what happens if userChoice is paper
if (userChoice == "paper") {
if (computerChoice == "scissors") {
cout << "\nScissors beats paper, you lose!\n\n";
}
else if (computerChoice == "rock") {
cout << "\nPaper beats rock, you win!\n\n";
}
else {
cout << "\nIt's a tie!\n\n";
}
}
// Choose what happens if userChoice is scissors
if (userChoice == "scissors") {
if (computerChoice == "rock") {
cout << "\nRock beats scissors, you lose!\n\n";
}
else if (computerChoice == "paper") {
cout << "\nScissors beats paper, you win!\n\n";
}
else {
cout << "\nIt's a tie!\n\n";
}
}
}
void playGame(string userChoice, int randomNumber, string computerChoice) {
letUserChoose();
genRandNum();
letComputerChoose(randomNumber, computerChoice);
compareChoices(userChoice, computerChoice);
}
int main(int nNumberofArgs, char* pszArgs[]) {
// Initialize variables
string userChoice = letUserChoose();
int randomNumber = genRandNum();
string computerChoice = letComputerChoose(randomNumber, computerChoice);
// Main loop of the game
do {
playGame(userChoice, randomNumber, computerChoice);
} while (playAgain());
// Let the player know that he or she has chosen to exit the game
cout << "\nYou have chosen to exit the game.\n\n";
system("PAUSE");
return 0;
}
Comments