石头剪刀布游戏 c++
Rock-paper-scissors game c++
石头剪刀布游戏 c++ 不显示 cout 来判断谁赢得了游戏,我不知道出了什么问题,为什么程序不显示 cout。请让我知道出了什么问题以及如何解决它以及为什么会发生它,我认为 cstdlib 在那里什么也没做。
目的:为剪刀布石头布游戏打分。如果用户输入 输入无效,你的程序应该这样说,否则它会输出 以上结果之一。
#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
string pone;
string ptwo;
string r;
string p;
string s;
cout << "Rock, Paper, Scissors Gamen";
cout << "nPlayer One, please enter your move: ('p' for Paper, 'r' for Rock, 's' for Scissor)";
cin >> pone;
cout << "nPlayer Two, please enter your move: ('p' for Paper, 'r' for Rock, 's' for Scissor)";
cin >> ptwo;
if (pone == ptwo)
{
cout <<"nThere is a tie"<<endl;
}
if ( pone == r && ptwo == p)
{
cout << "nPaper wraps rock, Player 1 win";
}
else if (pone == r && ptwo == s)
{
cout << "nRock smashes scissors, player 1 win";
}
if (pone == p && ptwo == r)
{
cout <<"nPaper wraps rock, player 1 win";
}
else if ( pone == p && ptwo == s)
{
cout <<"nScissors cut paper, player 2 win";
}
if ( pone == r && ptwo == p)
{
cout << "nPaper wraps rock, Player 1 win";
}
else if (pone == r && ptwo == s)
{
cout << "nRock smashes scissors, player 1 win";
}
if (pone == p && ptwo == r)
{
cout <<"nPaper wraps rock, player 1 win";
}
else if ( pone == p && ptwo == s)
{
cout <<"nScissors cut paper, player 2 win";
}
if ( ptwo == s && pone == r)
{
cout <<"nScissors cut paper, player 1 win";
}
else if (ptwo == s && pone == p)
{
cout <<"nRock smashes scissors, player 2 win ";
}
return 0;
}
这是你的问题:
if (pone == r && ptwo == p)
{
cout << "nPaper wraps rock, Player 1 win";
}
else if (pone == r && ptwo == s)
{
cout << "nRock smashes scissors, player 1 win";
}
//etc etc
将这些"r"、"p"和"s"放在引号中,如下所示:
if (pone == "r" && ptwo == "p")
//etc etc
你应该很好
你应该给 r、p 和 s 分配一些值
宁愿
r = "r";
p = "p";
s = "s";
希望这有帮助。
我知道
这有点晚了,但是...
这是我的程序。我使用了代码的 if elseif 部分,并发现了它的问题。 您重复 2/3 秒的代码两次
****这部分****
if ( pone == r && ptwo == p)
{
cout << "nPaper wraps rock, Player 1 win";
}
else if (pone == r && ptwo == s)
{
cout << "nRock smashes scissors, player 1 win";
}
而这部分
if (pone == p && ptwo == r)
{
cout <<"nPaper wraps rock, player 1 win";
}
else if ( pone == p && ptwo == s)
{
cout <<"nScissors cut paper, player 2 win";
}
这是我的代码版本,(这是一个准备运行的完整程序)希望这对:)有所帮助
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int userChoice; // To hold the user's choice
int computerChoice; // To hold the computer's choice
int choice;
//function prototypes
int getUserChoice(int);
int getComputerChoice();
void determineWinner(int,int);
void displayChoice (int userChoice, int computerChoice);
//*************************************************
//Function main
//*************************************************
int main()
{
// Get the computer's choice.
computerChoice = getComputerChoice();
// Get the user's choice.
userChoice = getUserChoice(choice);
while (userChoice != 4)
{
//Displays selections
displayChoice (userChoice,computerChoice);
// Determine the winner.
determineWinner(userChoice, computerChoice);
// Get the computer's choice.
computerChoice = getComputerChoice();
// Get the user's choice.
userChoice = getUserChoice(choice);
}//end while
return 0;
}
//end main
// The getUserChoice function displays a menu allowing
// the user to select rock, paper, or scissors. The
// function then returns 1 for rock (via the ROCK
// constant), or 2 for paper (via the PAPER constant),
// or 3 for scissors (via the SCISSORS constant).
int getUserChoice(int choice)
{
cout<<"nnROCK PAPER SCISSORS!!!";
cout<<"n---------n1) Rockn2) Papern3) Scissorsn4) Quitnn";
cout<<"Enter your choice:";
cin>>choice;
return choice;
}//end getUserChoice
// The getComputerChoice function returns the computer's
// game choice. It returns 1 for rock (via the ROCK
// constant), or 2 for paper (via the PAPER constant),
// or 3 for scissors (via the SCISSORS constant).
int getComputerChoice()
{
int number;
int seed = time(0);//gets system time
srand(seed);//seed the random number
number = 1 + rand() % 3;//generate random # 1-3
return number;
}//end getComputerChoice
// The displayChoice function accepts an integer
// argument and displays rock, paper, or scissors.
void displayChoice(int userChoice, int computerChoice)
{
//displays what you&the comp selected
if(userChoice == 1)
{
cout<<"You selected: Rockn";
}//end if
else if(userChoice == 2)
{
cout<<"You selected: Papern";
}//end else if
else if(userChoice == 3)
{
cout<<"You selected: Scissorsn";
}//end else if #2
if(computerChoice == 1)
{
cout<<"The computer selected: Rockn";
}//end if
else if(computerChoice == 2)
{
cout<<"The computer selected: Papern";
}//end else if
else if(computerChoice == 3)
{
cout<<"The computer selected: Scissorsn";
}//end else if #2
}
// The determineWinner function accepts the user's
// game choice and the computer's game choice as
// arguments and displays a message indicating
// the winner.
void determineWinner(int userChoice, int computerChoice)
{
//determines winner
if (userChoice == computerChoice)
{
cout <<"Tie. NO WINNER.nn"<<endl;
}
if ( userChoice == 1 && computerChoice == 2)
{
cout << "Paper wraps rock, COMPUTER WINSnn";
}
else if (userChoice == 1 && computerChoice == 3)
{
cout << "Rock smashes scissors. YOU WON!nn";
}
if (userChoice == 2 && computerChoice == 1)
{
cout <<"Paper wraps rock. YOU WON!nn";
}
else if ( userChoice == 2 && computerChoice == 3)
{
cout <<"Scissors cut paper, COMPUTER WINSnn";
}
if ( userChoice == 3 && computerChoice == 1)
{
cout <<"Scissors cut paper, COMPUTER WINSnn";
}
else if (userChoice == 3 && computerChoice == 2)
{
cout <<"Rock smashes scissors. YOU WON!nn";
}
}//end determindWinner
/*
Proof
ROCK PAPER SCISSORS!!!
---------
1) Rock
2) Paper
3) Scissors
4) Quit
Enter your choice:1
You selected: Rock
The computer selected: Paper
Paper wraps rock, COMPUTER WINS
ROCK PAPER SCISSORS!!!
---------
1) Rock
2) Paper
3) Scissors
4) Quit
Enter your choice:2
You selected: Paper
The computer selected: Paper
Tie. NO WINNER.
ROCK PAPER SCISSORS!!!
---------
1) Rock
2) Paper
3) Scissors
4) Quit
Enter your choice:3
You selected: Scissors
The computer selected: Paper
Rock smashes scissors. YOU WON!
ROCK PAPER SCISSORS!!!
---------
1) Rock
2) Paper
3) Scissors
4) Quit
Enter your choice:4
Press any key to continue . . .
*/
相关文章:
- 在C++游戏中与库存系统作斗争
- 换位表导致测试失败(但在游戏中运行良好)
- Craps游戏问题,忽略if语句
- 矢量下标超出SFML游戏中的范围
- 我是 c++ 的新手.学习基本知识后,我想做井字游戏.对于印刷板,我在下面写代码,但它显示错误
- 我在贪吃蛇游戏中收到了错误代码 -1073741571
- 我应该在简单的策略游戏中为各个派系使用类吗 - C++
- 游戏体验和升级
- 我的代码运行良好,但在游戏循环中中断
- 如何使用 SFML 在贪吃蛇游戏中定义游戏结束?
- 我可以使用任何好的逻辑来阻止计算机将 O 放在井字游戏中的现有 X 上
- 将鼠标和键盘输入发送到 unity3d 游戏 (Rust)
- 在猜谜游戏 c++ 中不计算尝试次数
- 在C++不适用于猜数字游戏的情况下再次播放选项
- 在为我的基于文本的 RPG 游戏制作库存时遇到困难
- 在定义字符数组(井字游戏)的 for 循环中应用输入限制
- Frank Luna 在他的书"使用 DirectX12 进行 3D 游戏编程"的介绍中盒子示例的问题
- 如何在Linux上正确发布C++软件(游戏)
- 提高基于组件的游戏引擎的效率
- 文本冒险游戏 - 如何区分一种项目类型与另一种项目类型以及如何构建项目类/子类