1
我目前正在開發一個項目,我們要編寫一個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)
{
}
}
}