Advertisement
WarPie90

Untitled

Aug 15th, 2023
1,795
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 2.94 KB | None | 0 0
  1.  
  2. type
  3.   TZobristHash = record
  4.     translation: array[#0..#127] of Int8;
  5.     table: array[0..63] of array [0..11] of Int64;
  6.     wtm: Int64;
  7.   end;
  8.   TState    = array [0..127] of char;
  9.  
  10.   EBoardIndex = (a1,b1,c1,d1,e1,f1,g1,h1,  wck, wcq, bck, bcq,  ep, wtm,wkng,bkng,
  11.                  a2,b2,c2,d2,e2,f2,g2,h2, __a2,__b2,__c2,__d2,__e2,__f2,__g2,__h2,
  12.                  a3,b3,c3,d3,e3,f3,g3,h3, __a3,__b3,__c3,__d3,__e3,__f3,__g3,__h3,
  13.                  a4,b4,c4,d4,e4,f4,g4,h4, __a4,__b4,__c4,__d4,__e4,__f4,__g4,__h4,
  14.                  a5,b5,c5,d5,e5,f5,g5,h5, __a5,__b5,__c5,__d5,__e5,__f5,__g5,__h5,
  15.                  a6,b6,c6,d6,e6,f6,g6,h6, __a6,__b6,__c6,__d6,__e6,__f6,__g6,__h6,
  16.                  a7,b7,c7,d7,e7,f7,g7,h7, __a7,__b7,__c7,__d7,__e7,__f7,__g7,__h7,
  17.                  a8,b8,c8,d8,e8,f8,g8,h8, __a8,__b8,__c8,__d8,__v0,__v1,__v2,__v3);
  18.  
  19. const
  20.   BOARD_INDICES: set of EBoardIndex = [a1..h1, a2..h2, a3..h3, a4..h4, a5..h5, a6..h6, a7..h7, a8..h8];
  21.  
  22.  
  23. // Initialize Zobrist keys with random values
  24. procedure TZobristHash.Init();
  25. var
  26.   i, j: Integer;
  27. begin
  28.   for i:=0 to 63 do
  29.     for j:=0 to 11 do
  30.       Self.table[i, j] := Random($7FFFFFFFFFFFFFFF);
  31.   Self.wtm := Random($7FFFFFFFFFFFFFFF);
  32.  
  33.   translation['K'] := 0;
  34.   translation['Q'] := 1;
  35.   translation['R'] := 2;
  36.   translation['B'] := 3;
  37.   translation['N'] := 4;
  38.   translation['P'] := 5;
  39.   translation['k'] := 6;
  40.   translation['q'] := 7;
  41.   translation['r'] := 8;
  42.   translation['b'] := 9;
  43.   translation['n'] := 10;
  44.   translation['p'] := 11;
  45. end;
  46.  
  47. // Calculate the Zobrist key for a given position
  48. function TZobristHash.Hash(board: TState): Int64;
  49. var
  50.   i: EBoardIndex;
  51.   x: Int32;
  52. begin
  53.   Result := 0;
  54.   for i in BOARD_INDICES do
  55.     if board[i] <> '.' then
  56.     begin
  57.       x := Int8(i) - (Int8(i) div 16) * 8;
  58.       Result := Result xor Self.table[x, self.translation[board[i]]];
  59.     end;
  60. end;
  61.  
  62. function TZobristHash.Update(hash: Int64; i: Int32; p:Char): Int64;
  63. var x: Int32;
  64. begin
  65.   x := Int8(i) - (Int8(i) div 16) * 8;
  66.   Result := hash xor self.table[x, self.translation[p]];
  67. end;
  68.  
  69.  
  70. var
  71.   z: TZobristHash;
  72.  
  73.   str: string =
  74.         'RNB.Q.NR'+ #1#1#1#1#0#1#0#0 +
  75.         'PPPKPPBP'+ #0#0#0#0#0#0#0#0 +
  76.         '...P..P.'+ #0#0#0#0#0#0#0#0 +
  77.         '........'+ #0#0#0#0#0#0#0#0 +
  78.         '........'+ #0#0#0#0#0#0#0#0 +
  79.         '...bpq..'+ #0#0#0#0#0#0#0#0 +
  80.         'ppppnppp'+ #0#0#0#0#0#0#0#0 +
  81.         'rnb.k..r'+ #0#0#0#0#0#0#0#0;
  82.  
  83.   s: TState;
  84.   h: Int64;
  85. begin
  86.   MemMove(str[1], s[0], 128);
  87.  
  88.   z.Init();
  89.  
  90.   h := z.Hash(s);
  91.   WriteLn('The Board hash => ', h);
  92.  
  93.   // let's move a pawn from a6 to a7
  94.   WriteLn h := z.Update(h, Int8(a6), 'p');  // xor out the pawn at a6
  95.   WriteLn h := z.Update(h, Int8(a7), 'p');  // xor in a pawn at a7
  96.  
  97.  
  98.   WriteLn h := z.Update(h, Int8(a7), 'p'); // xor out the pawn at a7
  99.   WriteLn h := z.Update(h, Int8(a6), 'p'); // xor in a pawn at a6
  100.  
  101.   // ^ should be back to previous state, cheap hashing!
  102. end.
  103.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement