2017-09-07 2 views
-2

あなたがアメリカ出身で、クラッカーバレルに行ったことがあるなら、おそらくボードゲームをしたことがあります。 1つだけ残っています。ちょうどピラミッドや三角形の中国チェッカーに似ています。C#、1つの方法を開始する2つのボタンに問題がある

ボタンを作成してフォームに追加するフォームがあり、フォーム上でのジャンプの仕組みに関するすべてのルールを持つ "TheBoard"クラスがあります。私のフォームでは、これをすべて実行する必要があるボタンクリッカーメソッドもあります。

私はレンガの壁に当たったようです。ボードクラスのifステートメント全体を移動するために、2回目のクリックを受け入れるようにするというロジックを理解できません。ボードクラスのmoveメソッドのパラメータにはint xがあり、これはパラメータとしてクリックしたボタンです。私は動きの後半が欠けているように感じる。どのように私は2つのボタンクリック(ペグの開始位置とペグの終了位置)を登録するために私の移動メソッドを取得するのですか?フォームの

コード:TheBoardクラスの

public partial class Form1 : Form 
{ 
    private Button[] btn = new Button[15]; 
    private TheBoard myboard = new TheBoard(); 

    public Form1() 
    { 
     InitializeComponent(); 

     int buttonsPerRow = 1; 
     int index = 0; 

     while (index < btn.Length) 
     { 
      int increment = this.Width/(buttonsPerRow + 1); 

      for (int j = 1; j <= buttonsPerRow; j++) 
      { 
       btn[index] = new Button 
       { 
        //other style elements of the button 
        Name = "btn" + index 
       } 


       btn[index].Click += new EventHandler(this.My_Click); 
       Controls.Add(btn[index]); 

       index++; 
      } 

      buttonsPerRow++; 
     } 
    } 

    private void My_Click(object sender, EventArgs e) { 

    myboard.getValues(); 
    Button b = (Button)sender; 
    string bName = b.Name; 
    // Now pull off the btn 
    string num = bName.Substring(3, bName.Length - 3); 
    // Parsing the number to an int 
    int x = Int32.Parse(num); 
    myboard.move(x); 

    int[] color = myboard.getValues(); 
    for (int i = 0; i < 15; i++) 
    { 
     color = myboard.getValues(); 
     if (color[i] == TheBoard.hasPeg) 
     { 
      btn[i].BackColor = System.Drawing.Color.Yellow; 
     } 
     else 
      btn[i].BackColor = System.Drawing.Color.Black; 
    }//for 



    } 
} 

コード:

class TheBoard 
{ 
    static public int hasPeg = 100; 
    static public int noPeg = 50; 
    private int[] board; 
    private int firstMove; //1st click 

    public TheBoard() 
    { 
     board = new int[15]; 
     board[0] = noPeg; 
     for(int i = 1; i < 15; i++) 
     { 
      board[i] = hasPeg; 
     } 
     firstMove = -1; //giving last move a location, starting it at the beginning 
    } 

    public int move(int x) 
    { 
     if(firstMove == -1) 
     { 
      firstMove = x; 
      return 0; 
     } 
     // blank at 0 


     // if you click a blank your 1st move 
     if (firstMove == noPeg) 
     { 
      Console.WriteLine("You cant move if there isn't a peg."); 
      return 666; 
     } 


     // first---------------------------------------middle-----------------------end 
     if (firstMove == 1 && board[0] == hasPeg && board[3] == hasPeg && board[6] == noPeg) 
     { 
      RemovePeg(board[0], board[3], board[6]); 
      return 0; 
     } 
     if (firstMove == 1 && board[0] == hasPeg && board[2] == hasPeg && board[4] == noPeg) 
     { 
      RemovePeg(board[0], board[2], board[4]); 
      return 0; 
     } 
     //etc for remaining firstMove possibilities 

     firstMove = -1; 
     return 5; 
    } 

    private int RemovePeg(int first, int second, int goal) { 
     board[goal] = hasPeg; 
     board[first] = noPeg; 
     board[second] = noPeg; 
     return 0; 
    } 

    public int[] getValues() 
    { 
     return board; 
    } 
} 
+0

ボタンに「onclick」イベントを割り当てる必要があります。 – DaniCE

+0

私は初心者ですから、どういう意味なのかよくわかりません。各ボタンを作成するforループやform1のmy_Clickには? –

答えて

0

私は、コードの上に見て、私はあなたの問題を理解すると思います。あなたは開始ペグを選択することができますが、どこに行くべきかを選択する方法はありません。あなたのコードを最小限に編集して、最初のボタンクリックをグローバル変数に保存し、2番目のボタンクリックはそれが2番目であることを認識し、2つの情報でボード移動を開始します(グローバル変数をリセットします)。

+0

私はmoveメソッドで2つのパラメータを持つ必要がありますか、そこにある必要がある唯一のパラメータは2回目のクリックですか? –

+0

私は2つ持っています。 「From」と「To」。 2回目のクリックまでmoveメソッドは呼び出されません。 –

+0

私はフラグを使用して、最初のクリックか2番目のクリックかを確認できますか? count = 0の場合と同様に、最初のクリックを行い、countを1に設定し、2回のクリックからの入力に基づいてmoveメソッドを実行するステートメントを持っていますか? –

関連する問題