2016-10-21 3 views
-2

거푸집을 굴릴 psudo-Shut The Box 프로그램을 만들려고하는데 사용자가 해당 번호 주사위 또는 주사위의 합계로난수 생성 및 다른 함수에서 같은 번호를 여러 번 사용하는 데 도움이 필요합니다.

한 가지를 제외하고는 모든 것이 완벽하게 작동하고있는 것처럼 보입니다. 사용자 입력 선택 사항 중 하나의 버튼을 클릭하려고하면 잘못된 확인란이 선택되어 있어도 여전히 임의의 숫자가 생성되고 있음이 분명합니다.

나는이 기능을 호출하는 방법의 결과라고 생각합니다. 롤 주사위 버튼을 누른 후 다시 임의로 생성 될 수있는 반면 다른 기능에서 호출 될 경우 동일한 값을 유지하는 무언가로 주사위 1과 주사위 2의 무작위 값을 전달하는 방법이 있습니까?

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; 
      } 
     } 
    } 
+1

클래스 멤버를 만들 –

+1

게시 코드 (및 그에 따라 [편집] 게시물)에 대한 [MCVE] 지침을 읽으십시오. 또한 임의의 숫자를 올바르게 생성하는 방법을 보여주는 http://stackoverflow.com/questions/767999/random-number-generator-only-generating-one-random-number를 읽으십시오. –

답변

0

이 프로그램의 범위를 벗어나는 변수를 선언 할 수있는 한 가지 해결책이 있습니다. 즉, 공용 변수.

public int RandomValueOne; 

이제 임의 값을이 공용 변수에 저장하십시오.

private int firstDiceValue() 
{ 
    RandomValueOne = firstDiceRandom(); 
    return RandomValueOne; 
} 

이제는 숫자가 다시 생성 될 염려없이 변수를 직접 사용할 수 있습니다. 한 번 "FirstDiceValue"함수를 호출하고 전역 변수를 계속 사용하십시오.