2

私は、ボタン付きの2x3ボードを作成したゲームReversi/Othelloと同じように小さなゲームを作成しています。

ボタンをクリックすると色が変わりますが、2つの黒い色の間に白い色があるかどうかを検出するのに問題があります。ある場合は、その白い色を黒に変更してください。これが理にかなっていると思います。ボタンは2D配列になっています。私がこれを行うのを助けることができるどんな提案でも大いにありがたいです。

画像:

ここに画像の説明を入力してください これが私のコードです:

![namespace reversitest
{
    public partial class Form1 : Form
    {

        private Button\[,\] squares;
        public Form1()
        {
            InitializeComponent();

            squares = new Button\[3, 2\];
            squares = new Button\[,\] {{button1,  button2,  button3},
                {button4,  button5,  button6,}};
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            foreach (Button sqrr in squares)
            {
                sqrr.Click += new System.EventHandler(this.DrawCharacter);
            }
        }
        int _turn = 0;
        private void DrawCharacter(object sender, EventArgs e)
        {
            Button sqrr = (Button)sender;
            int col = 0;

            if (sqrr.BackColor.Equals(Color.Black) || sqrr.BackColor.Equals(Color.White))
            {
                MessageBox.Show("Move Not Allowed!");
            }
            else 
            {
               for ( int i = 0; i < squares.GetLongLength(1); ++i)
               {

                  // check othere squares and change color
                   if (i < 2)
                   {
                       for (int f = 0; f < 3; ++f)
                       {
                           var ss = squares\[i, f\];
                           if (ss.BackColor.Equals(Color.Black))
                           {

                               MessageBox.Show("we have a black");

                               //ss = squares\[i, f+1\];
                               ss.BackColor = Color.Black;

                           }
                           else
                           {
                               MessageBox.Show("no black");
                           } 
                       }

                   }

                       if (_turn == 0)
                       {
                           _turn = 1;
                           sqrr.BackColor = Color.Black;


                       }
                       else
                       {
                           _turn = 0;
                           sqrr.BackColor = Color.White;


                       } 

               }


            }


        }
    }
}
4

3 に答える 3

2

まず、配列インデックスを使用してボタンに名前を付けます。ボタンを見つけるのに役立ちます。
たとえば、あなたによると、画像button1の名前はbtn_1_1になります。

次に、ボタンクリックイベント内で、最初にボタン名を取得してから、配置されたボタンを識別します。

        Button b = sender as Button;
        string[] btnData = b.Name.Split('_');
        int x = int.Parse(btnData[1]);
        int y = int.Parse(btnData[2]);

        //check for possible combinations 
        int top = y - 2;
        int botton = y + 2;

        int left = x - 2;
        int right = x + 2;

        if (top >= 0 && squares[top, y].Background == Color.Black)
        {
            squares[top+1, y].Background = Color.Black;
        }  
        ...  
        ...   

そのように続けます。詳細が必要な場合は、お気軽にお問い合わせください。

于 2012-12-31T05:26:53.533 に答える
1

最終回答

//check for possible combinations 
            int top = x - 2;
            int botton = x + 2;

            int left = y - 2;
            int right = y + 2;

            if (top >= 0 && squares[top, y].BackColor == Color.Black)
            {
                squares[top + 1, y].BackColor = Color.Black;
            }
            else if (left >= 0 && squares[x, left].BackColor == Color.Black)
            {
                squares[x, left + 1].BackColor = Color.Black;
            }

            else if (left >= 0 && squares[x, left].BackColor == Color.Black)
            {
                squares[x, left + 1].BackColor = Color.Black;
            }

後で8x8ボード用に拡張されます

于 2013-01-03T18:56:52.223 に答える
0

エレガントにする必要がありますか?一種のブルートフォース方式:8つの異なる方向のピースをチェックして、それらを整列させることができます。たとえば、黒い部分から始めます。次のピースを一方向にチェックします。白の場合は、続けて白だった位置をメモして、後で黒に変更できるようにします。最終的に黒い部分に当たったら、保存されているすべての位置を黒に変更して次の方向に進み、8つの方向すべてが完了するまでこのプロセスを繰り返します。

于 2012-12-28T03:38:46.967 に答える