c++ - 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;
}
解决方案
将方法的开始更改为此肯定会有所帮助
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') ...
等等等等
推荐阅读
- continuous-integration - TeamCity 更改模板参数而不在子级中更改它
- angular - Microsoft Edge 中的 Ngx-Editor 不保存格式
- java - 使用 Log4J 进行分类记录
- django - Heroku:部署 Django Rest 框架 + Vue
- android - 我应该为每个 http 连接使用一个加载器还是一个加载器就足够了?
- javascript - 如何为jsGrid中的每一行绑定动态选择选项?
- c# - 在 DataGridView 中显示 BindingList<(int, int, int)>
- cmake - CMake 在生成阶段只生成一个文件
- python - 无法在 matplotlib 中保存带注释的图像
- javascript - 使用 EvaluateJavaScript 单击按钮的意外行为