public override Move ComputeBestMove() { nodesVisited = 0; betaSkips = 0; Move bestMoveYet = new Move(-1, -1, -100); double score = AlphaBeta(b, MaxAlphaBetaDepth, double.MinValue, double.MaxValue, WhosMove == Colour.White ? 1 : -1, ref bestMoveYet); return bestMoveYet; }
private double AlphaBeta(Board b, int depth, double alpha, double beta, int colour, ref Move bestMoveYet) { if (depth == 0) return b.ScoreBoard(); else { if (colour == 1) { for (int i = 0; i < b.AllMovesCount; i++) { Move move = b.AllMoves[i]; double result = AlphaBeta(b.GetBoardAfterMove(move.From, move.To), depth - 1, alpha, beta, -colour, ref bestMoveYet); nodesVisited++; if (result > alpha) { alpha = result; if (depth == MaxAlphaBetaDepth) bestMoveYet = move; } if (beta <= alpha) { betaSkips++; goto skip1; } } skip1: return alpha; } else if (colour == -1) { for (int i = 0; i < b.AllMovesCount; i++) { Move move = b.AllMoves[i]; double result = AlphaBeta(b.GetBoardAfterMove(move.From, move.To), depth - 1, alpha, beta, -colour, ref bestMoveYet); nodesVisited++; if (result < beta) { beta = result; if (depth == MaxAlphaBetaDepth) bestMoveYet = move; //bestMoveYet = move; } if (beta <= alpha) { betaSkips++; goto skip2; } } skip2: return beta; } else throw new ApplicationException(); } throw new ApplicationException(); }
public override Move ComputeBestMove() { for (int i = 0; i < 100; i++) { int r = rand.Next(0, b.AllMovesCount); Move randMove = new Move(b.AllMoves[r].From, b.AllMoves[r].To, -100); Board boardAfterMove = new Board(b); if (boardAfterMove.MakeMove(randMove.From, randMove.To)) return randMove; } throw new ApplicationException("Can't find possible move"); }
static void Main(string[] args) { string wholeFile = ""; List<Tuple<Board, Move>> allData = new List<Tuple<Board, Move>>(); using(StreamReader fs = new StreamReader(File.OpenRead(".\\Ivanchuk.pgn"))) wholeFile = fs.ReadToEnd(); string[] games = Regex.Split(wholeFile, "\\[Event.*\\]\r\n\r\n"); PgnParser.Parser parser = new PgnParser.Parser(); for (int i = 0; i < 100; i++) { Board board = new Board(); var enu = parser.Parse(games[i]).GetEnumerator(); board.SetupStandardBoard(); while (enu.MoveNext()) { board = board.MakeMove(enu.Current.From, enu.Current.To); allData.Add(new Tuple<Board, Move>(board, enu.Current)); } } Board b = allData[263].Item1; var mostSimilars = allData.Where(x => x.Item1.WhosMove == b.WhosMove). OrderBy(x => Similarity.Calculate(x.Item1, b)).ToList(); Move suggestedMove = new Move(-1, -1); for (int i = 0; i < mostSimilars.Count; i++) { Tuple<Board, Move> tup = mostSimilars[i]; suggestedMove = tup.Item2; if (b.AllMoves.Contains(suggestedMove)) break; } }
private void EnumerateAllMoves() { m_AllMovesCount = 0; for (int i = 0; i < 64; i++) { if (S[i] != null && S[i].Colour == WhosMove) { switch (S[i].PieceType) { case PieceType.Pawn: EnumeratePawnMoves(i); break; case PieceType.Knight: EnumerateKnightMoves(i); break; case PieceType.King: EnumerateKingMoves(i); break; case PieceType.Rook: EnumerateRookMoves(i); break; case PieceType.Bishop: EnumerateBishopMoves(i); break; case PieceType.Queen: EnumerateQueenMoves(i); break; default: throw new ApplicationException(); } for (int j = 0; j < S[i].MoveCnt; j++) { m_AllPossibleMoves[m_AllMovesCount] = new Move(i, S[i].Moves[j], S[i].Powers[j]); m_AllMovesCount++; } } } }