| | 1 | | using Rudim.Board; |
| | 2 | | using Rudim.CLI; |
| | 3 | | using Rudim.Common; |
| | 4 | | using System; |
| | 5 | | using System.Collections.Generic; |
| | 6 | | using System.Diagnostics; |
| | 7 | | using System.Linq; |
| | 8 | | using System.Threading; |
| | 9 | |
|
| | 10 | | namespace Rudim.Search |
| | 11 | | { |
| | 12 | | public static class IterativeDeepening |
| | 13 | | { |
| | 14 | | public static Move BestMove; |
| | 15 | | public static int Score; |
| | 16 | | public static int Nodes; |
| | 17 | |
|
| | 18 | | public static void Search(BoardState boardState, int depth, CancellationToken cancellationToken, ref bool debugM |
| | 19 | | { |
| 5 | 20 | | Stopwatch timer = new Stopwatch(); |
| 5 | 21 | | BestMove = Move.NoMove; |
| 5 | 22 | | Score = 0; |
| 5 | 23 | | Nodes = 0; |
| 94 | 24 | | for (int i = 1; i <= depth; ++i) |
| | 25 | | { |
| 43 | 26 | | timer.Restart(); |
| 43 | 27 | | Score = Negamax.Search(boardState, i, cancellationToken); |
| | 28 | |
|
| 43 | 29 | | if (cancellationToken.IsCancellationRequested) |
| | 30 | | break; |
| | 31 | |
|
| 42 | 32 | | BestMove = boardState.BestMove; |
| 42 | 33 | | int nodesTraversed = Negamax.Nodes + Quiescence.Nodes; |
| 42 | 34 | | Nodes += nodesTraversed; |
| | 35 | |
|
| 42 | 36 | | timer.Stop(); |
| 42 | 37 | | double time = Math.Max(timer.ElapsedMilliseconds, 1); |
| 42 | 38 | | int nps = (int)(Nodes / time * 1000); |
| | 39 | |
|
| 42 | 40 | | List<Move> pv = TranspositionTable.CollectPrincipalVariation(boardState); |
| 42 | 41 | | string pvString = string.Join(' ', pv.Select(move => |
| 146 | 42 | | move.Source.ToString() + move.Target.ToString() + move.GetPromotionChar())); |
| | 43 | |
|
| 42 | 44 | | if (debugMode) |
| | 45 | | { |
| 0 | 46 | | CliClient.WriteLine($"info depth {i} score cp {Score} nodes {nodesTraversed} time {time} nps {nps} p |
| | 47 | | } |
| | 48 | | } |
| 5 | 49 | | } |
| | 50 | | } |
| | 51 | | } |