J'essaie de créer un programme psudo-Shut The Box qui lancera les dés et l'utilisateur aura la possibilité de choisir entre couvrir le nombre correspondant à la fois les dés ou la somme des dés.C# - Besoin d'aide pour générer un nombre aléatoire et utiliser le même nombre plusieurs fois dans d'autres fonctions
Il semble que tout fonctionne parfaitement jusqu'à présent, sauf pour une chose. Lorsque j'essaie de cliquer sur le bouton pour l'un des choix d'entrée utilisateur, il est clair qu'il génère toujours des nombres aléatoires en raison de cases à cocher incorrectes en cours de vérification.
Je suppose que c'est le résultat de la façon dont j'appelle les fonctions. Existe-t-il un moyen de passer la valeur aléatoire de dés un et de dés deux dans quelque chose qui restera le même s'il est appelé dans d'autres fonctions tout en pouvant être généré de nouveau au hasard après avoir appuyé sur le bouton de dé?
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;
}
}
}
make member member –
Veuillez lire les instructions [MCVE] sur le code de publication (et [modifier] publier en conséquence). Assurez-vous également de lire http://stackoverflow.com/questions/767999/random-number-generator-only-generating-one-random-number qui montre comment générer correctement des nombres aléatoires. –