如何进步我的五个骰子游戏 [英] How to progress my Five Dice game

查看:181
本文介绍了如何进步我的五个骰子游戏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我学习方案,特别是开始的C#,我一直与骰子游戏尝试更加熟悉和提高自己。我现在想在Windows窗体,其中两名球员摇骰子5,并保持自己的得分纪录创造了基本的游戏。

规则:


  • 5骰子开始


  • 如果1或4出现,球员没有得分点,这些骰子被删除。否则,添加所有的骰子总


  • 剩余骰子继续,直到有没有骰子遗留


到目前为止,我有一个图像阵列存储我所有的骰子图像,我的资源和一个按钮,将掷骰子。我需要什么特别的是能够去除处罚骰子(设置特定的一回空白),并让玩家继续滚动自己的骰子剩余,直到没有留下帮忙。

目前,我不能确定,我可以进一步这一点,也许我贪多,我可以咀嚼。我爱编码和任何帮助将非常AP preciated。

以下是此接口的图像:

在这里输入的形象描述

在这里输入的形象描述

 公共部分Form1类:表格
{    图片[] diceImages;
    INT []骰子;
    INT [] diceResults;
    随机随机的;    公共Form1中()
    {
        的InitializeComponent();
    }    私人无效Form1_Load的(对象发件人,EventArgs的发送)
    {
        diceImages =新的图像[7];
        diceImages [0] = Properties.Resources.blank;
        diceImages [1] = Properties.Resources.one;
        diceImages [2] = Properties.Resources.two;
        diceImages [3] = Properties.Resources.three;
        diceImages [4] = Properties.Resources.four;
        diceImages [5] = Properties.Resources.five;
        diceImages [6] = Properties.Resources.six;        骰子=新INT [5] {0,0,0,0,0};        随机=新的随机();        diceResults =新INT [6] {0,0,0,0,0,0};    }    私人无效btn_rollDice_Click(对象发件人,EventArgs的发送)
    {
        RollDice();        GetResults();        ResetResults();
    }    私人无效RollDice()
    {
        的for(int i = 0; I< dice.Length;我++)
        {
            骰子[I] = random.Next(1,7);            开关(骰子[I])
            {
                情况1:
                    diceResults [0] ++;
                    打破;
                案例2:
                    diceResults [1] ++;
                    打破;
                案例3:
                    diceResults [2] ++;
                    打破;
                情况4:
                    diceResults [3] ++;
                    打破;
                情况5:
                    diceResults [4] ++;
                    打破;
                情况6:
                    diceResults [5] ++;
                    打破;            }        }        lbl_dice1.Image = diceImages [骰子[0]];
        lbl_dice2.Image = diceImages [骰子[1]];
        lbl_dice3.Image = diceImages [骰子[2]];
        lbl_dice4.Image = diceImages [骰子[3]];
        lbl_dice5.Image = diceImages [骰子[4]];
    }    私人无效GetResults()
    {
        布尔oneRoll =假,fourRoll = FALSE;        的for(int i = 0; I< diceResults.Length;我++)
        {
            如果(diceResults [I] == 1安培;&安培; diceResults [I] == 4)
            {
                oneRoll = TRUE;
                fourRoll = TRUE;
            }
        }
    }    私人无效ResetResults()
    {    }}


解决方案

在code您发布至少有几个奇怪的现象,似乎并不适合与你的描述:


  • 的code简单的递增的数组中的元素( diceResults )当给定模值被卷(即元素对应至模<青霉>值的,而不是在模具在游戏中的顺序)。从你的描述,我本来期望的code简单地添加的模值到一个总和变量。

  • GetResults()方法,你的code在 diceResults 个别元素的值进行比较值 2 5 。换句话说,对于每个可能的模值,如果这个值出现两次或五次,同时设置标志。有许多的原因,这是奇怪的,但最大的,最明显的是,一个变量(即元素 diceResults [I] )可用的从不的同时有两个不同的值。即该数组元素永远都 2 5 ,因为如果语句需要。

考虑到这些问题,我更倾向于把重点放在原规范,而不是信任code在试图理解你的意图code的行为实际上是术语太多。 :)

看来,基本的问题是如何最好地从游戏中移除模具。建议(在上面的注释),以使用列表来跟踪骰子无疑是一个可行的。在这种方法中,一会遍历列表设置每个元素,如果给定元素辊不断出来作为 1 4 ,在移动之前删除该元素。

