<commit_before>/*
 * Copyright 2013-2014 the original author or authors.
 *
 * This file is part of Pulse Chess.
 *
 * Pulse Chess is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Pulse Chess is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with Pulse Chess.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.fluxchess.pulse;

import com.fluxchess.jcpi.models.GenericBoard;
import com.fluxchess.jcpi.models.GenericColor;
import com.fluxchess.jcpi.models.GenericFile;
import com.fluxchess.jcpi.models.GenericPiece;

import java.security.SecureRandom;

/**
 * This is our internal board.
 */
public final class Board {

  private static final int MAX_GAMEMOVES = Search.MAX_HEIGHT + 1024;

  private static final int BOARDSIZE = 128;

  public final int[] board = new int[BOARDSIZE];

  public final Bitboard[] pawns = new Bitboard[Color.values.length];
  public final Bitboard[] knights = new Bitboard[Color.values.length];
  public final Bitboard[] bishops = new Bitboard[Color.values.length];
  public final Bitboard[] rooks = new Bitboard[Color.values.length];
  public final Bitboard[] queens = new Bitboard[Color.values.length];
  public final Bitboard[] kings = new Bitboard[Color.values.length];

  public final int[][] colorCastling = new int[Color.values.length][Castling.values.length];
  public int enPassant = Square.NOSQUARE;
  public int activeColor = Color.WHITE;
  public int halfMoveClock = 0;
  private int halfMoveNumber;

  public long zobristCode = 0;
  private static final long[][] zobristPiece = new long[Piece.values.length][BOARDSIZE];
  private static final long[][] zobristCastling = new long[Color.values.length][Castling.values.length];
  private static final long[] zobristEnPassant = new long[BOARDSIZE];
  private static final long zobristActiveColor;

  // We will save some board parameters in a State before making a move.
  // Later we will restore them before undoing a move.
  private final State[] stack = new State[MAX_GAMEMOVES];
  private int stackSize = 0;

  private static final class State {
    public long zobristCode = 0;
    public final int[][] castling = new int[Color.values.length][Castling.values.length];
    public int enPassant = Square.NOSQUARE;
    public int halfMoveClock = 0;

    public State() {
      for (int color : Color.values) {
        for (int castling : Castling.values) {
          this.castling[color][castling] = File.NOFILE;
        }
      }
    }
  }

  private static final class Zobrist {
    private final SecureRandom random = new SecureRandom();

    byte[] result() {
      // Generate some random bytes for our keys
      byte[] bytes = new byte[16];
      random.nextBytes(bytes);
      return bytes;
    }

    public long next() {
      byte[] result = result();

      long hash = 0L;
      for (int i = 0; i < result.length; ++i) {
        hash ^= ((long) (result[i] & 0xFF)) << ((i * 8) % 64);
      }

      return hash;
    }
  }

  // Initialize the zobrist keys
  static {
    Zobrist zobrist = new Zobrist();

    for (int piece : Piece.values) {
      for (int i = 0; i < BOARDSIZE; ++i) {
        zobristPiece[Piece.ordinal(piece)][i] = Math.abs(zobrist.next());
      }
    }

    zobristCastling[Color.WHITE][Castling.KINGSIDE] = Math.abs(zobrist.next());
    zobristCastling[Color.WHITE][Castling.QUEENSIDE] = Math.abs(zobrist.next());
    zobristCastling[Color.BLACK][Castling.KINGSIDE] = Math.abs(zobrist.next());
    zobristCastling[Color.BLACK][Castling.QUEENSIDE] = Math.abs(zobrist.next());

    for (int i = 0; i < BOARDSIZE; ++i) {
      zobristEnPassant[i] = Math.abs(zobrist.next());
    }

    zobristActiveColor = Math.abs(zobrist.next());
  }

