【问题标题】:C# Game of craps using dice roll method使用掷骰子方法的 C# 掷骰子游戏
【发布时间】:2018-10-03 15:44:37
【问题描述】:

以下代码用于掷骰子游戏。我不确定代码的逻辑是否正确。我想对其进行测试,但是当我运行此代码时,没有给出任何输出。它编译并显示一个没有输出的空白屏幕。我无法弄清楚为什么什么都没有显示。此外,任何关于代码逻辑的建议都将不胜感激。当最初未滚动 2、3、7、11 或 12 时,我在如何进行重新滚动过程中遇到困难。谢谢

对于不熟悉游戏的玩家:掷出 2 个骰子,掷出 7 或 11 即为获胜。 2、3 或 12 是损失。任何其他数字都成为“点”,玩家重新滚动直到点或 7 被滚动。匹配点就是胜利。这一次 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();
    }
}
}

【问题讨论】:

  • 调用NewGame.PlayCraps()怎么样
  • 当你创建一个新游戏时,什么都不会发生,因为你没有调用任何掷骰子的方法。

标签: c# loops random methods


【解决方案1】:

在您的 Main 函数中,您正在创建一个 Craps 对象,但从未对它做任何事情。

如果你调用Craps.PlayCraps(),这将导致它实际上做一些事情,而不是创建一个对象然后等待用户输入。

【讨论】:

    【解决方案2】:

    我可能弄错了,但我相信main方法,你应该调用方法,而不仅仅是类。 示例:

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

    } }

    【讨论】:

      【解决方案3】:

      作为您在 Main() 中的其他建议的替代方法,您可以在新的 NewGame 对象上调用 PlayCraps() 方法,例如:

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

      您可以改为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();
          }
      }
      

      现在,当您初始化 NewGames Craps 对象时,PlayCraps() 方法将作为初始化的一部分被调用,游戏将开始。我认为另一种方式更清楚一点,它允许您在调用PlayCraps() 方法之前设置Craps 属性(如果它们存在的话),但我觉得这里使用构造函数值得一提。

      【讨论】:

        猜你喜欢
        • 2012-02-29
        • 2015-12-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-08-02
        • 2021-04-06
        相关资源
        最近更新 更多