[英]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 Game\n";
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/3s 的代碼兩次
****這部分****
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<<"\n\nROCK PAPER SCISSORS!!!";
cout<<"\n---------\n1) Rock\n2) Paper\n3) Scissors\n4) Quit\n\n";
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: Rock\n";
}//end if
else if(userChoice == 2)
{
cout<<"You selected: Paper\n";
}//end else if
else if(userChoice == 3)
{
cout<<"You selected: Scissors\n";
}//end else if #2
if(computerChoice == 1)
{
cout<<"The computer selected: Rock\n";
}//end if
else if(computerChoice == 2)
{
cout<<"The computer selected: Paper\n";
}//end else if
else if(computerChoice == 3)
{
cout<<"The computer selected: Scissors\n";
}//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.\n\n"<<endl;
}
if ( userChoice == 1 && computerChoice == 2)
{
cout << "Paper wraps rock, COMPUTER WINS\n\n";
}
else if (userChoice == 1 && computerChoice == 3)
{
cout << "Rock smashes scissors. YOU WON!\n\n";
}
if (userChoice == 2 && computerChoice == 1)
{
cout <<"Paper wraps rock. YOU WON!\n\n";
}
else if ( userChoice == 2 && computerChoice == 3)
{
cout <<"Scissors cut paper, COMPUTER WINS\n\n";
}
if ( userChoice == 3 && computerChoice == 1)
{
cout <<"Scissors cut paper, COMPUTER WINS\n\n";
}
else if (userChoice == 3 && computerChoice == 2)
{
cout <<"Rock smashes scissors. YOU WON!\n\n";
}
}//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 . . .
*/
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.