首页 > 解决方案 > 骰子游戏代码陷入无限循环

问题描述

我有一个猪骰子游戏,有两种模式(掷 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;
}

标签: c++xcode

解决方案


以下块中有几个问题可能会导致问题:

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

您编写条件的方式,它只是检查是否roll1除零以外的任何内容(即之前的部分&&),然后检查是否roll2等于 1。条件可能应该是:else if (roll1 == 1 && roll2 == 1)

而且我认为您想=在底部分配 ( ) playerScore 而不是评估相等性 ( ==)。您已经知道这一点,但它应该是这样的:playerScore = 0.


推荐阅读