已经这样做了,一会只是遍历列表再次设定值模图像,使用空白的形象为任何死超出了列表的长度。

但还有一个更简单的方法,并根据您的声明的设置特定的一回空白的,这似乎意味着,每个空白的模具应该出现在同一个位置,它是转了转,好像是更简单的方法可能是preferable给你。

具体而言,掷骰子后,只需通过骰子阵列扫描和重置任何 1 4 0 ,并使用该 0 值作为一个特殊的哨兵值,表明模具现在是空白的。

注意,但是你这样做(有一个列表,或者只是设定值 0 ),还有的是显示在用户的实际问题 1 4 卷,或立即将这些卷到空白模具。我要去承担前者,但它会很容易实现它,而不是其他方式。 (一如往常,code的开始是一个很好的规范&hellip;现在,你的规范是在细节上略显清淡,因而是含糊不清)。

采取这种方法,你的code可能是这样的东西更多:

 公共部分Form1类:表格
{
    #区域宣言
    图片[] diceImages;
    标签[]标签;
    INT []骰子;
    INT diceTotal;
    布尔checkOnesAndFours;
    随机随机的;
    #endregion    #区域Initialiazation;
    公共Form1中()
    {
        的InitializeComponent();
    }    私人无效Form1_Load的(对象发件人,EventArgs的发送)
    {
        //初始化数组这种方式消除了的机会
        //在分配数组索引一个错字。
        diceImages =新的图像[]
        {
            Properties.Resources.blank,
            Properties.Resources.one,
            Properties.Resources.two,
            Properties.Resources.three,
            Properties.Resources.four,
            Properties.Resources.five,
            Properties.Resources.six
        };        //数组总是以他们的默认的元素初始化
        //值,所以没有必要为`int`阵列指定`0`值明确
        骰子=新INT [5];        随机=新的随机();        diceTotal = 0;        //对于设置骰子图象的目的,这将是有帮助
        //保持在一个阵列控制引用。这是既方便
        //并再次,有助于防止印刷错误后卫
        标签=新标签[]
        {
            lbl_dice1,
            lbl_dice2,
            lbl_dice3,
            lbl_dice4,
            lbl_dice5
        };
    }    #endregion    #地区的私有方法    私人无效btn_rollDice_Click(对象发件人,EventArgs的发送)
    {
        RollDice();
    }    私人无效RollDice()
    {
        布尔rolledOneOrFour = FALSE;
        INT rollTotal = 0;        的for(int i = 0; I&LT; dice.Length;我++)
        {
            如果(checkOnesAndFours)
            {
                //首先,清除任何1或4从previous卷
                如果(骰子[I] == 1 ||骰子[I] == 4)
                {
                    骰子[I] = 0;
                }                //然后,忽略任何空白模具
                如果(骰子[I] == 0)
                {
                    继续;
                }
            }            骰子[I] = random.Next(1,7);
            如果(骰子[I] == 1 ||骰子[I] == 4)
            {
                rolledOneOrFour =真;
            }
            rollTotal + =骰子[I]
        }        如果(!rolledOneOrFour)
        {
            diceTotal + = rollTotal;
        }        checkOnesAndFours =真;        的for(int i = 0; I&LT; labels.Length;我++)
        {
            标签由[i] =图像配diceImages [骰子[I]];
        }
    }    #endregion
}

请注意:这并不完全清楚,我你的意思去做,当 1 4 登场。带你从字面上写的东西,我明白它的意思,如果的任何的模具显示了 1 4 上一滚,说的骰子无的计算该卷。以上code与记住,理解执行。

它发生,我认为你可能有,而不是意味着只有骰子,显示 1 4 不计数辊,并且该辊上的其它裸片仍然包括。它不会很难改变上述容纳,替代规范

请注意:您还会注意到Ç不是为了解决眼前的问题,在技术上要求我做其他的修改$ C $。我加了code本身的意见,试图解释为什么我做了这些变化,为什么我觉得他们做code更好。



