我正在尝试用剪刀纸玩游戏,在尝试自学c ++时练习一些基础知识。我让它简短地工作了,但是我调整了一些cout内容,现在它短路了一些if语句。任何关于为什么不起作用的见解将不胜感激。
#include <string>
#include <iostream>
#include <cstdlib>
#include <ctime>
游戏功能。要求输入播放器。将变量随机分配并猜测为整数,然后将其分配给字符串result和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 (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";
}
}
}
据我所知,主要功能正常运行,但是在编译时会出现一条警告:“ 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++;
}