为什么我的石头剪刀布游戏不起作用?



我正在做一个石头剪刀游戏,在我尝试自学 c++ 时练习一些基础知识。 我让它短暂工作,但我调整了一些 cout 内容,现在它短路了一些 if 语句。任何关于为什么这不起作用的见解将不胜感激。

#include <string>
#include <iostream>
#include <cstdlib>
#include <ctime>

游戏功能。请求播放器输入。将变量随机分配并猜测分配给整数,然后分配给字符串结果和result_2。最后,while 循环跟踪分数。

void Play_rps() {
int guess;
int i = 0;
int o = 0;
std::string result;
std::string result_2;
std::cout << " Rock, Paper, Scissors. GO!! n";
while (i != 2 || o != 2) {
int random = rand() % 4;
std::cout << " 1.) Rock n 2.) Paper n 3.) Scissors n";
std::cin >> guess;
switch (guess) {
case 1:
result = "rock";
break;
case 2:
result = "paper";
break;
case 3:
result = "scissors";
break;
}
switch (random) {
case 1:
result_2 = "rock";
break;
case 2:
result_2 = "paper";
break;
case 3:
result_2 = "scissors";
break;
}

嵌套的 if 语句确定每轮的出局结果并增加获胜计数器。


if (result == result_2) {
std::cout << " you threw " << result << std::endl;
std::cout << " your opponant threw " << result_2 << std::endl;
std::cout << " TIE! n" << std::endl;
}
else if (result == " rock" && result_2 == " paper") {
std::cout << " you threw " << result << std::endl;
std::cout << " your opponant threw " << result_2 << std::endl;
std::cout << " Paper Beats Rock!n You Lose! n" << std::endl;
o++;
}
else if (result == " paper" && result_2 == " rock") {
std::cout << " you threw " << result << std::endl;
std::cout << " your opponant threw " << result_2 << std::endl;
std::cout << " Paper Beats Rock!n You win! n" << std::endl;
i++;
}
else if (result == " scissors" && result_2 == " paper") {
std::cout << " you threw " << result << std::endl;
std::cout << " your opponant threw " << result_2 << std::endl;
std::cout << " scissors Beat Paper!n You Win! n" << std::endl;
i++;
}
else if (result == " rock" && result_2 == " scissors") {
std::cout << " you threw " << result << std::endl;
std::cout << " your opponant threw " << result_2 << std::endl;
std::cout << " Rock Beats Scissors!n You Win! n" << std::endl;
i++;
}
else if (result == " scissors" && result_2 == " rock") {
std::cout << " you threw " << result << std::endl;
std::cout << " your opponant threw " << result_2 << std::endl;
std::cout << " Rock Beat Scissors! n You lose! n" << std::endl;
o++;
}
else if (result == " paper" && result_2 == " scissors") {
std::cout << " you threw " << result << std::endl;
std::cout << " your opponant threw " << result_2 << std::endl;
std::cout << " scissors Beat Paper!n You Lose! n" << std::endl;
o++;
}
if (i == 2) {
std::cout << " YOU ARE VICTORIOUS n";
}
else if (o == 2) {
std::cout << " DEFEAT n";
}

}
}

据我所知,main 函数工作正常,但是当我编译时,有一个关于"t-time 是一个无符号的 int,这可能会导致数据丢失"的警告,这是我收到的唯一通知。

int main()
{
srand(time(NULL));
int choice;
do {
std::cout << "0.) Quit n1.) Play Game n";
std::cin >> choice;
switch (choice) {
case 0:
std::cout << "Thanks for nothing! n";
return 0;
case 1:
Play_rps();
break;
default:
std::cout << " That's not a valid choice. n";
}
} while (choice != 0);
} 

删除 == 语句中的空格,您可以分解一些 cout 调用

//" paper" --> "paper",  " rock" --> "rock"
std::cout << " you threw " << result << std::endl;
std::cout << " your opponant threw " << result_2 << std::endl;
if (result == result_2) {
std::cout << " TIE! n" << std::endl;
}
else if (result == "rock" && result_2 == "paper") {
std::cout << " Paper Beats Rock!n You Lose! n" << std::endl;
o++;
}
else if (result == "paper" && result_2 == "rock") {
std::cout << " Paper Beats Rock!n You win! n" << std::endl;
i++;
}
else if (result == "scissors" && result_2 == "paper") {
std::cout << " scissors Beat Paper!n You Win! n" << std::endl;
i++;
}
else if (result == "rock" && result_2 == "scissors") {
std::cout << " Rock Beats Scissors!n You Win! n" << std::endl;
i++;
}
else if (result == "scissors" && result_2 == "rock") {
std::cout << " Rock Beat Scissors! n You lose! n" << std::endl;
o++;
}
else if (result == "paper" && result_2 == "scissors") {
std::cout << " scissors Beat Paper!n You Lose! n" << std::endl;
o++;
}

最新更新