-1

I'm doing a Rock Paper Scissors game for my next plate, I think there's something missing I just can't figure out what. Here's what I've done so far, all I need is to get the result on who wins to show up like "PLayer 1 Wins" or "It's a tie". Could it be the looping? The char initialization also seems wrong. Please enlighten me, I'm grateful for any answers! This is what the output shows.

Edited:

#include <iostream> 
#include <cstdlib>
#include <ctime>
#include <stdlib.h>
#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;

        if (rndNum == 0) //Computer rock
        {
            P2 = 0;
            cout << "R\n";
        }

        else if (rndNum == 1) //Computer scissors
        {
            P2 = 1;
            cout << "P\n";
        }
        else if (rndNum == 2)  // Computer paper
        {
            P2 = 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;
}
applj
  • 1
  • 1
  • Please note that "why is my code not working" is offtopic for SO. Please see https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems how you may solve your problem – hellow Aug 29 '18 at 09:42
  • 2
    The reason is, that none of your if/else branches do match. There is a logical error somewhere in your expression. Try to find it with a debugger ;) – hellow Aug 29 '18 at 09:44

1 Answers1

0

It would certainly help to change the start of your method to this

    cout << "Input choice of player 1 : ";
    cin >> P1;
    cout << "Input choice of player 2 : ";
    cin >> P2;

You missed out the player 2 input

Also for some reason you are comparing P1 with a character but P2 with an integer.

else if ((P1 == 'R' && P2 == 0) ...

should be

else if ((P1 == 'R' && P2 == 'R') ...

etc. etc

john
  • 85,011
  • 4
  • 57
  • 81
  • I intended to leave out cin >> P2 since it's a computer opponent that is supposed to randomize its input, hence the srand(). To clarify, R should equate to 0, P = 1 and 2 = S, so I made P2 an integer since the randomizer results to an integer. Sorry I'm finding it hard to explain – applj Aug 29 '18 at 10:25
  • @applj Well then the problem is that all your tests use `P2` but the variable that has the value you want to test is `result`. So it should be `else if ((P1 == 'R' && result == 0) ...` etc. etc. – john Aug 29 '18 at 10:28
  • @applj Also `rndNum = rand() % 3 + 1;` gives you a number from 1 to 3, but your `if (rndNum ==` tests use numbers 0 to 2. You've got to be a bit more careful, the computer will do exactly what you say, not what you meant to say. – john Aug 29 '18 at 10:30
  • @applj Or maybe your code should say `P2 = 0;` instead of `result = 0;` etc. There's just a lack of consistency in your code. Decide on the variables you want to use and **delete** the rest. That will help. – john Aug 29 '18 at 10:33
  • I got it! I just changed 'results' to 'P2' and deleted '+1'. Thanks so much! – applj Aug 29 '18 at 10:39