我目前正在开发一个项目,我们将编写一个yahtzee游戏。游戏需要滚动5个骰子(使用随机选择5个不同的骰子脸部图像)并且玩家需要能够保持骰子以便在第二和第三个滚动时如果选中相关的复选框,则骰子保持不变。我希望使用if语句来做到这一点。我如何将其合并到我的代码中?
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 dice2
{
public partial class Form1 : Form
{
Image[] DiceImages; // Image array
int[] dice; //intiger array
Random rand;
CheckBox[] boxes = new CheckBox[4];
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
DiceImages = new Image[]
{
Properties.Resources.dice_blank,
Properties.Resources.dice_1,
Properties.Resources.dice_2,
Properties.Resources.dice_3,
Properties.Resources.dice_4,
Properties.Resources.dice_5,
Properties.Resources.dice_6
};
dice = new int[5]; //dice length
rand = new Random();
}
private void btn1_Click(object sender, EventArgs e)
{
RollDice();
}
private void RollDice()
{
for (int i = 0; i < dice.Length; i++) //start at 0, i is equal to less than the dice length (5)
dice[i] = rand.Next(1, 7);//dice is edice[int] = rand.Next(1, 7);
lbl1.Image = DiceImages[dice[0]];
lbl2.Image = DiceImages[dice[1]];
lbl3.Image = DiceImages[dice[2]];
lbl4.Image = DiceImages[dice[3]];
lbl5.Image = DiceImages[dice[4]];
}
private void cb1_CheckedChanged (object sender, EventArgs e)
{
}
private void cb2_CheckedChanged(object sender, EventArgs e)
{
}
private void cb3_CheckedChanged(object sender, EventArgs e)
{
}
private void cb4_CheckedChanged(object sender, EventArgs e)
{
}
private void cb5_CheckedChanged(object sender, EventArgs e)
{
}
}
}
答案 0 :(得分:1)
在您拿一个新号码之前,请检查是否选中了这个方框:
private void RollDice()
{
for (int i = 0; i < dice.Length; i++)
if(!boxes[i].Checked)
dice[i] = rand.Next(1, 7);