RSP游戏结果不会显示

时间:2018-08-29 09:41:05

标签: c++

我正在为下一张盘子做石头剪刀布游戏,我认为有些东西我根本无法弄清楚。这是我到目前为止所做的,我所需要的只是获得谁赢得胜利的结果,例如“ PLayer 1胜利”或“这是平局”。可能是循环吗? char初始化也似乎是错误的。请赐教,感谢您的回答! output就是这样。

编辑:

#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;
}

1 个答案:

答案 0 :(得分:0)

将您的方法的开头更改为此

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

您错过了播放器2的输入

由于某些原因,您正在将P1与一个字符进行比较,而P2与一个整数进行比较。

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

应该是

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

等等等