石头剪刀布游戏 c++

Rock-paper-scissors game c++

本文关键字:c++ 游戏 石头剪刀布      更新时间:2023-10-16

石头剪刀布游戏 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 . . .
*/