
扫雷规则某个单元格的点击逻辑:【鼠标左键点击单元格标记不是雷。鼠标右键标记该单元格是雷】①鼠标左键如果该单元格实际上是雷则直接游戏结束。如果该单元格实际上不是雷如果此前该单元格已标记为雷,移除PlayerMissiles的当前PositionPlayerMissiles显示九宫格内雷的数字。修改PlayerElement.IsMissilefalse,玩家已扫雷个数减一。如果九宫格内雷的数字是零就显示九宫格内的所有数字【显示空白连通器】②鼠标右键标记该单元格是雷PlayerElement.IsMissiletrue,玩家已扫雷个数加一添加当前Position到PlayerMissiles。【可能不一定是雷误判】玩家已扫雷个数可能不一定是准确的允许超过实际地雷数量扫雷游戏中不允许出现任何一个3×3九宫格都是雷. 生成逻辑需保证任意九宫格不出现9个雷【某个不是雷的单元格显示的数字范围为【0,8】】。在Kimi.com智能AI中输入内容使用C#实现一个10行20列 行列式 使用 一维数组 int[] arrnew int[200] 值为0~199表示随机出 20个数任意选中9个数都不能组成一个九宫格比如 {0,1,2,20,21,22,40,41,42}就组成九宫格是非法的kimi生成的辅助代码如下类文件 AntiSudokuGenerator.csusing System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace RemoteControlApp { /*使用C#实现一个10行20列 行列式 使用 一维数组 int[] arrnew int[200] 值为0~199表示 * 随机出 20个数任意选中9个数不能组成一个九宫格比如 {0,1,2,20,21,22,40,41,42}就组成九宫格是非法的 * 扫雷游戏中不允许出现任何一个3×3九宫格都是雷. 生成逻辑保证任意9个不组成九宫格。 */ public class AntiSudokuGenerator { /// summary /// 生成雷区 /// /summary /// param nameRows行数/param /// param nameCols列数/param /// param namemineCount生成的地雷总个数/param /// returns/returns public static int[] GenerateMineRegion(int Rows, int Cols, int mineCount) { if (Rows 1 || Cols 1 || mineCount Rows * Cols) { throw new Exception($行数和列数只能为正整数生成的雷的总个数不能超过行列式的总元素数); } //const int Rows 10; //const int Cols 20; //const int Total Rows * Cols; // 200 //const int SelectCount 20; const int GridSize 3; // 3x3 九宫格 int Total Rows * Cols; int SelectCount mineCount; // 1. 预计算所有九宫格 // 10行20列中3x3九宫格的左上角可以位于行0~7列0~17共 8*18144 个 List HashSetint grids new ListHashSetint(); for (int r 0; r Rows - GridSize; r) { for (int c 0; c Cols - GridSize; c) { HashSetint grid new HashSetint(); for (int dr 0; dr GridSize; dr) { for (int dc 0; dc GridSize; dc) { grid.Add((r dr) * Cols (c dc)); } } grids.Add(grid); } } Console.WriteLine($总九宫格数量: {grids.Count}); // 144 // 2. 预计算每个索引属于哪些九宫格 Listint[] belongsTo new Listint[Total]; for (int i 0; i Total; i) belongsTo[i] new Listint(); for (int g 0; g grids.Count; g) { foreach (var idx in grids[g]) { belongsTo[idx].Add(g); } } // 3. 贪心随机选择20个数 Random random new Random(); HashSetint selected new HashSetint(); Listint candidates Enumerable.Range(0, Total).OrderBy(_ random.Next()).ToList(); int[] gridSelectedCount new int[grids.Count]; foreach (int candidate in candidates) { if (selected.Count SelectCount) break; // 核心约束如果加入 candidate 会导致某个九宫格凑齐9个则跳过 bool wouldCompleteGrid false; foreach (int g in belongsTo[candidate]) { if (gridSelectedCount[g] GridSize * GridSize - 1) // 8 { wouldCompleteGrid true; break; } } if (!wouldCompleteGrid) { selected.Add(candidate); foreach (int g in belongsTo[candidate]) { gridSelectedCount[g]; } } } if (selected.Count SelectCount) { throw new Exception($未能成功选出{SelectCount}个满足条件的数请重试。); } // 4. 输出结果 int[] result selected.ToArray(); Array.Sort(result); return result; /* Console.WriteLine($\n成功选出 {result.Length} 个数已排序); Console.WriteLine(string.Join(, , result)); // 5. 可视化网格 Console.WriteLine(\n可视化X选中, .未选中\n); for (int r 0; r Rows; r) { for (int c 0; c Cols; c) { int idx r * Cols c; Console.Write(selected.Contains(idx) ? X : .); Console.Write(c % 3 2 ? : ); // 每3列加个空格方便看九宫格 } Console.WriteLine(r % 3 2 ? \n : ); // 每3行加个空行 } // 6. 严格验证 Console.WriteLine( 验证报告 ); bool foundAny false; foreach (var grid in grids) { if (grid.IsSubsetOf(selected)) { Console.WriteLine($[非法] 发现完整九宫格: {string.Join(, , grid.OrderBy(x x))}); foundAny true; } } if (!foundAny) { Console.WriteLine([通过] 选中的20个数中不存在任何3x3九宫格。); } // 7. 组合级验证 Console.WriteLine(\n 组合级验证 ); int maxInAnyGrid gridSelectedCount.Max(); Console.WriteLine($单个九宫格最多被选中格子数: {maxInAnyGrid} (安全阈值: 8)); Console.WriteLine(maxInAnyGrid 8 ? [通过] 生成逻辑保证任意9个不组成九宫格。 : [失败]); */ } } }新建Winform应用程序RemoteControlApp.net framework 4.8,将默认的Form1重命名为FormScanner新建单元格对象类MapElementMapElement.cs源程序如下using System; using System.Collections.Generic; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; namespace RemoteControlApp { /// summary /// 地图元素,标记某个矩形区域是否为地雷 /// /summary public class MapElement { public MapElement(int x, int y, int width, int height, bool isMissile false) { this.X x; this.Y y; this.Width width; this.Height height; this.IsMissile isMissile; } /// summary /// 起始X坐标 /// /summary public int X { get; set; } /// summary /// 起始Y坐标 /// /summary public int Y { get; set; } /// summary /// 宽度 /// /summary public int Width { get; set; } /// summary /// 高度 /// /summary public int Height { get; set; } /// summary /// 是否是地雷 /// /summary public bool IsMissile { get; set; } /// summary /// 数组索引位置i * ColumnCount j 【第i行第j列】 /// /summary public int Position { get; set; } -1; /// summary /// 当不是地雷IsMissilefalse时九宫格内累计有几个地雷范围【0,8】 /// /summary public int MissileCount { get; set; } -1; } }窗体FormScanner设计器代码如下文件FormScanner.Designer.csnamespace RemoteControlApp { partial class FormScanner { /// summary /// Required designer variable. /// /summary private System.ComponentModel.IContainer components null; /// summary /// Clean up any resources being used. /// /summary /// param namedisposingtrue if managed resources should be disposed; otherwise, false./param protected override void Dispose(bool disposing) { if (disposing (components ! null)) { components.Dispose(); } base.Dispose(disposing); } #region Windows Form Designer generated code /// summary /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// /summary private void InitializeComponent() { this.gameScene new System.Windows.Forms.PictureBox(); this.btnPlay new System.Windows.Forms.Button(); this.label1 new System.Windows.Forms.Label(); this.cboDifficulty new System.Windows.Forms.ComboBox(); this.label2 new System.Windows.Forms.Label(); this.lblTotal new System.Windows.Forms.Label(); this.lblScanner new System.Windows.Forms.Label(); this.label5 new System.Windows.Forms.Label(); this.label3 new System.Windows.Forms.Label(); this.lblResult new System.Windows.Forms.Label(); ((System.ComponentModel.ISupportInitialize)(this.gameScene)).BeginInit(); this.SuspendLayout(); // // gameScene // this.gameScene.BackColor System.Drawing.Color.Black; this.gameScene.Location new System.Drawing.Point(50, 0); this.gameScene.Name gameScene; this.gameScene.Size new System.Drawing.Size(720, 720); this.gameScene.TabIndex 2; this.gameScene.TabStop false; this.gameScene.Paint new System.Windows.Forms.PaintEventHandler(this.gameScene_Paint); this.gameScene.MouseClick new System.Windows.Forms.MouseEventHandler(this.gameScene_MouseClick); // // btnPlay // this.btnPlay.Font new System.Drawing.Font(宋体, 14F, System.Drawing.FontStyle.Bold); this.btnPlay.ForeColor System.Drawing.Color.Blue; this.btnPlay.Location new System.Drawing.Point(802, 98); this.btnPlay.Name btnPlay; this.btnPlay.Size new System.Drawing.Size(137, 50); this.btnPlay.TabIndex 3; this.btnPlay.Text 重新开始游戏; this.btnPlay.UseVisualStyleBackColor true; this.btnPlay.Click new System.EventHandler(this.btnPlay_Click); // // label1 // this.label1.AutoSize true; this.label1.Location new System.Drawing.Point(800, 26); this.label1.Name label1; this.label1.Size new System.Drawing.Size(29, 12); this.label1.TabIndex 4; this.label1.Text 难度; // // cboDifficulty // this.cboDifficulty.FormattingEnabled true; this.cboDifficulty.Items.AddRange(new object[] { 初级10%, 中级20%, 高级30%}); this.cboDifficulty.Location new System.Drawing.Point(785, 54); this.cboDifficulty.Name cboDifficulty; this.cboDifficulty.Size new System.Drawing.Size(173, 20); this.cboDifficulty.TabIndex 5; // // label2 // this.label2.AutoSize true; this.label2.Location new System.Drawing.Point(783, 208); this.label2.Name label2; this.label2.Size new System.Drawing.Size(65, 12); this.label2.TabIndex 7; this.label2.Text 地雷总个数; // // lblTotal // this.lblTotal.AutoSize true; this.lblTotal.Location new System.Drawing.Point(867, 208); this.lblTotal.Name lblTotal; this.lblTotal.Size new System.Drawing.Size(11, 12); this.lblTotal.TabIndex 8; this.lblTotal.Text 0; // // lblScanner // this.lblScanner.AutoSize true; this.lblScanner.Location new System.Drawing.Point(867, 233); this.lblScanner.Name lblScanner; this.lblScanner.Size new System.Drawing.Size(11, 12); this.lblScanner.TabIndex 10; this.lblScanner.Text 0; // // label5 // this.label5.AutoSize true; this.label5.Location new System.Drawing.Point(783, 233); this.label5.Name label5; this.label5.Size new System.Drawing.Size(65, 12); this.label5.TabIndex 9; this.label5.Text 已扫雷个数; // // label3 // this.label3.AutoSize true; this.label3.Location new System.Drawing.Point(776, 397); this.label3.Name label3; this.label3.Size new System.Drawing.Size(53, 12); this.label3.TabIndex 11; this.label3.Text 游戏结果; // // lblResult // this.lblResult.AutoSize true; this.lblResult.Font new System.Drawing.Font(宋体, 16F, System.Drawing.FontStyle.Bold); this.lblResult.ForeColor System.Drawing.Color.Red; this.lblResult.Location new System.Drawing.Point(781, 438); this.lblResult.Name lblResult; this.lblResult.Size new System.Drawing.Size(79, 22); this.lblResult.TabIndex 12; this.lblResult.Text 进行中; // // FormScanner // this.AutoScaleDimensions new System.Drawing.SizeF(6F, 12F); this.AutoScaleMode System.Windows.Forms.AutoScaleMode.Font; this.ClientSize new System.Drawing.Size(1011, 722); this.Controls.Add(this.lblResult); this.Controls.Add(this.label3); this.Controls.Add(this.lblScanner); this.Controls.Add(this.label5); this.Controls.Add(this.lblTotal); this.Controls.Add(this.label2); this.Controls.Add(this.cboDifficulty); this.Controls.Add(this.label1); this.Controls.Add(this.btnPlay); this.Controls.Add(this.gameScene); this.Name FormScanner; this.StartPosition System.Windows.Forms.FormStartPosition.CenterScreen; this.Text 扫雷-斯内科【雷为红色九宫格中心显示雷的个数雷的个数为0显示空格】; this.Load new System.EventHandler(this.FormScanner_Load); ((System.ComponentModel.ISupportInitialize)(this.gameScene)).EndInit(); this.ResumeLayout(false); this.PerformLayout(); } #endregion private System.Windows.Forms.PictureBox gameScene; private System.Windows.Forms.Button btnPlay; private System.Windows.Forms.Label label1; private System.Windows.Forms.ComboBox cboDifficulty; private System.Windows.Forms.Label label2; private System.Windows.Forms.Label lblTotal; private System.Windows.Forms.Label lblScanner; private System.Windows.Forms.Label label5; private System.Windows.Forms.Label label3; private System.Windows.Forms.Label lblResult; } }窗体FormScanner关键逻辑代码如下文件FormScanner.cs【已更新之前存在bug重新开始游戏因没有清空PlayerMissiles集合导致无法游戏结束的Bug】using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace RemoteControlApp { public partial class FormScanner : Form { /// summary /// 行数 /// /summary const int RowCount 20; /// summary /// 列数 /// /summary const int ColumnCount 24; /// summary /// 【实际】地图元素集合与PlayerElementList集合的元素个数完全一致 /// /summary static ListMapElement MapElementList new ListMapElement(); /// summary /// 【玩家操控】地图元素集合 /// /summary static ListMapElement PlayerElementList new ListMapElement(); /// summary /// 【实际】目标地雷集合 /// /summary static HashSetint DestMissiles new HashSetint(); /// summary /// 【玩家操控】玩家选择的地雷集合 /// /summary static HashSetint PlayerMissiles new HashSetint(); /// summary /// 是否显示实际的雷分布图默认false显示【玩家操控】的雷区【是否游戏结束】 /// /summary bool visibleLast false; /// summary /// 玩家已扫雷个数可能不一定是准确的允许超过实际地雷数量 /// /summary int scannerCount 0; public FormScanner() { InitializeComponent(); cboDifficulty.SelectedIndex 0; } private async void FormScanner_Load(object sender, EventArgs e) { await LoadBattleMap(); //强行触发重绘Paint事件 gameScene.Invalidate(); } /// summary /// 初始化加载地图实际雷区地图【MapElementList】保存到缓存中一直不做篡改用于显示某个单元格的九宫格内地雷的个数 /// 玩家操作的雷区地图【PlayerElementList】默认为灰色的【未知的】由玩家操作鼠标左键、鼠标右键来进行篡改属性IsMissile和MissileCount /// /summary private async Taskint LoadBattleMap() { if (cboDifficulty.SelectedIndex -1) { cboDifficulty.Focus(); MessageBox.Show(请选择难度:10%,20%,30%); return 0; } //实际地雷数量 int mineCount (int)((cboDifficulty.SelectedIndex 1) * RowCount * ColumnCount * 0.1); #region 复位清空到初始状态用于重新开始游戏 scannerCount 0; visibleLast false; MapElementList.Clear(); PlayerElementList.Clear(); DestMissiles.Clear(); PlayerMissiles.Clear(); #endregion await Task.Run(() { int[] mineRegion AntiSudokuGenerator.GenerateMineRegion(RowCount, ColumnCount, mineCount); DestMissiles mineRegion.ToHashSet(); for (int i 0; i RowCount; i) { for (int j 0; j ColumnCount; j) { //玩家操控的地图默认都不是雷绿灯 MapElement playerElement new MapElement(30 * j, 30 * i, 30, 30, false) { Position i * ColumnCount j }; PlayerElementList.Add(playerElement); //如果当前位置在地雷的集合mineRegion中存在 bool isMissile mineRegion.Contains(i * ColumnCount j); MapElement newElement new MapElement(30 * j, 30 * i, 30, 30, isMissile) { Position i * ColumnCount j }; MapElementList.Add(newElement); } } //计算九宫格的地雷个数 for (int i 0; i MapElementList.Count; i) { if (!MapElementList[i].IsMissile) { int missileCountNineGrid CalculateMissileCountFromNineGrid(i);//九宫格地雷个数:最多为8 MapElementList[i].MissileCount missileCountNineGrid; } } }); lblTotal.Text mineCount.ToString(); lblScanner.Text scannerCount.ToString(); lblResult.Text 游戏进行中...; //MessageBox.Show($地图元素总个数{MapElementList.Count},存在地雷个数【{mineCount}】); return mineCount; } private void gameScene_Paint(object sender, PaintEventArgs e) { Graphics g e.Graphics; if (visibleLast) { //游戏结束时显示实际雷区分布 ShowMap(MapElementList, g); } else { //游戏进行中显示玩家认为的雷区分布 ShowMap(PlayerElementList, g); } } /// summary /// 刷新雷区地图MissileCount为-1时代表【初始化未知】 /// /summary /// param nameshowMapElementList/param /// param nameg/param private void ShowMap(ListMapElement showMapElementList, Graphics g) { for (int i 0; i showMapElementList.Count; i) { g.DrawRectangle(new Pen(Color.White), showMapElementList[i].X, showMapElementList[i].Y, showMapElementList[i].Width, showMapElementList[i].Height); if (showMapElementList[i].IsMissile) { SizeF size g.MeasureString(●, new Font(宋体, 12)); g.DrawString(●, new Font(宋体, 12), new SolidBrush(Color.Red), showMapElementList[i].X (showMapElementList[i].Width - size.Width) / 2, showMapElementList[i].Y (showMapElementList[i].Height - size.Height) / 2); } else { int missileCountNineGrid showMapElementList[i].MissileCount;//九宫格地雷个数:最多为8 string text missileCountNineGrid -1 ? ● : (missileCountNineGrid 0 ? \x20 : missileCountNineGrid.ToString()); SizeF size g.MeasureString(text, new Font(宋体, 12)); Color showColor Color.Gray; switch (missileCountNineGrid) { case -1: showColor Color.Gray; break; case 0: case 1: case 2: showColor Color.LimeGreen; break; default: showColor Color.Red;//不小于3就显示红色 break; } g.DrawString(text, new Font(宋体, 12), new SolidBrush(showColor), showMapElementList[i].X (showMapElementList[i].Width - size.Width) / 2, showMapElementList[i].Y (showMapElementList[i].Height - size.Height) / 2); } } } /// summary /// 计算出九宫格内地雷个数:最多为8 /// /summary /// param namei/param /// returns/returns private int CalculateMissileCountFromNineGrid(int i) { int missileCountNineGrid 0;//九宫格地雷个数:最多为8 int rowNum MapElementList[i].Position / ColumnCount;//行号 int columnNum MapElementList[i].Position - MapElementList[i].Position / ColumnCount * ColumnCount;//列号 for (int r rowNum - 1; r rowNum 1; r) { for (int c columnNum - 1; c columnNum 1; c) { if (r rowNum c columnNum) { continue;//不考虑自身九宫格的中心 } if (ExistMissile(r, c)) { missileCountNineGrid; } } } return missileCountNineGrid; } /// summary /// 检测某个单元格是否存在雷 /// /summary /// param namerowNum/param /// param namecolumnNum/param /// returns/returns private bool ExistMissile(int rowNum, int columnNum) { if (rowNum 0 rowNum RowCount columnNum 0 columnNum ColumnCount) { MapElement element MapElementList.Find(x x.Position rowNum * ColumnCount columnNum); if (element ! null element.IsMissile) { return true; } } return false; } /// summary /// 展开全部的没有雷的整个连通区域【MissileCount0】递归所有连通器 /// /summary /// param nameplayerElement/param private void ShowAllZeroMissile(MapElement playerElement) { StackMapElement playerStack new StackMapElement(); playerStack.Push(playerElement); while (playerStack.Count 0) { MapElement current playerStack.Pop(); MapElement mapElement PlayerElementList.Find(pe pe.Position current.Position); int row current.Position / ColumnCount;//行号 int column current.Position - row * ColumnCount;//列号 if (mapElement.MissileCount 0) { for (int r row - 1; r row 1; r) { for (int c column - 1; c column 1; c) { ShowPlayerMissile(r, c); if (r 0 r RowCount c 0 c ColumnCount) { if (r row c column) { continue;//不考虑自身九宫格的中心 } MapElement nextElement PlayerElementList.Find(x x.Position r * ColumnCount c); MapElement srcElement MapElementList.Find(x x.Position r * ColumnCount c); if (srcElement ! null nextElement ! null srcElement.MissileCount 0 nextElement.MissileCount -1) { //nextElement.MissileCount -1 代表 未访问过,用于防止进入死递归 playerStack.Push(nextElement); } } } } } } } /// summary /// 显示玩家单元格的地雷信息 /// /summary /// param namerowNum/param /// param namecolumnNum/param private void ShowPlayerMissile(int rowNum, int columnNum) { if (rowNum 0 rowNum RowCount columnNum 0 columnNum ColumnCount) { MapElement playerElement PlayerElementList.Find(x x.Position rowNum * ColumnCount columnNum); MapElement srcElement MapElementList.Find(x x.Position rowNum * ColumnCount columnNum); if (playerElement ! null srcElement ! null playerElement.MissileCount -1) { playerElement.IsMissile false; playerElement.MissileCount srcElement.MissileCount; if(srcElement.MissileCount 0) { ShowPlayerMissile(rowNum - 1, columnNum); ShowPlayerMissile(rowNum 1, columnNum); ShowPlayerMissile(rowNum, columnNum - 1); ShowPlayerMissile(rowNum, columnNum 1); } } } } /// summary /// 某个单元格的点击逻辑:【鼠标左键点击单元格标记不是雷。鼠标右键标记该单元格是雷】 /// ①鼠标左键如果该单元格实际上是雷则直接游戏结束。如果该单元格实际上不是雷如果此前该单元格已标记为雷,移除PlayerMissiles的当前PositionPlayerMissiles显示九宫格内雷的数字 /// 修改PlayerElement.IsMissilefalse,玩家已扫雷个数减一。如果九宫格内雷的数字是零就显示九宫格内的所有数字【显示空白连通器】 /// ②鼠标右键标记该单元格是雷PlayerElement.IsMissiletrue,玩家已扫雷个数加一添加当前Position到PlayerMissiles。【可能不一定是雷误判】 /// 玩家已扫雷个数可能不一定是准确的允许超过实际地雷数量 /// /summary /// param namesender/param /// param namee/param private void gameScene_MouseClick(object sender, MouseEventArgs e) { MapElement mapElement MapElementList.Find(me me.X e.Location.X e.Location.X me.X me.Width me.Y e.Location.Y e.Location.Y me.Y me.Height); if (mapElement null) { return; } int position mapElement.Position; MapElement playerElement PlayerElementList.Find(pe pe.Position position); if (playerElement null) { return; } if (visibleLast) { MessageBox.Show($扫雷游戏已结束请点击【重新开始游戏】); return; } int row position / ColumnCount;//行号 int column position - row * ColumnCount;//列号 if (e.Button MouseButtons.Left) { //MessageBox.Show($鼠标【左】键打开该矩形认为不是雷第【{row}】行第【{column}】列); if (mapElement.IsMissile) { visibleLast true; lblResult.Text 扫雷游戏失败; MessageBox.Show($Game Over:扫雷失败); } else { if (playerElement.MissileCount 0) { MessageBox.Show($鼠标【左】键不是雷。已经处理该区域不用重复操作第【{row}】行第【{column}】列); return; } //如果玩家 上一次设定为地雷 if (playerElement.IsMissile) { scannerCount--; } PlayerMissiles.Remove(position); int missileCountNineGrid mapElement.MissileCount; playerElement.IsMissile false; playerElement.MissileCount missileCountNineGrid; //如果九宫格的中央是0则直接打开该九宫格。显示九宫格区域所有相邻的MissileCount0的全部打开 if (missileCountNineGrid 0) { ShowAllZeroMissile(playerElement); } else //如果雷的个数是【1,8】则仅显示自己 { playerElement.IsMissile false; playerElement.MissileCount missileCountNineGrid; } } //刷新已扫雷个数 lblScanner.Text scannerCount.ToString(); //强行触发重绘Paint事件 gameScene.Invalidate(); } else if (e.Button MouseButtons.Right) { //分四种情况①玩家认为是雷实际上是雷 ②玩家认为是雷实际上不是雷 ③玩家认为不是雷实际上是雷 ④玩家认为不是雷实际上不是雷 if (playerElement.IsMissile mapElement.IsMissile) { MessageBox.Show($鼠标【右】键玩家已标记该单元格是雷不用重复操作第【{row}】行第【{ column }】列); return; } else if (playerElement.IsMissile !mapElement.IsMissile) { MessageBox.Show($鼠标【右】键玩家已标记该单元格是雷不用重复操作第【{row}】行第【{ column }】列); return; } else if (!playerElement.IsMissile mapElement.IsMissile) { if (playerElement.MissileCount 0) { MessageBox.Show($鼠标【右】键玩家已确认单元格不是雷不用重复操作第【{row}】行第【{ column }】列); return; } //鼠标右键 只要 不是雷 都标记为雷 playerElement.IsMissile true; playerElement.MissileCount -1; scannerCount; PlayerMissiles.Add(position); } else //④玩家认为不是雷实际上不是雷 { if (playerElement.MissileCount 0) { MessageBox.Show($鼠标【右】键玩家已确认单元格不是雷不用重复操作第【{row}】行第【{ column }】列); return; } //鼠标右键 只要 不是雷 都标记为雷 playerElement.IsMissile true; playerElement.MissileCount -1; scannerCount; PlayerMissiles.Add(position); } //刷新已扫雷个数 lblScanner.Text scannerCount.ToString(); //强行触发重绘Paint事件 gameScene.Invalidate(); } if (PlayerMissiles.SetEquals(DestMissiles)) { visibleLast true; lblResult.Text 恭喜!扫雷游戏成功; MessageBox.Show($扫雷成功玩家标记的地雷与实际地雷完全相同); gameScene.Invalidate(); } } private async void btnPlay_Click(object sender, EventArgs e) { await LoadBattleMap(); //强行触发重绘Paint事件 gameScene.Invalidate(); } } }程序运行如下初始化失败情况成功情况