2011-02-21 6 views
1

皆さん、私はプロジェクト、try catchブロックに問題があります。真スキップホールドチェックとは、すべてのサイコロを転がした場合ダイスローリングで問題をキャッチしようとしました。チェックボックスの問題

falseの場合、以下の(「ロック」何とか、新たに開催されたサイコロを(試してみてください、第1ロールのための

まずチェック)AND:私は、次の操作を行うためにそれを必要としますロールunheldサイコロ)

何のサイコロが開催されていない場合は、例外

を投げる私は私のコードを添付しています。

Random = random.Next(0, 6); 
diceImages[i].Image = dieImages[Random]; 

dieImagesが本当にdiceImagesあるべきと仮定すると、(そうでない場合はコンパイルされません) 使用している:ので、このコードには多くの問題があります誰かが

public partial class FrmBupkis1 : Form 
{ 
    private PictureBox[] diceImages; 
    private CheckBox[] holds; 
    private Random rnd = new Random(); 


    public Frm1() 
    { 
     InitializeComponent(); 
     diceImages = new PictureBox[6]; 
     diceImages[0] = pbxDie0; 
     diceImages[1] = pbxDie1; 
     diceImages[2] = pbxDie2; 
     diceImages[3] = pbxDie3; 
     diceImages[4] = pbxDie4; 
     diceImages[5] = pbxDie5; 

     holds = new CheckBox[6]; 
     holds[0] = chbHold0; 
     holds[1] = chbHold1; 
     holds[2] = chbHold2; 
     holds[3] = chbHold3; 
     holds[4] = chbHold4; 
     holds[5] = chbHold5; 
    } 

    private void rollBtn_Click(object sender, EventArgs e) 
    { 

     //First Check for first roll, if true skip hold checks and roll all dice 
     //If false, Try the following ('lock' newly held dice (somehow) AND roll unheld dice) 
     //If no dice are held, throw exception 


      for (int i = 0; i < 6; i++) 
      { 
       //if die is not held, then assign random number to image box 
       if (holds[i].Checked == false) 
        diceImages[i].Image = iglDice.Images[rnd.Next(6)]; 

       try 
       { 
        Random = random.Next(0, 6); 
        diceImages[i].Image = dieImages[Random]; 
        rollBtn = true; 
       } 

       catch (FormatException ex) 
       { 
        Console.WriteLine(ex.Message, "Error. Try Again."); 
       } 

      } 

     } 

    private void gameOverBtn_Click(object sender, EventArgs e) 
    { 
     for (int i = 0; i < 6; i++) 
     { 

      diceImages[i].Image = null; 
      holds[i].Checked = false; 
      holds[i].Enabled = true; 
     } 
    } 


    private void quitBtn_Click(object sender, EventArgs e) 
    { 
     //btnQuit 
     this.Close(); 

    } 
} 
+0

なぜ各ダイを2回「リロール」していますか? –

答えて

1

....助けることができる私を失いましたよ.NETクラスと同じ名前の変数 - 混乱します。また、この変数はどこに宣言されていないようだ - ちょうどあなたが世界的に直接定義されているランダムなインスタンスを使用します。

diceImages[i].Image = diceImages[rnd.Next(6)]; 

また、あなたは0から6までの範囲を使用していた - 取得するには、単一のパラメータ法Nextを使用します上記のように0〜5の範囲(上限は排他的)です。そうしないと、最終的にdiceImagesにアクセスするとIndexOutofRangeExceptionが返されます。

1

私はあなたが例外をキャッチしようとしているが、私はあなたが実際に例外を投げている、あなたのコード内の任意の場所が表示されていない場所を確認。例外をスローすると、次のようになります。

throw new ApplicationException("No dice are held"); 

または、独自の例外を作成して代わりにスローすることができます。例外は次のようになります:

class NoDiceHeldException : ApplicationException 
{ 
    // define constructors here 
} 

、その後、あなたは次のように例外をスローする可能性:まだ非常に混乱してどのように...

throw new NoDiceHeldException(); 
+0

ここに私が今持っているのは.... – glogurl

+0

だから、私は今何を持っているのですか...あなたはそれを見て、空白を埋めるのを助けることができますか? – glogurl

1

