Search code examples
c#xnadice

XNA - Dice Check to see if player rolled the number 6


I'm making a "simple game" in c# XNA. I have a roll button and two players.
What I want to happen is each time you press that roll button the number that was generated equals 6 that same player is able to roll again. I have tried everything form loops and if-else statements.

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
            }
        }
    }

I also have something to adds every time the player rolls

player2turn++;
player1turn++;

When i insert it for both players it adds 2 to the roll number each time the player gets a 6. I want it to generate the 6 then have the player click to roll again. But it doesn't work the way I want.


Solution

  • 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;
    }