只是咧嘴一笑,这里有一个版本,确实使用列表,而不是:

 公共部分Form1类:表格
{
    #区域宣言
    图片[] diceImages;
    标签[]标签;
    清单&LT; INT&GT;骰子;
    INT diceTotal;
    布尔checkOnesAndFours;
    随机随机的;
    #endregion    #区域Initialiazation;
    公共Form1中()
    {
        的InitializeComponent();
    }    私人无效Form1_Load的(对象发件人,EventArgs的发送)
    {
        //初始化数组这种方式消除了的机会
        //在分配数组索引一个错字。
        diceImages =新的图像[]
        {
            Properties.Resources.blank,
            Properties.Resources.one,
            Properties.Resources.two,
            Properties.Resources.three,
            Properties.Resources.four,
            Properties.Resources.five,
            Properties.Resources.six
        };        //解释必须明确与它们的初始值进行初始化,如通过缺省
        //他们最初是空的。
        骰子=新的List&LT; INT&GT;(Enumerable.Repeat(0,5));        随机=新的随机();        diceTotal = 0;        //对于设置骰子图象的目的,这将是有帮助
        //保持在一个阵列控制引用。这是既方便
        //并再次,有助于防止印刷错误后卫
        标签=新标签[]
        {
            lbl_dice1,
            lbl_dice2,
            lbl_dice3,
            lbl_dice4,
            lbl_dice5
        };
    }    #endregion    #地区的私有方法    私人无效btn_rollDice_Click(对象发件人,EventArgs的发送)
    {
        RollDice();
    }    私人无效RollDice()
    {
        布尔rolledOneOrFour = FALSE;
        INT rollTotal = 0;        的for(int i = 0; I&LT; dice.Count;我++)
        {
            //清除任何1或4从previous卷
            如果(checkOnesAndFours&放大器;及(骰子由[i] == 1 ||骰子由[i] == 4))
            {
                //取下玩这种模具
                dice.RemoveAt(ⅰ);                //下一个列表元素来检查现在是在当前I值
                //和for循环会递增我当继续
                //被执行,因此减量我在那个期待
                //使得环路上移动到正确的下一个元素
                一世 - ;                继续;
            }            骰子[I] = random.Next(1,7);
            如果(骰子[I] == 1 ||骰子[I] == 4)
            {
                rolledOneOrFour =真;
            }
            rollTotal + =骰子[I]
        }        如果(!rolledOneOrFour)
        {
            diceTotal + = rollTotal;
        }        checkOnesAndFours =真;        的for(int i = 0; I&LT; labels.Length;我++)
        {
            标签由[i] =图像配I&LT; dice.Count? diceImages [骰子[Ⅰ]:diceImages [0];
        }
    }    #endregion
}



最后,需要注意的是,无论上述地址的一对夫妇与code的其他问题:


  • 在游戏开始时初始化骰子标签。

  • 重置整个游戏一旦游戏结束(即有没有更多的骰子左)。

在我离开这两个项目作为练习读者(当然,如果你遇到这些具体问题的问题,你总是可以张贴询问每个堆栈溢出另一个问题专门)。

I am learning to program, in particular starting with C# where I have been experimenting with dice games to become more familiar and to improve myself. I am now trying to create a basic game in windows forms, where two players roll 5 dice and keep record of their score.

Rules:

  • Begin with 5 dice

  • If a 1 or 4 appears, player scores no points and those dice are removed. Otherwise add all the dice to total

  • Continue with remaining dice until there a no dice left over

So far I have an Image array which stores all my dice images in my resources and a button which will roll the dice. What I need help with specifically is being able to remove the penalized dice (setting that particular one back to blank) and allowing the player to continue rolling their remaining dice until none are left.

At the moment I am unsure as to where I can further this and maybe I am biting off more than I can chew. I am loving coding and any help would be much appreciated.

Here's an image of the interface:

public partial class Form1 : Form
{

    Image[] diceImages;
    int[] dice;
    int[] diceResults;
    Random random;

    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        diceImages = new Image[7];
        diceImages[0] = Properties.Resources.blank;
        diceImages[1] = Properties.Resources.one;
        diceImages[2] = Properties.Resources.two;
        diceImages[3] = Properties.Resources.three;
        diceImages[4] = Properties.Resources.four;
        diceImages[5] = Properties.Resources.five;
        diceImages[6] = Properties.Resources.six;

        dice = new int[5] { 0, 0, 0, 0, 0 };

        random = new Random();

        diceResults = new int[6] { 0, 0, 0, 0, 0, 0 };

    }

    private void btn_rollDice_Click(object sender, EventArgs e)
    {
        RollDice();

        GetResults();

        ResetResults();
    }

    private void RollDice()
    { 
        for (int i = 0; i < dice.Length; i++)
        {
            dice[i] = random.Next(1, 7);

            switch (dice[i])
            { 
                case 1:
                    diceResults[0]++;
                    break;
                case 2:
                    diceResults[1]++;
                    break;
                case 3:
                    diceResults[2]++;
                    break;
                case 4:
                    diceResults[3]++;
                    break;
                case 5:
                    diceResults[4]++;
                    break;
                case 6:
                    diceResults[5]++;
                    break;

            }

        }

        lbl_dice1.Image = diceImages[dice[0]];
        lbl_dice2.Image = diceImages[dice[1]];
        lbl_dice3.Image = diceImages[dice[2]];
        lbl_dice4.Image = diceImages[dice[3]];
        lbl_dice5.Image = diceImages[dice[4]];
    }

    private void GetResults()
    {
        bool oneRoll = false, fourRoll = false;

        for (int i = 0; i < diceResults.Length; i++)
        {
            if (diceResults[i] == 1 && diceResults[i] == 4)
            {
                oneRoll = true;
                fourRoll = true;
            }
        }
    }

    private void ResetResults()
    {

    }

}

