| | | 1 | | using Rudim.Common; |
| | | 2 | | using System; |
| | | 3 | | |
| | | 4 | | namespace Rudim.Board |
| | | 5 | | { |
| | | 6 | | public static class History |
| | | 7 | | { |
| | | 8 | | private const int HistorySize = 4096; |
| | 1 | 9 | | private static readonly BoardHistory[] BoardHistories = new BoardHistory[HistorySize]; |
| | | 10 | | private static int _historyIndex; |
| | | 11 | | |
| | | 12 | | |
| | | 13 | | public static void SaveBoardHistory(Piece capturedPiece, Square enPassant, Castle originalCastlingRights, |
| | | 14 | | ulong boardHash, int lastDrawKiller, Move bestMove) |
| | | 15 | | { |
| | 5630140 | 16 | | BoardHistories[_historyIndex++] = new BoardHistory |
| | 5630140 | 17 | | { |
| | 5630140 | 18 | | CapturedPiece = capturedPiece, |
| | 5630140 | 19 | | EnPassantSquare = enPassant, |
| | 5630140 | 20 | | CastlingRights = originalCastlingRights, |
| | 5630140 | 21 | | BoardHash = boardHash, |
| | 5630140 | 22 | | LastDrawKiller = lastDrawKiller, |
| | 5630140 | 23 | | BestMove = bestMove |
| | 5630140 | 24 | | }; |
| | 5630140 | 25 | | } |
| | | 26 | | |
| | | 27 | | public static BoardHistory RestoreBoardHistory() |
| | | 28 | | { |
| | 5629821 | 29 | | return BoardHistories[--_historyIndex]; |
| | | 30 | | } |
| | | 31 | | |
| | | 32 | | public static void ClearBoardHistory() |
| | | 33 | | { |
| | 11 | 34 | | Array.Clear(BoardHistories); |
| | 11 | 35 | | _historyIndex = 0; |
| | 11 | 36 | | } |
| | | 37 | | |
| | | 38 | | public class BoardHistory |
| | | 39 | | { |
| | 15202924 | 40 | | public Piece CapturedPiece { get; set; } |
| | 11259961 | 41 | | public Square EnPassantSquare { get; set; } |
| | 11259961 | 42 | | public Castle CastlingRights { get; internal set; } |
| | 11262198 | 43 | | public ulong BoardHash { get; set; } |
| | 11259961 | 44 | | public int LastDrawKiller { get; set; } |
| | 11139523 | 45 | | public Move BestMove { get; set; } |
| | | 46 | | } |
| | | 47 | | |
| | | 48 | | public static bool HasHashAppearedTwice(ulong boardHash, int startingIndex) |
| | | 49 | | { |
| | 210 | 50 | | int count = 0; |
| | 4890 | 51 | | for (int i = _historyIndex - 1; i >= startingIndex; --i) |
| | | 52 | | { |
| | 2237 | 53 | | if (BoardHistories[i].BoardHash == boardHash) |
| | 11 | 54 | | count++; |
| | | 55 | | |
| | 2237 | 56 | | if (count == 2) |
| | 2 | 57 | | return true; |
| | | 58 | | } |
| | | 59 | | |
| | 208 | 60 | | return false; |
| | | 61 | | } |
| | | 62 | | public static bool IsHistoryEmpty() |
| | | 63 | | { |
| | 2 | 64 | | return _historyIndex == 0; |
| | | 65 | | } |
| | | 66 | | } |
| | | 67 | | } |