通过引用 Rolling Dice GUI C# 传递的参数

Parameters passed by reference Rolling Dice GUI C#

我已经开始工作了,但我正在尝试找到一种更简单的方法。

我需要一个显示两个骰子图片的程序,我需要一个 Class,并且至少有一个方法可以正确使用通过引用传递的参数。

我在我的 class 中使用两个通过引用传递的参数获得了我的 GetRoll 方法,但我能够实现此功能的唯一方法是编写大量的 if else 语句。必须有更好的方法。有任何想法吗? 这是我的表格:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DiceGame
{
    public partial class Form1 : Form
    {
        DiceClass objectRef;
        public Form1()
        {
            InitializeComponent();
            objectRef = new DiceClass();
        }    
        private void rollEm_Click(object sender, EventArgs e)
        {
            specialMessage.Text = "";
            objectRef.RollEm();
            string str1 = "";
            string str2 = "";
            objectRef.GetRoll(ref str1, ref str2);
            die1.Text = str1;
            die2.Text = str2;
            if (objectRef.BoxCars())
            {
                specialMessage.Text = "BOX CARS!!";
            }
            else
            {
                if (!objectRef.SnakeEyes())
                    return;
                specialMessage.Text = "SNAKE EYES!!";
            }
        }
    }
}

这是我的 class:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DiceGame
{
    class DiceClass
    {
        private static string nL = Environment.NewLine;
        string one = nL + " l ";
        string two = "l" + nL + nL + "  l";
        string three = "l l" + nL + nL + "l l";
        string four = "l l" + nL + nL + "l l";
        string five = "l l" + nL + " l " + nL + "l l";
        string six = "l l" + nL + "l l" + nL + "l l";
        private const int BOX = 6;
        private int firstDie;
        private int secondDie;
        Random randomNums = new Random();
        public DiceClass()
        {
            firstDie = 0;
            secondDie = 0;
        }

        public void RollEm()
        {
            firstDie = randomNums.Next(1, 7);
            secondDie = randomNums.Next(1, 7);
        }
        public bool BoxCars()
        {
            return firstDie == 6 && secondDie == 6;
        }

        public bool SnakeEyes()
        {
            return firstDie == 1 && secondDie == 1;
        }
        // is there an easier way to have this method work without all these if else statements??
        public void GetRoll(ref string first, ref string second)
        {
            if (firstDie == 1 && secondDie == 1)
            {
                first = one;
                second = one;
            }
            else if (firstDie == 1 && secondDie == 2)
            {
                first = one;
                second = two;
            }
            else if (firstDie == 1 && secondDie == 3)
            {
                first = one;
                second = three;
            }
            else if (firstDie == 1 && secondDie == 4)
            {
                first = one;
                second = four;
            }
            else if (firstDie == 1 && secondDie == 5)
            {
                first = one;
                second = five;
            }
            else if (firstDie == 1 && secondDie == 6)
            {
                first = one;
                second = six;
            }
            else if (firstDie == 2 && secondDie == 1)
            {
                first = two;
                second = one;
            }
            else if (firstDie == 2 && secondDie == 2)
            {
                first = two;
                second = two;
            }
            else if (firstDie == 2 && secondDie == 3)
            {
                first = two;
                second = three;
            }
            else if (firstDie == 2 && secondDie == 4)
            {
                first = two;
                second = four;
            }
            else if (firstDie == 2 && secondDie == 5)
            {
                first = two;
                second = five;
            }
            else if (firstDie == 2 && secondDie == 6)
            {
                first = two;
                second = six;
            }
            else if (firstDie == 3 && secondDie == 1)
            {
                first = three;
                second = one;
            }
            else if (firstDie == 3 && secondDie == 2)
            {
                first = three;
                second = two;
            }
            else if (firstDie == 3 && secondDie == 3)
            {
                first = three;
                second = three;
            }
            else if (firstDie == 3 && secondDie == 4)
            {
                first = three;
                second = four;
            }
            else if (firstDie == 3 && secondDie == 5)
            {
                first = three;
                second = five;
            }
            else if (firstDie == 3 && secondDie == 6)
            {
                first = three;
                second = six;
            }
            else if (firstDie == 4 && secondDie == 1)
            {
                first = four;
                second = one;
            }
            else if (firstDie == 4 && secondDie == 2)
            {
                first = four;
                second = two;
            }
            else if (firstDie == 4 && secondDie == 3)
            {
                first = four;
                second = three;
            }
            else if (firstDie == 4 && secondDie == 4)
            {
                first = four;
                second = four;
            }
            else if (firstDie == 4 && secondDie == 5)
            {
                first = four;
                second = five;
            }
            else if (firstDie == 4 && secondDie == 6)
            {
                first = four;
                second = six;
            }
            else if (firstDie == 5 && secondDie == 1)
            {
                first = five;
                second = one;
            }
            else if (firstDie == 5 && secondDie == 2)
            {
                first = five;
                second = two;
            }
            else if (firstDie == 5 && secondDie == 3)
            {
                first = five;
                second = three;
            }
            else if (firstDie == 5 && secondDie == 4)
            {
                first = five;
                second = four;
            }
            else if (firstDie == 5 && secondDie == 5)
            {
                first = five;
                second = five;
            }
            else if (firstDie == 5 && secondDie == 6)
            {
                first = five;
                second = six;
            }
            else if (firstDie == 6 && secondDie == 1)
            {
                first = six;
                second = one;
            }
            else if (firstDie == 6 && secondDie == 2)
            {
                first = six;
                second = two;
            }
            else if (firstDie == 6 && secondDie == 3)
            {
                first = six;
                second = three;
            }
            else if (firstDie == 6 && secondDie == 4)
            {
                first = six;
                second = four;
            }
            else if (firstDie == 6 && secondDie == 5)
            {
                first = six;
                second = five;
            }
            else
            {
                first = six;
                second = six;
            }
        }
    }
}

