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 154 155 156 157 158 159 160 161 162 163
|
//Ashton Dreiling
//Rock paper scissors exercise
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
#include <stdlib.h>
using namespace std;
//global constants
const int ONE_FOR_CAL=1;
const int THREE_FOR_CAL=3;
const int TWO_FOR_CAL=2;
//function prototypes
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()
{
//some variables
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 for switch statements
number=rand()% THREE_FOR_CAL + ONE_FOR_CAL;
cout <<"Please enter rock, paper, or scissors." << endl;
cin >> userInput;
//to convert what user types in first to lower case
for(int i = 0; i < userInput.length(); i++)
userInput[i] = tolower(userInput[i]);
//function to validate if user types rock paper scissors
inputValidation(userInput);
//function to set randomly generated numbers 1-3 t strings
switchStatements(number, rockPaperScissorsStatus, rock, paper, scissors);
//function to determine who is the winner
ifThenStatements(userInput, rockPaperScissorsStatus);
if(userInput==rockPaperScissorsStatus)
{
playGameAgain(userInput, rockPaperScissorsStatus, rock, paper, scissors, number);
}//if then statements to determine if game is played again based off of input
cout << "Would you like to play again?" << endl;
cin >> doAgain;
}//end of do while loop to determine if user plays again
while (doAgain=="yes" || doAgain=="YES" || doAgain=="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
}//end of switchStatements
void ifThenStatements(string userInput, string rockPaperScissorsStatus)
{
if (userInput==rockPaperScissorsStatus)
{
cout << "It's a draw." << endl;
}//end if then statement
else if (userInput=="rock" && rockPaperScissorsStatus=="scissors")
{
cout << "You won! Rock smashes scissors." << endl;
}// end else if statement
else if (userInput=="rock" && rockPaperScissorsStatus=="paper")
{
cout << "You lose. Paper covers rock." << endl;
}// end else if statement
else if (userInput=="scissors" && rockPaperScissorsStatus=="paper")
{
cout << "You win. Scissors cuts paper." << endl;
}//end else if statement
else if (userInput=="scissors" && rockPaperScissorsStatus=="rock")
{
cout << "You lose. Rock crushes scissors." << endl;
}//end else if statement
else if (userInput=="paper" && rockPaperScissorsStatus=="rock")
{
cout << "You win. Paper covers rock." << endl;
}//end else if statement
else if (userInput=="paper" && rockPaperScissorsStatus=="scissors")
{
cout << "You lose. Scissors cuts paper." << endl;
}//end else if statement
}//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);
}//end do while loop to determine if user is entering rock paper or scissors
while(userInput==rockPaperScissorsStatus);
}//end playGameAgain
void inputValidation(string &userInput)
{
while(userInput!="paper" && userInput !="rock" && userInput!="scissors")
{
cout << "Sorry, but you must enter rock, paper, or scissors." << endl;
cin >> userInput;
//convert user input to all lower case
for(int i = 0; i < userInput.length(); i++)
userInput[i] = tolower(userInput[i]);
}//end while loop inputValidation
}// end inputvalidation
|