Hey guys, so I'm having a bit of trouble with my homework and was hoping someone here would be able to help me. My win, lose, tie functions are giving me a bit of trouble and I'm having a hard time figuring out what is wrong. No matter what I do it will print out that I win, lose, and tie, which of course is not correct. These are the teacher's instructions for the assignment.
INSTRUCTIONS:
If the user selects 'p':
1. First the program should call a function named getComputerChoice to get the computer's choice in the game. The getComputerChoice function should generate a random number between 1 and 3. If the random number is 1 the computer has chosen Rock, if the random number is 2 the user has chosen Paper, and if the random number is 3 the computer has chosen Scissors. (Hint: return an int from this function and make the function have no arguments). This function is REQUIRED to be a value returning function (do NOT make a void function).
2. Next the program should call a function named getPlayerChoice to get the user's (player's) choice in the game. Have the user's choice be represented by a number just like the computer's was. The getPlayerChoice function should display a menu similar to the one below and then get a 1, 2, or 3 as the user's choice for Rock, Paper or Scissors. Do not allow the user to enter an invalid choice - they must enter 1, 2, or 3 (use integers for the menu on this instead of chars). (Hint: return an int from this function and make the function have no arguments). This function is REQUIRED to be a value returning function (do NOT make a void function).
Rock, Paper or Scissors?
1) Rock
2) Paper
3) Scissors
Please enter your choice:
3. Next the program should display what the user chose and what the computer chose. You will need to convert both the user's and the computer's integer choices of 1, 2, or 3 to the corresponding strings: Rock, Paper, or Scissors. The display should be similar to the following (in the example below the user chose 3 for Scissors and the computer chose 2 for Paper)
You chose: Scissors
The computer chose: Paper
4. Next the program should call a function named isTie (Hint: have the function return a bool and take two arguments) to see if the game was a tie. This function is REQUIRED to be a value returning function and must take TWO arguments (do NOT make a void function). The game is a tie if both the computer and user made the same choice. If the game was a tie display the appropriate message. An example of a tie is shown below with the appropriate message on the third line.
You choose: Scissors
The computer chose: Scissors
It's a TIE!
5. If the game was NOT a tie, the program should next determine if the player (user) has won the game. You should use a function called isPlayerWinnerto do this, the function should take two arguments (the player choice and the computer choice and return true if the player is a winner and false if the player is not a winner). This function is REQUIRED to be a value returning function (do NOT make a void function) The function is required to take two arguments.
After calling the function, based on the result of it, if the player has won the program should display a message similar to the one below telling the user they won. In the example below the user's winning choice is Rock and the computer's losing choice is Scissors. (Remember the rules for winning and losing are at the top of the page)
You choose: Rock
The computer chose: Scissors
You WIN!
6. If the game was NOT a tie and the user (player) did NOT win this means the computer won and the program should display a message saying the user has lost. In the example below the computer's winning choice is Paper (the user's losing choice is Rock). Do not make this complicated, if it was not a tie and the player did not win the computer won.
You choose: Rock
The computer chose: Paper
Sorry you LOSE.
If the user selects q: Quit the program
If the user selects anything other than p or q: Display an error message.
The program should keep re-displaying the menu and let the user play as many games against the computer as they would like.
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 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
|
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
using namespace std;
int getComputerChoice(); /* FUNCTION PROTOTYPES ARE LISTED HERE */
int getPlayerChoice();
bool isTie(int, int);
bool isPlayerWinner(int, int); /* END OF FUNCTION PROTOTYPES */
int main()
{
char userChoice; /* VARIABLES ARE DEFINED HERE*/
int playerChoice = 0;
int computerChoice = 0;
int myChoice = 0;
int randomCompNum = 0;
bool hasWon; /* END OF VARIABLE DEFINING */
do
{
cout << "\n"
<< "ROCK PAPER SCISSORS MENU\n"
<< "------------------------\n"
<< "p) Play Game\n"
<< "q) Quit\n"
<< "Please enter your choice : \n\n";
cin >> userChoice;
if (userChoice == 'p')
{
playerChoice = getPlayerChoice();
computerChoice = getComputerChoice();
hasWon = (isPlayerWinner(myChoice, randomCompNum) == true);
{
cout << "You WIN!\n";
}
hasWon = (isPlayerWinner(myChoice, randomCompNum) == false);
{
cout << "Sorry you LOSE.\n";
}
if (isTie(myChoice, randomCompNum) == true)
{
cout << "It's a TIE!\n\n\n";
}
else
{
return false;
cout << "\n";
}
}
else if (userChoice == 'q')
{
cout << "You have chosen to quit the program. Thank you for using the program!\n";
}
else if (userChoice != 'q' || userChoice != 'p')
{
cout << "Invalid selection. Try again.\n\n";
}
}
while (userChoice != 'q');
system("PAUSE");
return 0;
}
int getComputerChoice()
{
srand((unsigned int)time(NULL));
int randomCompNum = rand() % 3 + 1;
if (randomCompNum == 1)
{
cout << "The computer chose : Rock\n";
}
else if (randomCompNum == 2)
{
cout << "The computer chose : Paper\n";
}
else if (randomCompNum == 3)
{
cout << "The computer chose : Scissors\n";
}
return randomCompNum;
}
int getPlayerChoice()
{
int myChoice;
cout << "\n\nRock, Paper, or Scissors?\n"
<< "1) Rock\n"
<< "2) Paper\n"
<< "3) Scissors\n"
<< "Please enter your choice : \n";
cin >> myChoice;
if (myChoice == 1)
{
cout << "You chose : Rock\n";
}
else if (myChoice == 2)
{
cout << "You chose : Paper\n";
}
else if (myChoice == 3)
{
cout << "You chose : Scissors\n";
}
return myChoice;
while (myChoice < 1 || myChoice > 3)
{
cout << "Please pick a number between 1 & 3.\n";
cin >> myChoice;
}
}
bool isPlayerWinner(int myChoice, int randomCompNum)
{
if ((myChoice == 1) && (randomCompNum == 3))
{
return true;
}
else if ((myChoice == 3) && (randomCompNum == 2))
{
return true;
}
else if ((myChoice == 2) && (randomCompNum == 1))
{
return true;
}
else if ((randomCompNum == 3) && (myChoice == 1))
{
return false;
}
else if ((randomCompNum == 3) && (myChoice == 2))
{
return false;
}
else if ((randomCompNum == 2) && (myChoice == 1))
{
return false;
}
}
bool isTie(int myChoice, int randomCompNum)
{
if (myChoice == randomCompNum)
{
return true;
}
else if (myChoice != randomCompNum)
{
return false;
}
}
|