Like the title says I'm a student, new to C++, I've written this app that plays this flippin rock, paper, scissors game but when you run it you have to type in a number to get anything to work. Where did I go wrong.........? Also not sure on where to install the wins = 0/1/2/3/etc.....
Any help is appreciated, if not delete it and I'll figure it out.......thank you for your time.
#include <cstdlib>
#include <string>
#include <iostream>
#include <ctime>
usingnamespace std;
/*
* main entry point
*/
char readAndValidateUserMove() {
char Result; // to capture our result we have to use this........
do {
cin >> Result; // get input from user
if (Result == '1' || Result == '2' || Result == '3') {
return tolower(Result);
}
cin.clear();
cin.ignore();
} while (true); // if we get correct input, we'll leave this with the return statement
}
//generate the computer's guess by random.........
char generateCompMove() {
int nRandNum = rand() % 3; // 0-2
if (nRandNum == 0)
return'1';
elseif (nRandNum == 1)
return'2';
return'3';
}
//compare moves..............
int CompareMoves(char playerMove, char compMove) {
// 0 - player wins
// 1 - ai wins
// 2 tie
if (playerMove == '1') {
if (compMove == '1')
return 2;
elseif (compMove == '2')
return 1;
elsereturn 0;
} elseif (playerMove == '2') {
if (compMove == '2')
return 2;
elseif (compMove == '1')
return 1;
elsereturn 0;
} elseif (playerMove == '3') {
if (compMove == '3')
return 2;
elseif (compMove == '1')
return 1;
elsereturn 0;
}
return 0; // if we get here, something earlier went wrong
}
int main(int argc, char** argv) {
srand((unsigned) time(0)); // make sure you do this, or rand will return the same result every time
//variables declared
char player, comp;
int result;
string name;
player = readAndValidateUserMove();
comp = generateCompMove();
//prompt for users name
cout << "What is your name?" <<endl;
cin >> name;
//output a hello message
cout << "Hello " << name << "! Lets play rock, paper scissors!" <<endl;
cout << "First there are 3 rules: 1. Rock breaks Scissors || Scissors cuts Paper || Paper wraps Rock; Choose: 1 for Rock, 2 for Paper, 3 for Scissors. Enter your choice as a number 1, 2 or 3" <<endl;
cout << "Please enter your selection now!" <<endl;
cout << "Computer plays " <<endl;
cin >> comp;
result = CompareMoves(player, comp);
if (result == 0)
cout << "You win! Would you like to play again?";
elseif (result == 1)
cout << "You lost. Would you like to play again?";
else
cout << "Its a Tie. Would you like to play again?";
return 0;
}
You be better putting the game part of your code into a loop i.e.
1 2 3 4 5 6 7
while(userInput != "N")
{
//Game code
cout << "would you like to play again??";
cin >> userInput;
}
That way you could implement a system where wach time the player wins you increment the players counter and when the computer wins increase it's counter etc...
I have made a few changes:
*changed some things in the main method
**moved the calls to readAndValidateUserMove() and generateCompMove();
**added a loop around everything to keep it going if the user enters Y or y
**added an if-else if-else block to output the choice the computer made in text form
*changed the compareMoves method to make it more legible
**noticed a bug when playerMove=='2' and compMove=='1' (it made the wrong person win)
*changed readAndValidateUserMove method by moving the cin.clear() and cin.ignore()
*also changed generateCompMove method just to get rid of some if statements
#include <cstdlib>
#include <string>
#include <iostream>
#include <ctime>
usingnamespace std;
char readAndValidateUserMove()
{
char Result; // to capture our result we have to use this........
do {
cin >> Result; // get input from user
cin.clear();
cin.ignore();
if (Result == '1' || Result == '2' || Result == '3')
return tolower(Result);
} while (true); // if we get correct input, we'll leave this with the return statement
}
//generate the computer's guess by random.........
char generateCompMove()
{
return 48 + rand() % 3 + 1; // returns 1 to 3 in ASCII character form
}
//compare moves..............
int CompareMoves(char playerMove, char compMove)
{
// 0 - player wins
// 1 - ai wins
// 2 tie
if (playerMove == compMove)
return 2;
elseif (playerMove == '1')
{
if (compMove == '2')
return 1;
elsereturn 0;
}
elseif (playerMove == '2')
{
if (compMove == '1')
return 0;
elsereturn 1;
}
elseif (playerMove == '3')
{
if (compMove == '1')
return 1;
elsereturn 0;
}
return 0; // if we get here, something earlier went wrong
}
/*
* main entry point
*/
int main(int argc, char** argv)
{
srand((unsigned) time(0)); // make sure you do this, or rand will return the same result every time
//variables declared
char player, comp, choice;
int result;
string name;
do
{
//prompt for users name
cout << "What is your name?" << endl;
cin >> name;
//output a hello message
cout << endl << "Hello " << name << "! Lets play rock, paper scissors!" << endl;
//output rules message
cout << "First there are 3 rules: 1. Rock breaks Scissors || Scissors cuts Paper || Paper wraps Rock"
<< endl << endl << "Now Choose: 1 for Rock, 2 for Paper, 3 for Scissors. Enter your choice as a number 1, 2 or 3" << endl;
//prompt for an integer between 1 and 3
cout << "Please enter your selection now!" << endl;
player = readAndValidateUserMove(); //this function gets legal user input between 1 and 3 and stores to player
cout << endl << "Computer plays ";
comp = generateCompMove();
if (comp == '1')
cout << "Rock";
elseif (comp == '2')
cout << "Paper";
else
cout << "Scissors";
cout << endl << endl;
result = CompareMoves(player, comp);
if (result == 0)
cout << "You win!";
elseif (result == 1)
cout << "You lost.";
else
cout << "Its a Tie.";
cout << endl << endl << "Would you like to play again (Y/N)?";
cin >> choice;
} while(tolower(choice) == 'y');
return 0;
}
Wow, lessons learned..............thank you very much for the help, I had some things messed up for sure. Thank you for the help. Obviously I had things in the wrong spots already, but miraculously it worked, hahahaha.
Where would I ad the wins total stuff within this program.............
Awesome, thank you very much, I appreciate the help. It looked more complicated for me and staring at this code for a couple of hours I was beat with it.