File size: 817 Bytes
079c32c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/* global BigInt */
export default class ZobristCache {
  constructor(size) {
    this.size = size;
    this.zobristTable = this.initializeZobristTable(size);
    this.hash = BigInt(0);
  }

  initializeZobristTable(size) {
    let table = [];
    for (let i = 0; i < size; i++) {
      table[i] = [];
      for (let j = 0; j < size; j++) {
        table[i][j] = {
          "1": BigInt(this.randomBitString(64)), // black
          "-1": BigInt(this.randomBitString(64))  // white
        };
      }
    }
    return table;
  }

  randomBitString(length) {
    let str = "0b";
    for (let i = 0; i < length; i++) {
      str += Math.round(Math.random()).toString();
    }
    return str;
  }

  togglePiece(x, y, role) {
    this.hash ^= this.zobristTable[x][y][role];
  }

  getHash() {
    return this.hash;
  }
}