解决方案

The code you posted has at least a couple of oddities that don't seem to fit with your description:

  • The code simply increments an element in an array (diceResults) when a given die value is rolled (i.e. the element corresponds to the die value, not the die's order in the game). From your description, I would have expected the code to simply add the die value to a single sum variable.
  • In your GetResults() method, your code compares the individual element values in the diceResults to the values 2 and 5. In other words, for each possible die value, if that value comes up twice or five times, you set both flags. There are a number of reasons this is strange, but the biggest, most obvious one is that a single variable (i.e. the element diceResults[i]) can never have two different values at the same time. I.e. that array element will never be both 2 and 5, as the if statement requires.

Given those issues, I'm more inclined to focus on the original specification than to trust the code too much in terms of trying to understand what your intended behavior of the code actually is. :)

It seems that the basic question is how best to remove die from play. The suggestion (in the comments above) to use a list to track the dice is certainly a workable one. In that approach, one would iterate through the list to set each element, and if the roll for a given element ever comes up as 1 or 4, remove that element before moving on.

Having done that, one would just iterate through the list again to set the die value images, using the "blank" image for any die beyond the length of the list.

But there is a simpler way, and based on your statement "setting that particular one back to blank", which seems to imply that each blank die should appear in the same position in which it was rolled, it seems like that simpler way might be preferable to you.

Specifically, after rolling the dice, just scan through the dice array and reset any 1 and 4 values to 0, and use this 0 value as a special "sentinel" value to indicate that die is now blank.

Note that however you do this (with a list, or just setting values to 0), there is also the question of whether to show the user the actual 1 and 4 rolls, or to immediately set those rolls to a blank die. I'm going to assume the former, but it would be very easy to implement it the other way instead. (As always, the beginning of good code is a good specification…right now, your specification is a bit light on detail, and thus is vague and ambiguous).

Taking that approach, your code might look something more like this:

public partial class Form1 : Form
{
    #region Declaration
    Image[] diceImages;
    Label[] labels;
    int[] dice;
    int diceTotal;
    bool checkOnesAndFours;
    Random random;
    #endregion

    #region Initialiazation;
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        // Initializing an array this way eliminates the chance of having
        // a typo in the array index for the assignment.
        diceImages = new Image[]
        {
            Properties.Resources.blank,
            Properties.Resources.one,
            Properties.Resources.two,
            Properties.Resources.three,
            Properties.Resources.four,
            Properties.Resources.five,
            Properties.Resources.six
        };

        // Arrays are always initialized with the elements having their default
        // values, so there's no need to specify `0` values for `int` arrays explicitly
        dice = new int[5];

        random = new Random();

        diceTotal = 0;

        // For the purposes of setting the dice images, it will be helpful
        // to keep the control references in an array. This is both convenient
        // and, again, helps guard against typographical errors
        labels = new Label[]
        {
            lbl_dice1,
            lbl_dice2,
            lbl_dice3,
            lbl_dice4,
            lbl_dice5
        };
    }

    #endregion

    #region Private Methods

    private void btn_rollDice_Click(object sender, EventArgs e)
    {
        RollDice();
    }

    private void RollDice()
    {
        bool rolledOneOrFour = false;
        int rollTotal = 0;

        for (int i = 0; i < dice.Length; i++)
        {
            if (checkOnesAndFours)
            {
                // First, clear any 1 or 4 from the previous roll
                if (dice[i] == 1 || dice[i] == 4)
                {
                    dice[i] = 0;
                }

                // Then, ignore any blank die
                if (dice[i] == 0)
                {
                    continue;
                }
            }

            dice[i] = random.Next(1, 7);
            if (dice[i] == 1 || dice[i] == 4)
            {
                rolledOneOrFour = true;
            }
            rollTotal += dice[i];
        }

        if (!rolledOneOrFour)
        {
            diceTotal += rollTotal;
        }

        checkOnesAndFours = true;

        for (int i = 0; i < labels.Length; i++)
        {
            labels[i].Image = diceImages[dice[i]];
        }
    }

    #endregion
}