ので、私は以下のこの私のトライキャッチ投げ用を持っています私はまだあなたがやろうと、とにかく助けしようとしているかについて混乱しています....ぐふ....

  for (int i = 0; i < 6; i++) 
      { 
       //if die is not held, then assign random number to image box 
       try 
       { 
        if (holds[i].Checked == false) 
         diceImages[i].Image = iglDice.Images[rnd.Next(6)]; 

        if (holds[i].Checked == true) 
        { 
         //HERE IS WHER I NEED IT TO HOLD THE DICE IF THEY ARE CHECK AND ONLY ROLL THE REMAINING DICE. UGH I'M STILL LOST... 
        } 

        throw new ApplicationException("No dice are held"); 

       } 

       catch (Exception ex) 
       { 
        MessageBox.Show("You must hold at least 1 scoring dice before rolling", "Format Error"); 
       } 

      } 
1

を作業する必要があります。ループを0から5まで進めていくうちに、項目のANYが保持されているかどうかを追跡する必要があります。ループを終了した後(つまりその下)に例外をスローしたいとします。たとえば、コードを実行すると、例外が6回スローされてしまいます。

私はtry/catchループがループの外にあるべきだと思います。また、このようにする必要があります:サイコロ(あなたがまだ不足しているコードがある場所)をロックするには

try 
{ 
    bool foundHeldDie = false; 

    for (int i = 0; i < 6; ++i) 
    { 
     // your code, which sets foundHeldDie to true if appropriate 
     if (holds[i].Checked == true) 
     { 
      foundHeldDie = true; 
      holds[i].Enabled = false; 
     } 
    } 

    if (!foundHeldDie) 
    { 
     // throw the exception 
    } 
} 
catch (Exception ex) 
{ 
    // handle the exception 
} 

、このチェックボックスを無効にすることができ(falseにEnabledプロパティを設定し)、ユーザーはならない、そのようそれを変更することができます。

+0

だから私は何を持っているのか、今何をしているのかは、最初のターンですべてのダイスをロールすることが許されているなら、それは任意のダイスを保持するだろうが、ダイスが選択されていなければ例外メッセージを投げない。私の更新プログラムで自分のコードを投稿しました。 – glogurl

+0

psは、ダーンのゲームが何をすべきかへのリンクを得ています。スコアリングも何もサイコロが保持しているものはありません。http://www.farklerules.com/play-online/ – glogurl

+0

あなたは、例外。 forループの後にif節に戻すことができますか?また、foundHeldDie = trueを設定する必要があります。最後に、Enabled = trueと設定すると、フォームが有効に設定されます。チェックボックスを無効にする必要があります:hold [i] .Enabled = false。 –

1
public partial class FrmBupkis1 : Form 
{ 
    private PictureBox[] diceImages; 
    private CheckBox[] holds; 
    private Random rnd = new Random(); 


    public FrmBupkis1() 
    { 
     InitializeComponent(); 
     diceImages = new PictureBox[6]; 
     diceImages[0] = pbxDie0; 
     diceImages[1] = pbxDie1; 
     diceImages[2] = pbxDie2; 
     diceImages[3] = pbxDie3; 
     diceImages[4] = pbxDie4; 
     diceImages[5] = pbxDie5; 

     holds = new CheckBox[6]; 
     holds[0] = chbHold0; 
     holds[1] = chbHold1; 
     holds[2] = chbHold2; 
     holds[3] = chbHold3; 
     holds[4] = chbHold4; 
     holds[5] = chbHold5; 
    } 

    private void rollBtn_Click(object sender, EventArgs e) 
    { 

     //First Check for first roll, if true skip hold checks and roll all dice 
     //If false, Try the following ('lock' newly held dice (somehow) AND roll unheld dice) 
     //If no dice are held, throw exception 

      for (int i = 0; i < 6; i++) 
     { 
      //if die is not held, then assign random number to image box 
      if (holds[i].Checked == false) 
       diceImages[i].Image = iglDice.Images[rnd.Next(6)]; 
     } 


     try 
     { 
      bool foundHeldDie = false; 

      for (int i = 0; i < 6; ++i) 
      { 
       // your code, which sets foundHeldDie to true if appropriate 
       if (holds[i].Checked == true) 
       { 
        foundHeldDie = true; 
        holds[i].Enabled = false; 
       } 
      } 

      if (!foundHeldDie) 
      { 
       // throw the exception 
       throw new ApplicationException("No dice are held"); 

      } 
     } 

     catch (Exception ex) 
     { 
      // handle the exception 
      MessageBox.Show("You must hold at least one die before rolling.", "Error"); 

     } 
     } 

    private void gameOverBtn_Click(object sender, EventArgs e) 
    { 
     for (int i = 0; i < 6; i++) 
     { 

      diceImages[i].Image = null; 
      holds[i].Checked = false; 
      holds[i].Enabled = true; 
     } 
    } 


    private void quitBtn_Click(object sender, EventArgs e) 
    { 
     //btnQuit 
     this.Close(); 

    } 
} 
関連する問題