Rock-paper-scissors C++
Aug 11, 2014 at 3:21pm UTC
How I can change input from R,P,S to Rock, Paper, Scissors
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
#include <iostream>
using namespace std;
int main()
{
int game;
char player1, player2;
cin >> game;
for (int i = 0;i<game;i++){
cin >> player1 >> player2;
switch (player1){
case 'R' :
if (player2 == 'R' )
cout << "draw" << endl;
if (player2 == 'P' )
cout << "player 2 win" << endl;
if (player2 == 'S' )
cout << "player 1 win" << endl;
break ;
case 'P' :
if (player2 == 'R' )
cout << "player 1 win" << endl;
if (player2 == 'P' )
cout << "draw" << endl;
if (player2 == 'S' )
cout << "player 2 win" << endl;
break ;
case 'S' :
if (player2 == 'R' )
cout << "player 2 win" << endl;
if (player2 == 'P' )
cout << "player 1 win" << endl;
if (player2 == 'S' )
cout << "draw" << endl;
break ;
default :
cout << "Invalid Entry" ;
return 0;
}
}
return 0;
}
Aug 11, 2014 at 3:28pm UTC
SImply change player1 and player2 from char to string.
Keep in mind that once you do that, you will no longer be able to use a switch statement.
Aug 11, 2014 at 3:38pm UTC
Thank you :D
Aug 11, 2014 at 3:42pm UTC
Simply change player1 and player2 from char to string.
Unless they've changed something in C++11, that won't work because the switch() statement requires an integral type. So you would have to change the switch statement to an if/then/else.
Another way to do this is with more data. Create an enum with values Rock, Paper and Scissors. Then create a 2D array so that arr[player1][player2] gives the outcome of a round. Here, player1 and player2 are enum values. The value of the array might be 1, 2, or 0 to indicate player 1 wins, player 2 wins, or nobody wins.
Aug 11, 2014 at 5:29pm UTC
@Mineanthat
If you would still like to use the switch statement, you could write the program this way..
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
#include <iostream>
#include <string>
using namespace std;
int main()
{
int game;
char player1, player2;
string p[2];
cout << "How many games of Rock, Paper Scissors, do you wish to play?" << endl;
cin >> game;
for (int i = 0;i<game;i++){
cout << "Enter your choice, Rock, Paper or Scissor" << endl;
cin >> p[0] >> p[1];
player1=toupper(p[0][0]); // Gets first letter of Player 1 choice, and capitalizes it
player2=toupper(p[1][0]); // same for player 2
switch (player1){
case 'R' :
if (player2 == 'R' )
cout << "draw" << endl;
if (player2 == 'P' )
cout << "player 2 win" << endl;
if (player2 == 'S' )
cout << "player 1 win" << endl;
break ;
case 'P' :
if (player2 == 'R' )
cout << "player 1 win" << endl;
if (player2 == 'P' )
cout << "draw" << endl;
if (player2 == 'S' )
cout << "player 2 win" << endl;
break ;
case 'S' :
if (player2 == 'R' )
cout << "player 2 win" << endl;
if (player2 == 'P' )
cout << "player 1 win" << endl;
if (player2 == 'S' )
cout << "draw" << endl;
break ;
default :
cout << "Invalid Entry" ;
return 0;
}
}
return 0;
}
Last edited on Aug 11, 2014 at 5:30pm UTC
Topic archived. No new replies allowed.