private void Window_KeyDown(object sender, KeyEventArgs e) { if (Key.A == e.Key) { MouseClickControl(ControlType.AI, MouseButton.Left, 0, 0); } else if (Key.T == e.Key) { xTL = GetMousePosition().X; yTL = GetMousePosition().Y; } else if (Key.X == e.Key) { xRB = GetMousePosition().X; yRB = GetMousePosition().Y; ms.UpdateShapePostion(xTL, yTL, xRB, yRB); ms.UpdateSizeAndBoom(height, width, booms); } else if (Key.S == e.Key) { ms.UpdateSizeAndBoom(height, width, booms); int bx = (int)GetMousePosition().X; int by = (int)GetMousePosition().Y; mr.SetCurrentState(ms.GetMRState()); int wSquare = (int)(xRB - xTL) / width; int hSquare = (int)(yRB - yTL) / height; int count = 0; while (count < width * width) { MinesweeperAI.Action act = ma.GetActionFromRule(mr); int x = (int)(act.y * wSquare + wSquare / 2 + xTL); int y = (int)(act.x * hSquare + hSquare / 2 + yTL); SetCursorPos(x, y); if (act.mouse == 0) { mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, 0, 0, 0, 0); } else if (act.mouse == 2) { mouse_event(MOUSEEVENTF_MIDDLEDOWN | MOUSEEVENTF_MIDDLEUP, 0, 0, 0, 0); } else { mouse_event(MOUSEEVENTF_RIGHTDOWN | MOUSEEVENTF_RIGHTUP, 0, 0, 0, 0); } SetCursorPos(bx, by); Thread.Sleep(60); mr.SetCurrentState(ms.GetMRState()); var curState = mr.GetCurrentState(); dme.UpdateSquares(curState); int blankCount = 0; int abnormalCount = 0; for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) { if (curState[i][j] == MinesweeperRule.MS_blank) { blankCount++; } if (curState[i][j] == MinesweeperRule.MS_bombdeath || curState[i][j] == MinesweeperRule.MS_bombmisflagged || curState[i][j] == MinesweeperRule.MS_bombrevealed) { abnormalCount++; } } } if (blankCount == 0 || abnormalCount > 0) { break; } count++; } } }
private void MouseClickControl(ControlType ct, MouseButton mouse, int row = 0, int col = 0) { if (mouse == MouseButton.Left) { switch (ct) { case ControlType.Square: if (mr.playState == MinesweeperRule.PlayState.Start) { time = 0; timer.Start(); } mr.Action(row, col); dme.UpdateSquares(mr.GetCurrentState()); if (mr.playState == MinesweeperRule.PlayState.Win) { dme.UpdateFace(MinesweeperRule.F_facewin); timer.Stop(); MessageBox.Show("Win game!"); } else if (mr.playState == MinesweeperRule.PlayState.Lose) { dme.UpdateFace(MinesweeperRule.F_facedead); timer.Stop(); } break; case ControlType.Face: mr.CreateNewGame(height, width, booms); CreateDrawingMinesweeperEnv(saveSettingInfor.SettingWindowInformation); break; case ControlType.Setting: SettingWindow settingWindow = new SettingWindow(saveSettingInfor.SettingWindowInformation); if (settingWindow.ShowDialog() == true) { CreateDrawingMinesweeperEnv(settingWindow.Information); saveSettingInfor.SettingWindowInformation = settingWindow.Information; SaveSetting(); height = settingWindow.Information[0]; width = settingWindow.Information[1]; booms = settingWindow.Information[2]; mr.CreateNewGame(height, width, booms); } break; case ControlType.AI: MinesweeperAI.Action act = ma.GetActionFromRule(mr); if (act.mouse >= 0) { MouseClickControl(ControlType.Square, (act.mouse == 0) ? MouseButton.Left : MouseButton.Right, act.x, act.y); } break; default: break; } } else if (mouse == MouseButton.Right) { switch (ct) { case ControlType.Square: if (mr.playState == MinesweeperRule.PlayState.Start) { time = 0; timer.Start(); } mr.Action(row, col, 1); dme.UpdateSquares(mr.GetCurrentState()); dme.UpdateNumBoom(booms - mr.totalFlag); break; default: break; } } }