From f57a13fce80faa98a06146995373acb2c39c3dcc Mon Sep 17 00:00:00 2001 From: yeziuku <53083103+yeziuku@users.noreply.github.com> Date: Tue, 21 Nov 2023 19:54:36 +0800 Subject: [PATCH] ChessBoardExample (#2) * Create ChessBoardExample * Rename ChessBoardExample to ChessBoardExample.cs --- Solutions/ChessBoardExample.cs | 158 +++++++++++++++++++++++++++++++++ 1 file changed, 158 insertions(+) create mode 100644 Solutions/ChessBoardExample.cs diff --git a/Solutions/ChessBoardExample.cs b/Solutions/ChessBoardExample.cs new file mode 100644 index 0000000..1d612c6 --- /dev/null +++ b/Solutions/ChessBoardExample.cs @@ -0,0 +1,158 @@ +using System; +using System.Collections.Generic; +using System.Drawing; +using System.Windows.Forms; + +namespace ChessBoardExample +{ + public partial class Form2 : Form + { + const int gridSize = 12; // 每个小方块大小为40像素 + const int step = 3; // 递进 + + Dictionary chessboardDict = new Dictionary(); + HashSet redPanelsSet = new HashSet(); + + public Form2() + { + InitializeComponent(); + InitializeChessboard(); + + foreach (var panel in chessboardDict.Values) + { + panel.MouseEnter += OnCellMouseEnter; + panel.MouseLeave += OnCellMouseLeave; + } + + string key = "5_9"; + Button b = new Button(); + b.Size = new Size(gridSize, gridSize); + b.Text = ""; + chessboardDict[key].Controls.Add(b); + b.Click += new EventHandler((sender, args) => + { + if (redPanelsSet.Count > 0) SetOriginalCells(); + else CheckRedCells(5, 9); + }); + } + + private void InitializeChessboard() + { + for (int i = 0; i < 16; i++) + for (int j = 0; j < 16; j++) + AddNewGrid(i, j); + } + + private void AddNewGrid(int x, int y) + { + string key = x.ToString() + "_" + y.ToString(); + var p = new Point(x * gridSize, y * gridSize); + + Panel cell = new Panel() + { + Name = key, + Size = new Size(gridSize, gridSize), + Location = p + }; + if ((x + y) % 2 == 0) + cell.BackColor = Color.White; + else + cell.BackColor = Color.LightGray; + + Controls.Add(cell); + + chessboardDict[key] = cell; + } + + private void CheckRedCells(int x, int y) + { + try + { + SetOriginalCells(); + HashSet keysToSet = new HashSet(); + + for (int dx = -step; dx <= step; ++dx) + { + for (int dy = -step; dy <= step; ++dy) + { + if (Math.Abs(dx) + Math.Abs(dy) <= step) + {//限制在中心点周围范围内 + if (x + dx >= -15 && x + dx < 16 && y + dy >= -15 && y + dy < 16) + {//检查是否在棋盘范围内 + string key = (x + dx) + "_" + (y + dy); + if (chessboardDict.ContainsKey(key)) + keysToSet.Add(key);//将符合条件的坐标添加到集合中 + } + } + } + } + this.SetRedCells(keysToSet); // 调用设置红色背景方法,传递新计算出来的键集合 + } + catch (Exception ex) + { + MessageBox.Show(ex.ToString()); + } + } + + private void SetRedCells(HashSet keysToSet) + { + foreach (var k in keysToSet) + { + var coords = k.Split('_'); + int x = int.Parse(coords[0]); + int y = int.Parse(coords[1]); + + var npn = chessboardDict[x + "_" + y]; + npn.BackColor = Color.FromArgb(128, Color.Red);//设置红色面板 + + redPanelsSet.Add(npn); + } + } + + private void SetOriginalCells() + { + try + { + foreach (var panel in redPanelsSet) + { + string[] coords = panel.Name.Split('_'); + int x = int.Parse(coords[0]); + int y = int.Parse(coords[1]); + + if ((x + y) % 2 == 0) + panel.BackColor = Color.White; + else + panel.BackColor = Color.LightGray; // 恢复为原来的背景颜色 + } + + redPanelsSet.Clear(); // 清空红色面板集合 + } + catch (Exception ex) + { + MessageBox.Show(ex.ToString()); + } + } + + private void OnCellMouseEnter(object sender, System.EventArgs e) + { + try + { + // 获取当前鼠标进入区域对应格子位置; + string[] s = ((Panel)sender).Name.Split('_'); + int x = int.Parse(s[0]); + int y = int.Parse(s[1]); + + CheckRedCells(x, y); + } + catch (Exception ex) + { + MessageBox.Show(ex.ToString()); + } + } + + private void OnCellMouseLeave(object sender, EventArgs e) + { + SetOriginalCells(); + } + } +}