简体   繁体   English

C#使用骰子掷骰子游戏

[英]C# Game of craps using dice roll method

The code below is for a game of Craps. 以下代码适用于掷骰子游戏。 I am not sure if the logic of the code is correct. 我不确定代码的逻辑是否正确。 I want to test it, but when I run this code no output is given. 我想测试它,但是当我运行这段代码时,没有输出。 It compiles and a blank screen is displayed with no output. 它将编译并显示空白屏幕,没有输出。 I can't figure out why nothing is displayed. 我不知道为什么什么都不显示。 Also, any advice on the logic of the code would be appreciated. 同样,任何对代码逻辑的建议也将不胜感激。 I am having difficulty with how to do the reroll process when 2, 3, 7, 11, or 12 are not rolled initially. 当最初不滚动2、3、7、11或12时,我在如何执行重新滚动过程上遇到困难。 Thanks 谢谢

For those unfamiliar with the game: 2 dice are rolled and rolling a 7 or 11 is a win. 对于那些不熟悉该游戏的人:掷2个骰子,掷7或11则获胜。 2, 3, or 12 is a loss. 2、3或12是亏损。 Any other number becomes the "point" and the player rerolls until the point or a 7 is rolled. 其他任何数字都将成为“点”,并且玩家将重新滚动直到该点或7被掷出为止。 Matching the point is a victory. 达到目标就是胜利。 This time a 7 is a loss. 这次7是亏损。

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(6) + 1;

        die2 = random.Next(6) + 1;

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

    public void PlayCraps()
    {
        RollDice();
        int gameStatus = 0;
        int point = roll;
        int numRolls = 1;

        while (gameStatus < 1)
        {


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

                RollDice();
                Console.WriteLine("The point is: {0}", point);

                while (point != roll || roll != 7)
                {
                    if (roll == point)
                    {
                        Console.WriteLine("You won!");
                        numRolls++;
                        gameStatus++;
                    }

                    if (roll == 7)
                    {
                        Console.WriteLine("You lost");
                        numRolls++;
                        gameStatus++;
                    }
                    RollDice();
                    numRolls++;

                }

            }
        }
    }



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

In your Main function, you're creating a Craps object, but never doing anything with it. Main函数中,您正在创建Craps对象,但是从不对其执行任何操作。

If you call Craps.PlayCraps() , that'll cause it to actually do something other than create an object and then wait for user input. 如果调用Craps.PlayCraps() ,它将导致它实际执行除创建对象以外的其他操作,然后等待用户输入。

I may be mistaken, but I believe in the main method, you should call the method, not just the class. 我可能会弄错,但我相信在主方法中,您应该调用该方法,而不仅仅是类。 Example: 例:

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

} } }}

As an alternative to the other suggestions that you in your Main() you call your PlayCraps() method on your new NewGame object like: 作为您在Main()的其他建议的替代方案,您可以在新的NewGame对象上调用PlayCraps()方法,例如:

 Craps NewGame = New Craps();
 NewGame.PlayCraps();

You could instead call the PlayCraps() method in the Craps constructor: 您可以改为Craps构造函数中调用PlayCraps()方法:

class Craps
{
    const int dieSides = 6;

    int roll;
    //const int repeatGame = 1000;

    Random random = new Random();

    //start the game in the constructor:
    public Craps()
    {
       this.PlayCraps();
    }


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

        die1 = random.Next(6) + 1;

        die2 = random.Next(6) + 1;

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

    public void PlayCraps()
    {
        RollDice();
        int gameStatus = 0;
        int point = roll;
        int numRolls = 1;

        while (gameStatus < 1)
        {


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

                RollDice();
                Console.WriteLine("The point is: {0}", point);

                while (point != roll || roll != 7)
                {
                    if (roll == point)
                    {
                        Console.WriteLine("You won!");
                        numRolls++;
                        gameStatus++;
                    }

                    if (roll == 7)
                    {
                        Console.WriteLine("You lost");
                        numRolls++;
                        gameStatus++;
                    }
                    RollDice();
                    numRolls++;

                }

            }
        }
    }



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

Now when you initialize your NewGames Craps object the PlayCraps() method will be called as part of that initialization and the game will start. 现在,当您初始化NewGames Craps对象时,将在该初始化过程中调用PlayCraps()方法,然后游戏将开始。 I think the other way is a little more clear and it would allow you to set Craps properties (if they ever exist) before calling your PlayCraps() method, but I feel that the use of the constructor here is worth mentioning. 我认为另一种方法更清晰一些,它允许您在调用PlayCraps()方法之前设置Craps属性(如果存在),但是我认为这里值得一提的是构造函数的使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM