回复计划游戏结果不会显示

RSP Game results won't show up

本文关键字:显示 结果 计划 游戏 回复      更新时间:2023-10-16

我正在为我的下一个盘子做一个剪刀石头布游戏,我想缺少一些东西,我只是想不通。这是我到目前为止所做的,我所需要的只是得到谁赢的结果,比如"PLayer 1 Wins"或"这是一个平局"。会不会是循环?字符初始化似乎也是错误的。 请启发我,我很感激任何答案!这是输出显示的内容。

编辑:

#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 << "Rn";
}
else if (rndNum == 1) //Computer scissors
{
P2 = 1;
cout << "Pn";
}
else if (rndNum == 2)  // Computer paper
{
P2 = 2;
cout << "Sn";
}
//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;
}

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

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

等等等等