首页 > 解决方案 > RSP 游戏结果不会显示

问题描述

我正在为我的下一个盘子做一个石头剪刀布游戏,我认为缺少一些东西,我只是不知道是什么。这就是我到目前为止所做的,我所需要的只是让结果显示谁赢了,比如“玩家 1 赢”或“平局”。会不会是循环?char 初始化似乎也是错误的。请赐教,我很感激任何答案!这就是输出显示的内容。

编辑:

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

标签: c++

解决方案


将方法的开始更改为此肯定会有所帮助

    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') ...

等等等等


推荐阅读