XNA - 骰子检查玩家是否滚动了数字6

时间:2017-11-10 23:54:50

标签: c# xna dice

我正在c#XNA中制作一个“简单的游戏”。我有一个滚动按钮和两个玩家。
我想要发生的是每次按下滚动按钮时,生成的数字等于6,相同的玩家可以再次滚动。我已经尝试了所有形式循环和if-else语句。

public void DiceCheck()
    {
        if (randomNum == 6)
        {
            if (playerTurn) //Intial value of bool playerTurn is equal to true: Which means player 1 turn
            {
                playerTurn = true; //Allow player 1 to roll again
            }

            else if (playerTurn == false) // Player 2 turn
            {
                playerTurn = false; //Allow player 2 to roll again
            }
        }
    }

每次玩家滚动时我都会添加一些内容

player2turn++;
player1turn++;

当我为两个玩家插入它时,每次玩家获得6时,它会将掷骰编号加2。我希望它生成6然后让玩家再次点击滚动。但它不能按我想要的方式工作。

1 个答案:

答案 0 :(得分:1)

public void GameLoop(Random dice) 
{
    int randomNum = 0; 
    int turn = 0;
    while(true)
    {
         randomNum = dice.Next(1,7); //next turn roll
         Console.WriteLine(string.Format("Player {0} rolled a {1}", turn%2 + 1, randomNum));
         if(!CheckForReroll(randomNum)) // if it's a reroll don't change player's turns
         {
             turn++;
         }

         if(turn == 10) break; //made up rules to stop at turn 10 so we don't loop infinitely
    }
}

public bool CheckForReroll(int randomNum)
{
    return randomNum == 6;
}