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

C++ 骰子游戏代码陷入无限循环,c++,xcode,C++,Xcode,我有一个猪骰子游戏,有两种模式(1或2个骰子掷)。它是由两个人玩的。当我选择1个骰子运行我的程序时,它运行得很好,但当我掷2个骰子时,它会被扔进一个无限循环。我正在寻找一个关于问题所在的提示,以及为什么它被抛出一个循环,因为在我看来,两个程序应该几乎相同。如果代码看起来很奇怪,请提前道歉 #include <iostream> #include <cstdlib> #include <ctime> using namespace std; const in

我有一个猪骰子游戏,有两种模式(1或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;
}
#包括
#包括
#包括
使用名称空间std;
常数int PLAYER1=0;
常数int PLAYER2=1;
const int winningScore=100;
int turn=播放器1;
void printIntro()
{

cout以下模块中存在可能导致问题的几个问题:

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

你是指
playerScore==0;
(在
else中,如果(roll1&&roll2==1)
)作为一项任务?哇,你需要练习一下代码组织。为什么所有这些自由函数而不是持有状态的对象?此外,你可能想了解。另请参见。堆栈溢出不是一项免费的调试服务,你应该展示你尝试使用调试器或其他更简单的方法(如debug print stateme)调试代码的情况nts。您还可以分别测试代码的每一部分,以确定代码的哪一部分是导致问题的原因,并做出正确的判断。这不会是您在代码中出现错误的唯一一次,学习调试程序将比让别人帮您找到错误有更大的帮助。类似
if(x==(1 | | 0))
如果((x&&y)==2)
不做你认为他们做的事。
|
和&
运算符所做的唯一事情就是接收两个布尔值并输出一个布尔值。就是这样。这些运算符与“and”和“or”的含义几乎没有任何相似之处在英语中,它们与将某物与多个值进行比较无关。
if(x==(1 | | 0))
所做的是将
x
1 | | 0
进行比较,即1。正确的方法是将两个比较与逻辑运算符结合使用,如
if(x==1 | x==0)