-1

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

(请看我的 for 循环)

这张图片说明了我的问题是:

在此处输入图像描述

这是我的代码:

        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);
        }
    }
}
4

2 回答 2

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;
              }
            }
          }
    }
于 2013-01-05T22:40:43.393 回答
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;
                    }
                }
            }
        }
于 2013-01-05T23:34:19.030 回答