喜欢游戏的黑白棋会对角线检测颜色

时间:2013-01-05 20:18:31

标签: c# for-loop multidimensional-array

我正在重新制作经典的黑白棋游戏,但我在尝试对角线正确改变颜色方面遇到了麻烦。我有两个for循环,但它们无法正常工作,任何人都可以查看我的代码并告诉我我做错了什么。

(请查看我的for循环)

此图片说明了我的问题:

enter image description here

这是我的代码:

        private void ClickedButton(object sender, EventArgs e)
        {
            Button s = (Button)sender;
            int x = int.Parse(s.Name.Split()[0]);
            int y = int.Parse(s.Name.Split()[1]);

            if (cnt == 0)
            {
                cnt = 1;
                s.BackColor = Color.Black;

                for (int i = 3; i > x; --i)
                {
                    for (int j = 0; j < y; ++j)
                    {
                        if (b[i, j].BackColor == Color.Black)
                        {
                            b[i - 1, j + 1].BackColor = Color.Black;

                        }
                    }
                }
            }
            else
            {
                cnt = 0;
                s.BackColor = Color.Red;
            }

           // MessageBox.Show("you have clicked button: " + x +" "+ y);
        }
    }
}

2 个答案:

答案 0 :(得分:1)

    private void ClickedButton(object sender, EventArgs e)
    {
        Button s = (Button)sender;
        int x = int.Parse(s.Name.Split()[0]);
        int y = int.Parse(s.Name.Split()[1]);

        if (b[x, y].BackColor == Color.Red || b[x, y].BackColor == Color.Black)
          return;

        var color = cnt == 1 ? Color.Red : Color.Black;
        cnt = 1 - cnt;
        b[x, y].BackColor = color;

          int len = 4;

          var directions = new[]
          {
            new {x =  0, y =  1}, 
            new {x =  0, y = -1}, 
            new {x =  1, y =  0},
            new {x = -1, y =  0}, 
            new {x = -1, y = -1},
            new {x =  1, y = -1},
            new {x = -1, y =  1},
            new {x =  1, y =  1}
          };
          b[x, y].BackColor = color;

          foreach (var dir in directions)
          {
            for (var i = 1; i < len; ++i)
            {
              var xi = x + i * dir.x;
              var yi = y + i * dir.y;
              if (xi < 0 || xi >= len || yi < 0 || yi >= len)
                break;
              if (b[xi, yi].BackColor != Color.Black && b[xi, yi].BackColor != Color.Red)
                break;
              if (b[xi, yi].BackColor == color)
              {
                for (var j = 1; j < i; ++j)
                  b[x + j * dir.x, y + j * dir.y].BackColor = color;
                break;
              }
            }
          }
    }

答案 1 :(得分:0)

答案

private void ClickedButton(object sender, EventArgs e)
        {
            Button s = (Button)sender;
            int x = int.Parse(s.Name.Split()[0]);
            int y = int.Parse(s.Name.Split()[1]);

           // if (b[x, y].BackColor != Color.Red && b[x, y].BackColor != Color.Black)
            //    return;

            var color = cnt == 1 ? Color.Red : Color.Black;
            cnt = 1 - cnt;
            b[x, y].BackColor = color;

            int len = 4;

            var directions = new[]
          {
            new {x =  0, y =  1}, 
            new {x =  0, y = -1}, 
            new {x =  1, y =  0},
            new {x = -1, y =  0}, 
            new {x = -1, y = -1},
            new {x =  1, y = -1},
            new {x = -1, y =  1},
            new {x =  1, y =  1}
          };
            b[x, y].BackColor = color;

            foreach (var dir in directions)
            {
                for (var i = 1; i < len; ++i)
                {
                    var xi = x + i * dir.x;
                    var yi = y + i * dir.y;
                    if (xi < 0 || xi >= len || yi < 0 || yi >= len)
                        break;
                    if (b[xi, yi].BackColor != Color.Black && b[xi, yi].BackColor != Color.Red)
                        break;
                    if (b[xi, yi].BackColor == color)
                    {
                        for (var j = 1; j < i; ++j)
                            b[x + j * dir.x, y + j * dir.y].BackColor = color;
                        break;
                    }
                }
            }
        }