NOTE: it was not entirely clear to me what you mean to do when a 1 or 4 comes up. Taking what you wrote literally, I understand it to mean that if any die shows a 1 or 4 on a roll, that none of the dice count for that roll. The above code is implemented with that understanding in mind.

It occurs to me that you might have instead meant that only the dice that show 1 or 4 are not counted for the roll, and that the other dice for that roll are still included. It would not be hard to change the above to accommodate that alternative specification.

NOTE: you'll also notice that I have made other changes to the code not technically required in order to address the immediate question. I added comments in the code itself to try to explain why I made those changes, and why I feel they make the code better.


Just for grins, here's a version that does use a list instead:

public partial class Form1 : Form
{
    #region Declaration
    Image[] diceImages;
    Label[] labels;
    List<int> dice;
    int diceTotal;
    bool checkOnesAndFours;
    Random random;
    #endregion

    #region Initialiazation;
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        // Initializing an array this way eliminates the chance of having
        // a typo in the array index for the assignment.
        diceImages = new Image[]
        {
            Properties.Resources.blank,
            Properties.Resources.one,
            Properties.Resources.two,
            Properties.Resources.three,
            Properties.Resources.four,
            Properties.Resources.five,
            Properties.Resources.six
        };

        // Lists must be initialized explicitly with their initial values, as by default
        // they are initially empty.
        dice = new List<int>(Enumerable.Repeat(0, 5));

        random = new Random();

        diceTotal = 0;

        // For the purposes of setting the dice images, it will be helpful
        // to keep the control references in an array. This is both convenient
        // and, again, helps guard against typographical errors
        labels = new Label[]
        {
            lbl_dice1,
            lbl_dice2,
            lbl_dice3,
            lbl_dice4,
            lbl_dice5
        };
    }

    #endregion

    #region Private Methods

    private void btn_rollDice_Click(object sender, EventArgs e)
    {
        RollDice();
    }

    private void RollDice()
    {
        bool rolledOneOrFour = false;
        int rollTotal = 0;

        for (int i = 0; i < dice.Count; i++)
        {
            // Clear any 1 or 4 from the previous roll
            if (checkOnesAndFours && (dice[i] == 1 || dice[i] == 4))
            {
                // Remove this die from play
                dice.RemoveAt(i);

                // The next list element to examine is now at the current i value
                // and the for loop is going to increment i when the continue
                // is executed, so decrement i in anticipation of that
                // so that the loop moves on to the correct next element
                i--;

                continue;
            }

            dice[i] = random.Next(1, 7);
            if (dice[i] == 1 || dice[i] == 4)
            {
                rolledOneOrFour = true;
            }
            rollTotal += dice[i];
        }

        if (!rolledOneOrFour)
        {
            diceTotal += rollTotal;
        }

        checkOnesAndFours = true;

        for (int i = 0; i < labels.Length; i++)
        {
            labels[i].Image = i < dice.Count ? diceImages[dice[i]] : diceImages[0];
        }
    }

    #endregion
}


Finally, note that neither of the above addresses a couple of other issues with the code:

  • Initializing the dice labels at the beginning of a game.
  • Resetting the entire game once the game has ended (i.e. there are no more dice left).

I leave these two items as an exercise for the reader (of course, if you run into problems with those specific issues, you may always post another question on Stack Overflow asking about each specifically).

这篇关于如何进步我的五个骰子游戏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