zobristkeyimpl.java

来自「chess 一个beguanyu国际象棋的一个Java源码」· Java 代码 · 共 128 行

JAVA
128
字号
/*  ZobristKeyImpl - Implements Zobrist keys for plies.  Copyright (C) 2003 The Java-Chess team <info@java-chess.de>  This program is free software; you can redistribute it and/or  modify it under the terms of the GNU General Public License  as published by the Free Software Foundation; either version 2  of the License, or (at your option) any later version.  This program 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 General Public License for more details.  You should have received a copy of the GNU General Public License  along with this program; if not, write to the Free Software  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.*/ package de.java_chess.javaChess.engine.hashtable;import de.java_chess.javaChess.bitboard.*;import de.java_chess.javaChess.board.*;import de.java_chess.javaChess.piece.*;import de.java_chess.javaChess.position.*;import java.util.*;/** * This class implements the computation of a Zobrist key. */public class ZobristKeyImpl {    // Static variables    /**     * The only instance of this class (singleton pattern).     */    private static ZobristKeyImpl _instance = null;    /**     * The random numbers for the key computation.     */    private long [] [] [] _factors;    /**     * The factor for black moves.     */    private long _blackMoves;    // Instance variables        // Constructors    /**     * Create a new key instance.     */    private ZobristKeyImpl() {	// Create a new multidimensional array.	_factors = new long[2][6][64];	Random rand = new Random();	for( int i = 0; i < 64; i++) {	    for( int j = 0; j < 6; j++) {		_factors[0][j][i] = rand.nextLong();		_factors[1][j][i] = rand.nextLong();	    }	}	_blackMoves = rand.nextLong();    }    // Methods    /**     * Get the only instance of this class.     */    public static final ZobristKeyImpl getInstance() {	if( _instance == null) {	    _instance = new ZobristKeyImpl();	}	return _instance;    }    /**     * Compute a key for a board and a color.     *     * @param board The current board.     * @param white Flag to indicate if white moves.     *     * @return The zobrist key.     */    public long computeKey( BitBoard board, boolean white) {	long val = 0L;	// Get a bitmask of the empty squares to speedup the 	// test for a piece on the square.	long emptySquareMask = board.getEmptySquares();	// Reuse the same position object for each square.	Position curPosition = new PositionImpl( 0);	for( int square = 0; square < 64; square++) {	    if( ( emptySquareMask & 1) == 0) {  // If there's a piece on the square		curPosition.setSquareIndex( square);		Piece p = board.getPiece( curPosition);				if( p != null) {		    val ^= _factors[p.getColor()][p.getType() - 1][square];		}	    }	    emptySquareMask >>>= 1;  // Shift the mask to test for the next square.	}	if( ! white) {	    val ^= _blackMoves;	}	return val;    }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?