| | 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 | | { |
| 5621412 | 16 | | BoardHistories[_historyIndex++] = new BoardHistory |
| 5621412 | 17 | | { |
| 5621412 | 18 | | CapturedPiece = capturedPiece, |
| 5621412 | 19 | | EnPassantSquare = enPassant, |
| 5621412 | 20 | | CastlingRights = originalCastlingRights, |
| 5621412 | 21 | | BoardHash = boardHash, |
| 5621412 | 22 | | LastDrawKiller = lastDrawKiller, |
| 5621412 | 23 | | BestMove = bestMove |
| 5621412 | 24 | | }; |
| 5621412 | 25 | | } |
| | 26 | |
|
| | 27 | | public static BoardHistory RestoreBoardHistory() |
| | 28 | | { |
| 5621093 | 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 | | { |
| 15182000 | 40 | | public Piece CapturedPiece { get; set; } |
| 11242505 | 41 | | public Square EnPassantSquare { get; set; } |
| 11242505 | 42 | | public Castle CastlingRights { get; internal set; } |
| 11244742 | 43 | | public ulong BoardHash { get; set; } |
| 11242505 | 44 | | public int LastDrawKiller { get; set; } |
| 11122068 | 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 | | } |