使用字典定义映射一次:

Dictionary<int, string> dieRapping = new Dictionary<int, string>() {
    { 1, nL + " l " },
    { 2, "l" + nL + nL + "  l" },
    { 3, "l l" + nL + nL + "l l" },
    { 4, "l l" + nL + nL + "l l" },
    { 5, "l l" + nL + " l " + nL + "l l" },
    { 6, "l l" + nL + "l l" + nL + "l l" }
};

现在只需使用 dieRapping[someInteger] 即可在常数时间内将整数转换为字符串。所以你的整个 GetRoll 方法变成了这样:

public void GetRoll(ref string first, ref string second)
{
    first = dieRapping[firstDie];
    second = dieRapping[secondDie];
}

请注意,即使没有映射,如果您单独处理 firstsecond 也可以节省很多。由于它们不依赖于另一个,您可以先处理 first,然后再处理 second:

if (firstDie == 1)
    first = one;
else if (firstDie == 2)
    first = two;
else …

if (secondDie == 1)
    second = one;
else if (secondDie == 2)
    second = two;
else …

或者,由于 firstsecond 的逻辑相同,您可以引入另一种方法:

public string GetSingleRoll(int value)
{
    if (value == 1)
        return one;
    else if (value == 2)
        return two;
    else …
}

然后您可以在 GetRoll:

中调用该方法两次
first = GetSingleRoll(firstDie);
second = GetSingleRoll(secondDie);

但这只是减少重复的一些方法。在您的情况下,使用映射可能是最好的解决方案。

// is there an easier way to have this method work without all these if else statements??

是:

class DiceClass
{
    private static string nL = Environment.NewLine;
    List<string> vals = new List<string>
    {
        nL + " l ",
        "l" + nL + nL + "  l",
        "l l" + nL + nL + "l l",
        "l l" + nL + nL + "l l",
        "l l" + nL + " l " + nL + "l l",
        "l l" + nL + "l l" + nL + "l l"
    };
    private const int BOX = 6;
    private int firstDie;
    private int secondDie;
    Random randomNums = new Random();
    public DiceClass()
    {
        firstDie = 0;
        secondDie = 0;
    }

    public void RollEm()
    {
        firstDie = randomNums.Next(1, 7);
        secondDie = randomNums.Next(1, 7);
    }
    public bool BoxCars()
    {
        return firstDie == 6 && secondDie == 6;
    }

    public bool SnakeEyes()
    {
        return firstDie == 1 && secondDie == 1;
    }

    public void GetRoll(ref string first, ref string second)
    {
        //str1 = GenerateString(numOne);
        //str2 = GenerateString(numTwo);

        first = vals[firstDie < 1 ? vals.Count - 1 : firstDie - 1];
        second = vals[secondDie < 1 ? vals.Count - 1 : secondDie - 1];
    }
}

上面的代码实际上是将你的值保存在一个列表List<string> vals中,然后将它们适当的值赋给GetRollfirstsecond参数。[=18] =]

有关此语法的详细信息firstDie < 1 ? vals.Count - 1 : firstDie - 1?: Operator (C# Reference)