所以我一直在研究yahtzee游戏,到目前为止我已经有了随机显示骰子图像的标签,这就是代码。
#region Declaration
Image[] diceImages;
int[] dice;
Random rand;
#endregion
#region Initialization
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
diceImages=new Image[7];
diceImages[0] = Properties.Resources.dice_face_0;
diceImages[1] = Properties.Resources.dice_face_1;
diceImages[2] = Properties.Resources.dice_face_2;
diceImages[3] = Properties.Resources.dice_face_3;
diceImages[4] = Properties.Resources.dice_face_4;
diceImages[5] = Properties.Resources.dice_face_5;
diceImages[6] = Properties.Resources.dice_face_6;
dice = new int[5] { 0, 0, 0, 0, 0 };
rand = new Random();
}
#endregion
private void btnRoll_Click(object sender, EventArgs e)
{
for (int i = 0; i < dice.Length; i++)
dice[i] = rand.Next(1, 7);
lblDie1.Image = diceImages[dice[0]];
lblDie2.Image = diceImages[dice[1]];
lblDie3.Image = diceImages[dice[2]];
lblDie4.Image = diceImages[dice[3]];
lblDie5.Image = diceImages[dice[4]];
}
我可以按数字顺序排列骰子图像,如果是,怎么做?
答案 0 :(得分:0)
完成后:
for (int i = 0; i < dice.Length; i++)
dice[i] = rand.Next(1, 7);
创建一个有序列表:
dice = dice.OrderBy(x => x).ToArray();
然后继续分配图像。
答案 1 :(得分:0)
您可以使用Array.Sort
(MSDN documentation)
private void btnRoll_Click(object sender, EventArgs e)
{
for (int i = 0; i < dice.Length; i++)
dice[i] = rand.Next(1, 7);
Array.Sort(dice);
lblDie1.Image = diceImages[dice[0]];
lblDie2.Image = diceImages[dice[1]];
lblDie3.Image = diceImages[dice[2]];
lblDie4.Image = diceImages[dice[3]];
lblDie5.Image = diceImages[dice[4]];
}