mercredi 3 octobre 2018

C# craps simulation

I have seen questions related to Craps, but they were related to python and Java. I hope someone familiar with C# can help me. The code below is meant to simulate the game of craps. The code mostly works, except for one issue.

I suppose I should briefly explain the game. Two 6 sided dice are rolled. If the result is a 7 or 11. The player or "shooter" wins automatically. If a 2, 3, or 12 is rolled, the shooter loses automatically. However, if another number is rolled, that number becomes the "point". The shooter rolls again until either they roll the point again or a 7. If the point is rerolled the shooter wins. If a 7 is rolled, this time it is a loss.

So, the problem is that this code still gives wins or losses automatically for 2, 3, 11, and 12 after the first roll. For example: The shooter rolls a 6. 6 becomes the point and the shooter rolls again. If the shooter rolls a 3 this code marks this result as a loss when it should just keep rolling until a 6 or 7 is rolled. This incorrect end happens whenever a 2, 3, 11, or 12 is rolled after the first roll. Any help on correcting the logic of this code would be greatly appreciated. Thanks Also, I hope this question is formatted correctly. Please let me know if it is not.

class Craps
{
    const int dieSides = 6;

    int roll;
    //const int repeatGame = 1000;

    Random random = new Random();

    public void RollDice()
    {
        int die1 = 0;
        int die2 = 0;

        die1 = random.Next(1, 7);

        die2 = random.Next(1, 7);

        roll = die1 + die2;
        //Console.WriteLine("The shooter roled: {0}", roll);
    }

    public void PlayCraps()
    {
        RollDice();
        Console.WriteLine("The shooter roled: {0}", roll);

        int gameStatus = 0;
        int point = roll;
        int numRolls = 1;
        if (roll != 7 || roll != 11 || roll != 2 || roll != 3 || roll != 12)
        {
            Console.WriteLine("The point is: {0}", point);
        }
        while (gameStatus < 1)
        {


            if (roll == 7 || roll == 11)
            {
                Console.WriteLine("You won!");
                gameStatus++;
                break;
            }
            else if (roll == 2 || roll == 3 || roll == 12)
            {
                Console.WriteLine("You lost.");
                gameStatus++;
                break;
            }
            else
            {

                while (point != roll || roll != 7)
                {
                    RollDice();

                    if (roll == point)
                    {
                        Console.WriteLine("The shooter roled: {0}", roll);
                        Console.WriteLine("You won!");
                        numRolls++;
                        gameStatus++;
                        break;
                    }

                    if (roll == 7)
                    {
                        Console.WriteLine("The shooter roled: {0}", roll);
                        Console.WriteLine("You lost");
                        numRolls++;
                        gameStatus++;
                        break;
                    }
                    else
                    {
                        numRolls++;
                        Console.WriteLine("The shooter roled: {0}", roll);
                        break;
                    }

                }

            }

        }
        Console.WriteLine("This game was {0} roll(s)", numRolls);
    }



    static void Main(string[] args)
    {
        Craps NewGame = new Craps();
        NewGame.PlayCraps();
        Console.ReadLine();
    }
}

Aucun commentaire:

Enregistrer un commentaire