private void Chessboard_MouseDown(object sender, MouseEventArgs e) { int x = Pixel2Index(e.X); int y = Pixel2Index(e.Y); if (gs.PlaceChess(x, y, gs.NextPlayer())) { DrawChess(x, y, gs.NextPlayer()); Redraw(); if (gs.WhoIsWin() != ChessPiece.EMPTY) { Win(gs.WhoIsWin()); return; } if (enableNeuroSuccessful) { GameAI.PlaceChessAI(gs, NeuroEvaluateFunction); } else { GameAI.PlaceChessAI(gs, GameAI.EvaluateFunctionWithoutNervus); } DrawChess(gs.X, gs.Y, gs.NextPlayer()); Redraw(); if (gs.WhoIsWin() != ChessPiece.EMPTY) { Win(gs.WhoIsWin()); } } //MessageBox.Show(Pixel2Index(x).ToString() + ' ' + Pixel2Index(y).ToString()); }
/// <summary> /// 迭代器,用于找到当前状态的所有子节点 /// </summary> /// <returns></returns> public IEnumerator <GameState> GetNextSiblingStates() { var newGameState = new GameState(this); int tmp_x = -1, tmp_y = -1; ChessPiece chess = newGameState.turn; for (int i = 0; i < Settings.BOARD_SIZE; i++) { for (int j = 0; j < Settings.BOARD_SIZE; j++) { if (chessboard[i, j] == ChessPiece.EMPTY) { if (tmp_x != -1) { newGameState.chessboard[tmp_x, tmp_y] = ChessPiece.EMPTY; } newGameState.PlaceChess(i, j, chess); tmp_x = i; tmp_y = j; yield return(newGameState); } } } }
/// <summary> /// 迭代器,用于找到当前状态的所有子节点 /// </summary> /// <returns></returns> public IEnumerator<GameState> GetNextSiblingStates() { var newGameState = new GameState(this); int tmp_x = -1, tmp_y = -1; ChessPiece chess = newGameState.turn; for (int i = 0; i < Settings.BOARD_SIZE; i++) { for (int j = 0; j < Settings.BOARD_SIZE; j++) { if (chessboard[i, j] == ChessPiece.EMPTY) { if (tmp_x != -1) newGameState.chessboard[tmp_x, tmp_y] = ChessPiece.EMPTY; newGameState.PlaceChess(i, j, chess); tmp_x = i; tmp_y = j; yield return newGameState; } } } }
/// <summary> /// 使用AI放置棋子 /// </summary> /// <param name="gs">游戏状态</param> /// <param name="ef">评价函数</param> public static void PlaceChessAI(GameState gs, EvaluateFunction ef) { IEnumerator <GameState> ie_gs = gs.GetNextSiblingStates(); int x = 0, y = 0; double value = Settings.LOSE_POINT; double temp_value = 0; while (ie_gs.MoveNext()) { if (IsNessarySearch(ie_gs.Current, ie_gs.Current.X, ie_gs.Current.Y)) { temp_value = MinMax(ie_gs.Current, Settings.SEARCH_DEPTH - 1, Settings.LOSE_POINT, Settings.WIN_POINT, gs.turn, ef); if (temp_value > value) { x = ie_gs.Current.X; y = ie_gs.Current.Y; value = temp_value; } } } //Debug.WriteLine("{0} {1} {2}", x, y, temp_value); gs.PlaceChess(x, y, gs.NextPlayer()); }
/// <summary> /// 使用AI放置棋子 /// </summary> /// <param name="gs">游戏状态</param> /// <param name="ef">评价函数</param> public static void PlaceChessAI(GameState gs, EvaluateFunction ef) { IEnumerator<GameState> ie_gs = gs.GetNextSiblingStates(); int x = 0, y = 0; double value = Settings.LOSE_POINT; double temp_value = 0; while (ie_gs.MoveNext()) { if (IsNessarySearch(ie_gs.Current, ie_gs.Current.X, ie_gs.Current.Y)) { temp_value = MinMax(ie_gs.Current, Settings.SEARCH_DEPTH - 1, Settings.LOSE_POINT, Settings.WIN_POINT, gs.turn, ef); if (temp_value > value) { x = ie_gs.Current.X; y = ie_gs.Current.Y; value = temp_value; } } } //Debug.WriteLine("{0} {1} {2}", x, y, temp_value); gs.PlaceChess(x, y, gs.NextPlayer()); }