i am stuck on how to write this ROCK PAPER and SCISSOR program. here is what i have so far.
#include <iostream>
#include<ctime>
#include <cstdlib>
using namespace std;
if (choice=='R')
cout << "Rock"<<endl;
else if ( choice == 'P')
cout << "Paper"<<endl;
else if (choice == 'S')
cout << "Scissors"<<endl;
else
cout <<"invalid choice"<<endl;
}
return choice;
}
int computerChoice()
{
int computerChoice = rand() % 3;
if (computerChoice == 0) <---------- how do i change this letters
{
cout << "Rock"<<endl;
}
else if (computerChoice == 1) <---------- same here
{
cout << "Paper"<<endl;
}
else
{
cout << "Scissors"<<endl; <---------- same here
}
return computerChoice;
}
char choice;
int computerChoice;
while (true)
{
choice = humanChoice();
if ( choice == 'Q') break;
if (computerChoice == humanChoice)
{
cout << "its a tie"<<endl;
}
if (humanChoice == 'R')
{
if ( computerChoice == 'P')
{
cout << "you loose"<<endl;
}
else if ( computerChoice =='P')
{
cout << " you win"<<endl;
}
}
else if ( humanChoice =='S')
{
if (computerChoice =='R')
{
cout << "you lose"<<endl;
}
else if (computerChoice =='R')
{
cout << " you win"<<endl;
}
}
if (humanChoice == 'P')
{
if ( computerChoice == 'S')
{
cout << "you loose"<<endl;
}
else if ( computerChoice =='S')
{
cout << " you win"<<endl;
}
cout << "thanks for playing"<<endl;
}
}
return 0;
}
i dont understand how i should convert the random generated number that computer outputs to corresponding letter such that i can fix up my logic section. could someone please help me out?
assign whatever values you want to the numbers 0-2. you can use an if statement.
but you have computer choice declared as an int, which it needs to be, so you will need another variable to hold the letter value.
1 2 3 4 5 6
if (computerChoice == 0 )
letter = 'R';
elseif (computerChoice == 1 )
letter = 'P';
else
letter = 'S';
also I see that in main you do not yet call computerChoice() function, which makes sense because it does not quite work yet.
EDIT:
or better yet, now that I read your full post
1 2 3 4 5 6 7 8 9 10
char computerChoice()
{
//your random number generator here
if (computerChoice == 0 )
return = 'R';
elseif (computerChoice == 1 )
return = 'P';
elsereturn = 'S';
}
Thanks for the help but i still need more help to finish up this program. i have ran into problem of figuring out my main. i already have done the logic ( not sure if it works but it compiles). could you please help me with the main.
#include <iostream>
#include<ctime>
#include <cstdlib>
using namespace std;
This doesn't belong in the 'Lounge' section. You will have a button to edit the original post, and you can move it to the 'Beginner' section, where you are more likely to receive an answer.
Just use a switch statement. You can pick a different letter for each choice number.
I can't be bothered to read your code, especially when it's not in code tags, but this could be simplified using an enum for rock, paper and scissors and a pre-made array of the result, since there are so few possible results
1 2 3 4 5 6 7 8 9 10
enum RPS
{
rock, // rock is 0
paper, // paper is 1
scissors // scissors is 2
};
std::string names[] = { "rock", "paper", "scissors" };
int results[][] = { { -1, 1, 0 }, { 1, -1, 2 }, { 0, 2, -1 } };
then to find the result of a game just take the value from the array, -1 represents a draw in my solution
1 2 3 4 5 6 7 8
RPS r = rock;
RPS s = scissors;
int winner = results[r][s];
if ( winner == -1 )
// it is a draw
else
// output names[winner];
please note i have learned to use functions, loops and selection structures so far. this program has to be in written with those in mind.
so far i have written the print function, computer choice function and also the human choice function.
This is how i am told my main should look like.
// initialize the computer's random number generator
// declare variables
// start loop
// determine computer's choice
// prompt for, and read, the human's choice
// if human wants to quit, break out of loop
// print results
// end loop
// end program