【发布时间】: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