private void InitializeGame(int x, int y, int mines) { CleanUp(); this.X = x; this.Y = y; //布雷 GenerateMap(x, y, mines); matrix = new MineControl[x * y]; //初始化MineControl对象数组 for (int i = 0; i < x; i++) { for (int j = 0; j < y; j++) { MineControl currentbtn = new MineControl(); currentbtn.Width = ButtonSize; currentbtn.Height = ButtonSize; currentbtn.Top = 25 + ButtonSize * i; currentbtn.Left = +ButtonSize * j; currentbtn.Visible = true; currentbtn.Value = g[i, j]; currentbtn.MouseDownEvent += MineMouseDown; currentbtn.MouseUpEvent += MineMouseUp; currentbtn.LocationX = i; currentbtn.LocationY = j; this.Controls.Add(currentbtn); matrix[i * y + j] = currentbtn; } } this.Width = 10 + ButtonSize * y; this.Height = 45 + ButtonSize * x + ButtonSize; this.Refresh(); }
private void MineMouseDown(object sender, EventArgs e) { MineControl btn = (MineControl)sender; switch (btn.MouseStatus) { case 1: //left btn.Press(); break; case 2: //right btn.PutFlag(); break; case 3: //both //按下周围9个键 for (int i = -1; i <= 1; i++) { if (i + btn.LocationX < 0 || i + btn.LocationX >= X) { continue; } for (int j = -1; j <= 1; j++) { if (j + btn.LocationY < 0 || j + btn.LocationY >= Y) { continue; } matrix[(i + btn.LocationX) * Y + j + btn.LocationY].Press(); } } break; } }
private void MineMouseUp(object sender, EventArgs e) { MineControl btn = (MineControl)sender; switch (btn.MouseStatus) { case 1: //left btn.Unseal(); if (btn.Value == 0) { FloodFill(btn.LocationX, btn.LocationY); } break; case 2: //right //do nothing break; case 3: //both bool doUnseal = false; //检查是否符合翻开的条件 if (CheckCondition(btn.LocationX, btn.LocationY)) { doUnseal = true; } for (int i = -1; i <= 1; i++) { if (i + btn.LocationX < 0 || i + btn.LocationX >= X) { continue; } for (int j = -1; j <= 1; j++) { if (j + btn.LocationY < 0 || j + btn.LocationY >= Y) { continue; } MineControl curbtn = matrix[(i + btn.LocationX) * Y + j + btn.LocationY]; if (doUnseal) { curbtn.Unseal(); if (curbtn.Value == 0) { FloodFill(curbtn.LocationX, curbtn.LocationY); } } else { curbtn.UnPress(); } } } break; } }