Aug 29, 2018 at 9:03am UTC
I know something's missing but I can't figure it out. it's supposed to be R = 0, P = 1, S = 0 and P2 is a computer opponent that inputs a randomized value. Here's what I've done so far, all I need is to get the result on who wins to show up.
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
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <Windows.h>
using namespace std;
class Game
{
private : int rndNum, result;
char P1, P2, repeat;
const char R = 'R' , S = 'S' , P = 'P' ;
public : void RSPLogic();
};
void Game::RSPLogic()
{
do {
cout << "Input choice of player 1 : " ;
cin >> P1;
cout << "Input choice of player 2 : " ;
srand(time(0));
rndNum = rand() % 3 + 1;
if (rndNum == 0) //Computer rock
{
result = 0;
cout << "R\n" ;
}
else if (rndNum == 1) //Computer scissors
{
result = 1;
cout << "P\n" ;
}
else if (rndNum == 2) // Computer paper
{
result = 2;
cout << "S\n" ;
}
//Player 1 Win
if ((P1 == 'R' && P2 == 1) || (P1 == 'S' && P2 == 2) || (P1 = 'P' && P2 == 0))
cout << endl << "Player 1 Wins!" << endl << endl;
//Player 2 Win
else if ((P1 == 'R' && P2 == 2) || (P1 == 'S' && P2 == 0) || (P1 == 'P' && P2 == 1))
cout << endl << "Player 2 Wins!" << endl << endl;
//Tie
else if ((P1 == 'R' && P2 == 0) || (P1 == 'S' && P2 == 1) || (P1 == 'P' && P2 == 2))
cout << endl << " It's a Tie!" << endl << endl;
cout << endl << "Press [Y/y] to continue." << endl;
cin >> repeat;
repeat = toupper(repeat);
}
while (repeat == 'Y' );
}
int main()
{
Game obj;
obj.RSPLogic();
system("pause" );
return 0;
}
Last edited on Aug 29, 2018 at 10:32am UTC
Aug 29, 2018 at 9:51am UTC
srand() should be called once at the start of the program.
rand() % 3 returns a number in { 0, 1, 2 }. By adding 1, you move the values to { 1, 2, 3 }, but you don't check for those.
You're comparing P1 and P2, but you never set P2. P2's value is in rndNum.
Aug 29, 2018 at 10:12am UTC
@kbw I totally forgot stdlib.h, my bad. I just replaced 'results' with 'P2' and deleted +1, it's working now thanks for your help!
Last edited on Aug 29, 2018 at 10:48am UTC