| | | 1 | | using Rudim.Board; |
| | | 2 | | using Rudim.Common; |
| | | 3 | | using System.Collections.Generic; |
| | | 4 | | using System.Threading; |
| | | 5 | | |
| | | 6 | | namespace Rudim.Search |
| | | 7 | | { |
| | | 8 | | public static partial class Negamax |
| | | 9 | | { |
| | | 10 | | public static int Nodes; |
| | | 11 | | private static int _searchDepth; |
| | | 12 | | |
| | | 13 | | public static int Search(BoardState boardState, int depth, CancellationToken cancellationToken) |
| | | 14 | | { |
| | 43 | 15 | | _searchDepth = depth; |
| | 43 | 16 | | Nodes = 0; |
| | 43 | 17 | | Quiescence.ResetNodes(); |
| | 43 | 18 | | int score = Search(boardState, depth, int.MinValue + 1, int.MaxValue - 1, true, cancellationToken); |
| | 43 | 19 | | return score; |
| | | 20 | | } |
| | | 21 | | |
| | | 22 | | private static int Search(BoardState boardState, int depth, int alpha, int beta, bool allowNullMove,Cancellation |
| | | 23 | | { |
| | 1845707 | 24 | | int ply = _searchDepth - depth; |
| | 1845707 | 25 | | bool isPvNode = beta - alpha > 1; |
| | 1845707 | 26 | | Nodes++; |
| | | 27 | | |
| | 1845707 | 28 | | if (boardState.IsDraw()) |
| | 0 | 29 | | return 0; |
| | | 30 | | |
| | 1845707 | 31 | | (bool hasValue, int ttScore, Move ttBest) = TranspositionTable.GetEntry(boardState.BoardHash, alpha, beta, d |
| | 1845707 | 32 | | if (hasValue) |
| | | 33 | | { |
| | | 34 | | // TODO: This doesn't seem right - revisit TT impl |
| | 78672 | 35 | | boardState.BestMove = ttBest; |
| | 78672 | 36 | | return TranspositionTable.RetrieveScore(ttScore, ply); |
| | | 37 | | } |
| | | 38 | | |
| | 1767035 | 39 | | if (depth <= 0) |
| | 1240057 | 40 | | return Quiescence.Search(boardState, alpha, beta, cancellationToken); |
| | | 41 | | |
| | 526978 | 42 | | if (CanPruneNullMove(isPvNode, boardState, allowNullMove, depth)) |
| | | 43 | | { |
| | 120437 | 44 | | boardState.MakeNullMove(); |
| | 120437 | 45 | | int score = -Search(boardState, depth - 1 - 2, -beta, -beta + 1, false, cancellationToken); |
| | 120437 | 46 | | boardState.UndoNullMove(); |
| | 120437 | 47 | | if (score >= beta) |
| | 87438 | 48 | | return beta; // TODO : Store in TT |
| | | 49 | | } |
| | | 50 | | |
| | | 51 | | int originalAlpha = alpha; |
| | 439540 | 52 | | bool foundPv = false; |
| | 439540 | 53 | | TranspositionEntryType entryType = TranspositionEntryType.Alpha; |
| | | 54 | | |
| | 439540 | 55 | | boardState.GenerateMoves(); |
| | 439540 | 56 | | PopulateMoveScores(boardState, ply); |
| | | 57 | | |
| | 439540 | 58 | | int numberOfLegalMoves = 0; |
| | 439540 | 59 | | List<Move> moves = boardState.Moves; |
| | 4071522 | 60 | | for(int i = 0; i < moves.Count; ++i) |
| | | 61 | | { |
| | 1983194 | 62 | | MoveOrdering.SortNextBestMove(moves, i); |
| | 1983194 | 63 | | Move move = moves[i]; |
| | 1983194 | 64 | | if (cancellationToken.IsCancellationRequested) |
| | | 65 | | break; |
| | 1983190 | 66 | | boardState.MakeMove(move); |
| | 1983190 | 67 | | if (boardState.IsInCheck(boardState.SideToMove.Other())) |
| | | 68 | | { |
| | 258827 | 69 | | boardState.UnmakeMove(move); |
| | 258827 | 70 | | continue; |
| | | 71 | | } |
| | | 72 | | |
| | 1724363 | 73 | | int score = SearchDeeper(boardState, depth, alpha, beta, cancellationToken, foundPv, allowNullMove); |
| | | 74 | | |
| | 1724363 | 75 | | numberOfLegalMoves++; |
| | | 76 | | |
| | 1724363 | 77 | | boardState.UnmakeMove(move); |
| | 1724363 | 78 | | if (score >= beta) |
| | | 79 | | { |
| | 386969 | 80 | | return BetaCutoff(beta, move, ply, boardState, depth); |
| | | 81 | | } |
| | 1337394 | 82 | | if (score > alpha) |
| | | 83 | | { |
| | 1970 | 84 | | AlphaUpdate(score, move, boardState, depth, out alpha, out foundPv, out entryType); |
| | | 85 | | } |
| | | 86 | | } |
| | | 87 | | |
| | 52571 | 88 | | if (numberOfLegalMoves == 0) |
| | | 89 | | { |
| | 103 | 90 | | if (boardState.IsInCheck(boardState.SideToMove)) |
| | 103 | 91 | | return -Constants.MaxCentipawnEval + ply; |
| | 0 | 92 | | return 0; |
| | | 93 | | } |
| | | 94 | | |
| | 52468 | 95 | | TranspositionTable.SubmitEntry(boardState.BoardHash, TranspositionTable.AdjustScore(alpha, ply), depth, boar |
| | | 96 | | |
| | 52468 | 97 | | return alpha; |
| | | 98 | | } |
| | | 99 | | |
| | | 100 | | private static int SearchDeeper(BoardState boardState, int depth, int alpha, int beta, |
| | | 101 | | CancellationToken cancellationToken, bool foundPv, bool allowNullMove) |
| | | 102 | | { |
| | | 103 | | int score; |
| | 1724363 | 104 | | if (foundPv) |
| | | 105 | | { |
| | 25569 | 106 | | score = PrincipalVariationSearch(boardState, depth, alpha, beta, allowNullMove, cancellationToken); |
| | | 107 | | } |
| | | 108 | | else |
| | | 109 | | { |
| | 1698794 | 110 | | score = -Search(boardState, depth - 1, -beta, -alpha, allowNullMove, cancellationToken); |
| | | 111 | | } |
| | 1724363 | 112 | | return score; |
| | | 113 | | } |
| | | 114 | | } |
| | | 115 | | } |