【问题标题】:Parameters passed by reference Rolling Dice GUI C# [closed]通过引用 Rolling Dice GUI C# 传递的参数 [关闭]
【发布时间】:2016-07-14 11:35:45
【问题描述】:

我已经完成了这项工作,但我正在尝试找到一种更简单的方法来做到这一点。

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

我在我的类中使用了两个通过引用传递的参数的 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!!";
            }
        }
    }
}

这是我的课:

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;
            }
        }
    }
}

【问题讨论】:

  • 将你的骰子表示存储在一个字符串数组中,并使用滚动的值作为数组中的索引

标签: c# user-interface reference pass-by-reference dice


【解决方案1】:

使用字典来定义你的映射一次

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);

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

【讨论】:

  • 谢谢戳!我不敢相信我没有看到您独立治疗第一和第二的建议。你回答了我的问题!
【解决方案2】:

//有没有更简单的方法让这个方法在没有所有这些 if else 语句的情况下工作??

是的:

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&lt;string&gt; vals中,然后将它们的适当值分配给GetRollfirstsecond参数。

有关此语法firstDie &lt; 1 ? vals.Count - 1 : firstDie - 1 的更多信息: ?: Operator (C# Reference)

【讨论】:

  • 谢谢大家!实际上,我确实已经想到了一个数组。我可能应该提到这个原始问题,但实际上我一直在尝试避免使用数组。原因是因为这是我大学班级的一个项目,我们还没有学过数组。因此,我试图在我们在本课程中讨论过的范围内找到一种方法来做到这一点。但是使用字典非常酷!我以前没见过,但这也超出了我们所学的范围。还有其他想法吗?还是我的老师实际上是在寻找 36 个 if else 语句?
猜你喜欢
  • 2016-02-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-15
  • 2014-09-11
  • 1970-01-01
  • 2012-06-04
相关资源
最近更新 更多