我试图制作一个psudo-Shut The Box程序,它将掷骰子,用户可以选择覆盖对应于骰子的数字或骰子的总和。
到目前为止,似乎所有事情都完美无缺,除了一件事。当我尝试单击任一用户输入选项的按钮时,很明显由于选中了错误的复选框,它仍然会生成随机数。
我怀疑这是我调用函数的结果。有没有办法将骰子中的随机值传递给骰子两个,如果在其他函数中调用则保持相同,同时在按下掷骰子按钮后能够再次随机生成?
private int firstDiceRandom()
{
Random diceOne = new Random();
int oneDie = diceOne.Next(1, 7);
System.Threading.Thread.Sleep(100);
return oneDie;
}
// Set value of Random number to a value
private int firstDiceValue()
{
int value = firstDiceRandom();
return value;
}
// Random number for Dice Two
private int secondDiceRandom()
{
Random diceTwo = new Random();
int twoDie = diceTwo.Next(1, 7);
return twoDie;
}
// Set value of Random number to a value
private int secondDiceValue()
{
int value = secondDiceRandom();
return value;
}
// Set value of Total random numbers
private int totalDice()
{
int totalDice = firstDiceValue() + secondDiceValue();
return totalDice;
}
// Check if boxes 7-12 are checked
private bool bothDiceBool()
{
if (checkBox7.Checked && checkBox8.Checked && checkBox9.Checked && checkBox10.Checked && checkBox11.Checked && checkBox12.Checked)
{
return false;
}
return true;
}
// Roll Dice Button and Generation of dice pictures for values.
private void rollDiceButton_Click(object sender, EventArgs e)
{
int oneDie = firstDiceValue();
int twoDie = secondDiceValue();
switch (oneDie)
{
case 1:
diceOneImage.Image = new Bitmap(@"location");
break;
case 2:
diceOneImage.Image = new Bitmap(@"location");
break;
case 3:
diceOneImage.Image = new Bitmap(@"location");
break;
case 4:
diceOneImage.Image = new Bitmap(@"location");
break;
case 5:
diceOneImage.Image = new Bitmap(@"location");
break;
case 6:
diceOneImage.Image = new Bitmap(@"location");
break;
}
//If 7-12 are all checked, not supposed to run, sends a blank picture where the die would be.
if (bothDiceBool() == true)
{
switch (twoDie)
{
case 1:
diceTwoImage.Image = new Bitmap(@"location");
break;
case 2:
diceTwoImage.Image = new Bitmap(@"location");
break;
case 3:
diceTwoImage.Image = new Bitmap(@"location");
break;
case 4:
diceTwoImage.Image = new Bitmap(@"location");
break;
case 5:
diceTwoImage.Image = new Bitmap(@"location");
break;
case 6:
diceTwoImage.Image = new Bitmap(@"location");
break;
}
}
// Blank die picture
else
{
diceTwoImage.Image = new Bitmap(@"location");
}
}
// User choice button
private void button2_Click(object sender, EventArgs e)
{
int buttonOneTotal = totalDice();
// If Choice One selected when button pressed
if (radioButton1.Checked)
{
switch (buttonOneTotal)
{
case 2:
checkBox2.Checked = true;
radioButton1.Checked = false;
break;
case 3:
checkBox3.Checked = true;
radioButton1.Checked = false;
break;
case 4:
checkBox4.Checked = true;
radioButton1.Checked = false;
break;
case 5:
checkBox5.Checked = true;
radioButton1.Checked = false;
break;
case 6:
checkBox6.Checked = true; radioButton1.Checked = false;
break;
case 7:
checkBox7.Checked = true;
radioButton1.Checked = false;
break;
case 8:
checkBox8.Checked = true;
radioButton1.Checked = false;
break;
case 9:
checkBox9.Checked = true;
radioButton1.Checked = false;
break;
case 10:
checkBox10.Checked = true;
radioButton1.Checked = false;
break;
case 11:
checkBox11.Checked = true;
radioButton1.Checked = false;
break;
case 12:
checkBox12.Checked = true;
radioButton1.Checked = false;
break;
}
}
// If Choice two selected when button pressed
else if (radioButton2.Checked)
{
int buttonTwoOne = firstDiceValue();
int buttonTwoTwo = secondDiceValue();
// First Die
switch (buttonTwoOne)
{
case 1:
checkBox1.Checked = true;
radioButton2.Checked = false;
break;
case 2:
checkBox2.Checked = true;
radioButton2.Checked = false;
break;
case 3:
checkBox3.Checked = true;
radioButton2.Checked = false;
break;
case 4:
checkBox4.Checked = true;
radioButton2.Checked = false;
break;
case 5:
checkBox5.Checked = true;
radioButton2.Checked = false;
break;
case 6:
checkBox6.Checked = true;
radioButton2.Checked = false;
break;
}
// Second Die
switch (buttonTwoTwo)
{
case 1:
checkBox1.Checked = true;
radioButton1.Checked = false;
break;
case 2:
checkBox2.Checked = true;
radioButton1.Checked = false;
break;
case 3:
checkBox3.Checked = true;
radioButton1.Checked = false;
break;
case 4:
checkBox4.Checked = true;
radioButton1.Checked = false;
break;
case 5:
checkBox5.Checked = true;
radioButton1.Checked = false;
break;
case 6:
checkBox6.Checked = true;
radioButton1.Checked = false;
break;
}
}
}
答案 0 :(得分:0)
一个可以帮助您的案例的解决方案是声明一个超出此程序范围的变量。换句话说,是一个公共变量。
public int RandomValueOne;
现在您要做的是将随机值存储在此公共变量中。
private int firstDiceValue()
{
RandomValueOne = firstDiceRandom();
return RandomValueOne;
}
现在,您可以直接使用您的变量,而无需担心数字重新生成。打电话给你的功能" FirstDiceValue" ONCE并继续使用全局变量isntead