C#使用掷骰子法的掷骰子游戏

C#使用掷骰子法的掷骰子游戏,c#,loops,random,methods,C#,Loops,Random,Methods,下面的代码是一个垃圾游戏。我不确定代码的逻辑是否正确。我想测试它,但是当我运行这段代码时,没有给出任何输出。它会编译并显示一个没有输出的空白屏幕。我不明白为什么什么也不显示。此外,任何关于代码逻辑的建议都将不胜感激。当最初没有滚动2、3、7、11或12时,我很难理解如何执行重新滚动过程。谢谢 对于那些不熟悉游戏的人:掷2个骰子,掷7或11就是胜利。2、3或12是一种损失。任何其他数字成为“点”,玩家重新掷骰,直到掷到点或7为止。达到这一点就是胜利。这次7是一个损失 class Craps {

下面的代码是一个垃圾游戏。我不确定代码的逻辑是否正确。我想测试它,但是当我运行这段代码时,没有给出任何输出。它会编译并显示一个没有输出的空白屏幕。我不明白为什么什么也不显示。此外,任何关于代码逻辑的建议都将不胜感激。当最初没有滚动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();
    }
}
}
类垃圾
{
常数int DIESEDS=6;
整流罩;
//const int repeatGame=1000;
随机=新随机();
公共无效滚动骰子()
{
int die1=0;
int die2=0;
die1=随机。下一个(6)+1;
die2=随机。下一个(6)+1;
辊=die1+die2;
Console.WriteLine(“射手角色:{0}”,滚动);
}
公共垃圾场
{
滚动骰子();
int gameStatus=0;
int点=滚动;
int numRolls=1;
同时(游戏状态<1)
{
如果(滚动==7 | |滚动==11)
{
Console.WriteLine(“你赢了!”);
打破
}
否则如果(滚动==2 | |滚动==3 | |滚动==12)
{
Console.WriteLine(“你输了”);
打破
}
其他的
{
滚动骰子();
WriteLine(“点是:{0}”,点);
同时(点!=滚动| |滚动!=7)
{
如果(滚动=点)
{
Console.WriteLine(“你赢了!”);
numRolls++;
游戏状态++;
}
如果(滚动==7)
{
Console.WriteLine(“你输了”);
numRolls++;
游戏状态++;
}
滚动骰子();
numRolls++;
}
}
}
}
静态void Main(字符串[]参数)
{
骰子新游戏=新骰子();
Console.ReadLine();
}
}
}

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

}
}

在您的
Main
函数中,您正在创建一个
垃圾
对象,但从未对其进行任何操作


如果您调用
Craps.PlayCraps()
,这将导致它实际执行一些操作,而不是创建一个对象,然后等待用户输入。

作为您在
Main()
中的其他建议的替代方法,您可以在新
新游戏
对象上调用
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();
    }
}
类垃圾
{
常数int DIESEDS=6;
整流罩;
//const int repeatGame=1000;
随机=新随机();
//在构造函数中开始游戏:
公共垃圾
{
这个。玩垃圾();
}
公共无效滚动骰子()
{
int die1=0;
int die2=0;
die1=随机。下一个(6)+1;
die2=随机。下一个(6)+1;
辊=die1+die2;
Console.WriteLine(“射手角色:{0}”,滚动);
}
公共垃圾场
{
滚动骰子();
int gameStatus=0;
int点=滚动;
int numRolls=1;
同时(游戏状态<1)
{
如果(滚动==7 | |滚动==11)
{
Console.WriteLine(“你赢了!”);
打破
}
否则如果(滚动==2 | |滚动==3 | |滚动==12)
{
Console.WriteLine(“你输了”);
打破
}
其他的
{
滚动骰子();
WriteLine(“点是:{0}”,点);
同时(点!=滚动| |滚动!=7)
{
如果(滚动=点)
{
Console.WriteLine(“你赢了!”);
numRolls++;
游戏状态++;
}
如果(滚动==7)
{
Console.WriteLine(“你输了”);
numRolls++;
游戏状态++;
}
滚动骰子();
numRolls++;
}
}
}
}
静态void Main(字符串[]参数)
{
骰子新游戏=新骰子();
Console.ReadLine();
}
}

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

在创建新游戏时调用NewGame.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();
    }
}