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 143 144 145 146 147 148 149 150 151 152 153
|
//Ashton Dreiling
//Rock paper scissors exercise
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
#include <stdlib.h>
using namespace std;
const int ONE_FOR_CAL=1;
const int THREE_FOR_CAL=3;
const int TWO_FOR_CAL=2;
void switchStatements(int number, string &rockPaperScissorsStatus, string rock, string paper, string scissors);
void playGameAgain (string userInput, string rockPaperScissorsStatus, string rock, string paper, string scissors, int number);
void ifThenStatements(string userInput, string rockPaperScissorsStatus);
void inputValidation (string &userInput);
int main()
{
int number=0;
string rockPaperScissorsStatus;
string rock="rock";
string paper="paper";
string scissors="paper";
string doAgain;
string userInput;
srand(time(0));
cout << "Today, you will be playing rock, paper, scissors against a computer!" << endl;
do
{
number=rand()% THREE_FOR_CAL + ONE_FOR_CAL;
cout <<"Please enter rock, paper, or scissors." << endl;
cin >> userInput;
for(int i = 0; i < userInput.length(); i++)
userInput[i] = tolower(userInput[i]);
inputValidation(userInput);
switchStatements(number, rockPaperScissorsStatus, rock, paper, scissors);
ifThenStatements(userInput, rockPaperScissorsStatus);
if(userInput==rockPaperScissorsStatus)
{
playGameAgain(userInput, rockPaperScissorsStatus, rock, paper, scissors, number);
}
cout << "Would you like to play again?" << endl;
cin >> doAgain;
}
while (doAgain=="yes" || "YES" || "Yes");
system("Pause");
return 0;
}//end main
void switchStatements(int number, string &rockPaperScissorsStatus, string rock, string paper, string scissors)
{
switch(number)
{
case ONE_FOR_CAL:
cout << "The computer has chosen rock." << endl;
rockPaperScissorsStatus="rock";
break;
case TWO_FOR_CAL:
cout << "The computer has chosen paper." << endl;
rockPaperScissorsStatus="paper";
break;
case THREE_FOR_CAL:
cout << "The computer has chosen scissors." << endl;
rockPaperScissorsStatus="scissors";
break;
}//end switch statements
}//end of switchStatements
void ifThenStatements(string userInput, string rockPaperScissorsStatus)
{
if (userInput==rockPaperScissorsStatus)
{
cout << "It's a draw." << endl;
}
else if (userInput=="rock" && rockPaperScissorsStatus=="scissors")
{
cout << "You won! Rock smashes scissors." << endl;
}
else if (userInput=="rock" && rockPaperScissorsStatus=="paper")
{
cout << "You lose. Paper covers rock." << endl;
}
else if (userInput=="scissors" && rockPaperScissorsStatus=="paper")
{
cout << "You win. Scissors cuts paper." << endl;
}
else if (userInput=="scissors" && rockPaperScissorsStatus=="rock")
{
cout << "You lose. Rock crushes scissors." << endl;
}
else if (userInput=="paper" && rockPaperScissorsStatus=="rock")
{
cout << "You win. Paper covers rock." << endl;
}
else if (userInput=="paper" && rockPaperScissorsStatus=="scissors")
{
cout << "You lose. Scissors cuts paper." << endl;
}
}//end ifThenStatements
void playGameAgain(string userInput, string rockPaperScissorsStatus, string rock, string paper, string scissors, int number)
{
do
{
cout << "You must continue to play until it's no longer a draw." << endl;
cout <<"Please enter rock, paper, or scissors." << endl;
cin >> userInput;
switchStatements(number, rockPaperScissorsStatus, rock, paper, scissors);
ifThenStatements(userInput, rockPaperScissorsStatus);
}
while(userInput==rockPaperScissorsStatus);
}
void inputValidation(string &userInput)
{
do
{
if(userInput!="paper" && userInput !="rock" && userInput!="scissors")
{
cout << "Sorry, but you must enter rock, paper, or scissors." << endl;
cin >> userInput;
for(int i = 0; i < userInput.length(); i++)
userInput[i] = tolower(userInput[i]);
}
}
while(userInput!="rock" && userInput!="paper" && userInput!="scissors");
}
|