骰子游戏代码陷入无限循环

时间:2018-07-26 17:01:53

标签: c++ xcode

我有一个猪骰子游戏,其中有两种模式(掷出1或2个骰子)。与2位人类玩家一起玩过。当我选择1个骰子运行程序时,它运行良好,但是当我掷2个骰子时,它陷入了无限循环。我正在寻找问题的根源以及为什么将其抛出循环的提示,因为我认为这两个程序应该几乎相同。如果代码看起来很奇怪,请提前抱歉。

#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

const int PLAYER1 = 0;
const int PLAYER2 = 1;
const int winningScore = 100;
int turn = PLAYER1;


void printIntro()
{
    cout << " Welcome to the dice game: Pig! "<< endl;
    cout << "The goal is to be the first player to reach 100. If playing with one die the rules are that each player can rol as many times as they    choose. just dont roll a 1 or else you'll lose your turn AND all points accumulated in that round. If you're  playing with 2 dies the same rules applies, but if you roll snake eyes (double 1's) you'll not only lose your turn but you'll also loose all your points. good luck and may the best player win!"<< endl;
}


int game1(string playerName, int playerScore)
{
    int roll = rand() % 6 + 1;
    cout << playerName << " You rolled: " << roll <<endl;

    if(roll == 1)
{
    cout << " OH NO! You rolled a 1. "<< endl;
    cout << " Your turn is over. " << endl;
    playerScore = 0;
}

else
{
    playerScore +=roll;
    cout << playerName << " Your score: " << playerScore <<endl;
}

if(roll == 1)
{
    if(turn == PLAYER1)
        turn = PLAYER2;

    else
        turn = PLAYER1;
}
else
{
    char choice;

    cout << " Would you like to roll again? (y/n): ";
    cin >> choice;

    if(choice != 'y')
    {
        if (turn == PLAYER1)
            turn = PLAYER2;
        else
            turn = PLAYER1;
    }
}

return playerScore;

}

int game2(string playerName, int playerScore)
{  
int roll1 = rand() % 6 + 1;
int roll2 = rand() % 6 + 1;

cout << playerName << " You rolled: " << roll1 << " and " << roll2 <<endl;

if(roll1 || roll2 == 1)
{
    cout << " OH NO! You rolled a 1. " << endl;
    cout << " Your turn is over. " << endl;
    playerScore = 0;
}

else if (roll1 && roll2 == 1)
{
    cout << "OH CRAP! You rolled snake eyes!" << endl;
    cout << " Your turn is over. " << endl;
    playerScore == 0;
}

else
{
    playerScore += roll1 + roll2 ;
    cout << playerName << " Your score: " << playerScore <<endl;
}

if(roll1 || roll2 == 1)
{
    if(turn == PLAYER1)
        turn = PLAYER2;

    else
        turn = PLAYER1;
}

else if (roll1 && roll2 == 1)
{
    if(turn == PLAYER1)
        turn = PLAYER2;

    else
        turn = PLAYER1;
}

else
{
    char choice;

    cout << "Would you like to roll again? (y/n): ";
    cin >> choice;

    if(choice != 'y')
    {
        if (turn == PLAYER1)
            turn = PLAYER2;
        else
            turn = PLAYER1;
    }
}

return playerScore;
}


int main()
{
    srand(time(0));

int player1score = 0;
int player2score = 0;
string player1name;
string player2name;
int dieRoll;

printIntro();

cout << " Player 1, Enter your name: ";
cin >> player1name;
cout << " Player 2, Enter your name: ";
cin >> player2name;
cout << "Wouild you like to roll with 1 or 2 dice?" << endl;
cin >> dieRoll;

if (dieRoll == 1)
{
    while (player1score < winningScore && player2score < winningScore)
    {


        if (turn == PLAYER1)
        {
            player1score = game1(player1name, player1score);
        }

        else
        {
            player2score = game1(player2name, player2score);
        }

    }

    if(player1score >= winningScore)
    {
        cout << player1name <<endl;
        cout << " Your score is : " << player1score<<endl;
        cout << player1name << " WINS! " << endl;
    }

    else
    {
        cout << player2name << endl;
        cout <<" Your score: "<< player2score << endl;
        cout << player2name << " WINS!" << endl;
    }
}

else
{
    while (player1score < winningScore && player2score < winningScore)
    {


        if (turn == PLAYER1)
        {
            player1score = game2(player1name, player1score);
        }

        else
        {
            player2score = game2(player2name, player2score);
        }

    }

    if(player1score >= winningScore)
    {
        cout << player1name <<endl;
        cout << " Your score is : " << player1score<<endl;
        cout << player1name << " WINS! " << endl;
    }

    else
    {
        cout << player2name << endl;
        cout <<" Your score: "<< player2score << endl;
        cout << player2name << " WINS!" << endl;
    }
}

return 0;
}

1 个答案:

答案 0 :(得分:3)

以下块中有几个问题可能会引起问题:

else if (roll1 && roll2 == 1)
{
    cout << "OH CRAP! You rolled snake eyes!" << endl;
    cout << " Your turn is over. " << endl;
    playerScore == 0;
}

您编写条件代码的方式只是检查roll1是否为零(这是&&之前的部分),然后然后检查是否{ {1}}等于1。条件可能应为:roll2

我想您想在底部分配(else if (roll1 == 1 && roll2 == 1))playerScore,而不是评估是否相等(=)。您已经知道了,但这应该是这样的:==