摇滚剪刀游戏c ++

时间:2014-03-04 08:38:58

标签: 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; 
}

3 个答案:

答案 0 :(得分:2)

这是你的问题:

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

你应该很好

答案 1 :(得分:1)

您应该为r,p和s指定一些值

优选地

r = "r";
p = "p";
s = "s";

希望这有帮助。

答案 2 :(得分:0)

我知道这有点晚了但是......

这是我的计划。我使用了你的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<<"\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 . . .
*/