  public Board(GenericBoard genericBoard) {
    assert genericBoard != null;

    // Initialize stack
    for (int i = 0; i < stack.length; ++i) {
      stack[i] = new State();
    }

    // Initialize piece type lists
    for (int color : Color.values) {
      pawns[color] = new Bitboard();
      knights[color] = new Bitboard();
      bishops[color] = new Bitboard();
      rooks[color] = new Bitboard();
      queens[color] = new Bitboard();
      kings[color] = new Bitboard();
    }

    // Initialize board
    for (int square : Square.values) {
      board[square] = Piece.NOPIECE;
<commit_msg>Fix Zobrist Hashing<commit_after>/*
 * Copyright 2013-2014 the original author or authors.
 *
 * This file is part of Pulse Chess.
 *
 * Pulse Chess is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Pulse Chess is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with Pulse Chess.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.fluxchess.pulse;

import com.fluxchess.jcpi.models.GenericBoard;
import com.fluxchess.jcpi.models.GenericColor;
import com.fluxchess.jcpi.models.GenericFile;
import com.fluxchess.jcpi.models.GenericPiece;

import java.security.SecureRandom;

/**
 * This is our internal board.
 */
public final class Board {

  private static final int MAX_GAMEMOVES = Search.MAX_HEIGHT + 1024;

  private static final int BOARDSIZE = 128;

  public final int[] board = new int[BOARDSIZE];

  public final Bitboard[] pawns = new Bitboard[Color.values.length];
  public final Bitboard[] knights = new Bitboard[Color.values.length];
  public final Bitboard[] bishops = new Bitboard[Color.values.length];
  public final Bitboard[] rooks = new Bitboard[Color.values.length];
  public final Bitboard[] queens = new Bitboard[Color.values.length];
  public final Bitboard[] kings = new Bitboard[Color.values.length];

  public final int[][] colorCastling = new int[Color.values.length][Castling.values.length];
  public int enPassant = Square.NOSQUARE;
  public int activeColor = Color.WHITE;
  public int halfMoveClock = 0;
  private int halfMoveNumber;

  public long zobristCode = 0;
  private static final long[][] zobristPiece = new long[Piece.values.length][BOARDSIZE];
  private static final long[][] zobristCastling = new long[Color.values.length][Castling.values.length];
  private static final long[] zobristEnPassant = new long[BOARDSIZE];
  private static final long zobristActiveColor;

  // We will save some board parameters in a State before making a move.
  // Later we will restore them before undoing a move.
  private final State[] stack = new State[MAX_GAMEMOVES];
  private int stackSize = 0;

  private static final class State {
    public long zobristCode = 0;
    public final int[][] castling = new int[Color.values.length][Castling.values.length];
    public int enPassant = Square.NOSQUARE;
    public int halfMoveClock = 0;

    public State() {
      for (int color : Color.values) {
        for (int castling : Castling.values) {
          this.castling[color][castling] = File.NOFILE;
        }
      }
    }
  }

  private static final class Zobrist {
    private final SecureRandom random = new SecureRandom();

    byte[] result() {
      // Generate some random bytes for our keys
      byte[] bytes = new byte[16];
      random.nextBytes(bytes);
      return bytes;
    }

    public long next() {
      byte[] result = result();

      long hash = 0L;
      for (int i = 0; i < result.length; ++i) {
        hash ^= ((long) (result[i] & 0xFF)) << ((i * 8) % 64);
      }

      return hash;
    }
  }

  // Initialize the zobrist keys
  static {
    Zobrist zobrist = new Zobrist();

    for (int piece : Piece.values) {
      for (int i = 0; i < BOARDSIZE; ++i) {
        zobristPiece[Piece.ordinal(piece)][i] = zobrist.next();
      }
    }

    zobristCastling[Color.WHITE][Castling.KINGSIDE] = zobrist.next();
    zobristCastling[Color.WHITE][Castling.QUEENSIDE] = zobrist.next();
    zobristCastling[Color.BLACK][Castling.KINGSIDE] = zobrist.next();
    zobristCastling[Color.BLACK][Castling.QUEENSIDE] = zobrist.next();

    for (int i = 0; i < BOARDSIZE; ++i) {
      zobristEnPassant[i] = zobrist.next();
    }

    zobristActiveColor = zobrist.next();
  }

  public Board(GenericBoard genericBoard) {
    assert genericBoard != null;

    // Initialize stack
    for (int i = 0; i < stack.length; ++i) {
      stack[i] = new State();
    }

    // Initialize piece type lists
    for (int color : Color.values) {
      pawns[color] = new Bitboard();
      knights[color] = new Bitboard();
      bishops[color] = new Bitboard();
      rooks[color] = new Bitboard();
      queens[color] = new Bitboard();
      kings[color] = new Bitboard();
    }

    // Initialize board
    for (int square : Square.values) {
      board[square] = Piece.NOPIECE;

{'fraction_non_alphanumeric': 0.1111935552257791, 'fraction_numerical': 0.0067839728641085435, 'mean_word_length': 3.292538671519563, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2264036', 'n_tokens_mistral': 3022, 'n_tokens_neox': 2877, 'n_words': 914}