All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.fathzer.chess.utils.transposition.GenerationDetector Maven / Gradle / Ivy

The newest version!
package com.fathzer.chess.utils.transposition;

import java.util.function.Predicate;

/** An abstract generation detector that triggers a new generation if a pawn is moved or a capture occurred.
 */
public abstract class GenerationDetector implements Predicate {
	private long lastHash;
	
	@Override
	public boolean test(B t) {
		final long hash = getHash(t);
		if (hash==lastHash) {
			// Seems the position didn't changes
			return false;
		}
		lastHash = hash;
		return getHalfMoveCount(t)<2;
	}
	
	/** Gets the hash key of a board.
	 * @param board The board
	 * @return a long
	 */
	protected abstract long getHash(B board);
	
	/** Gets the current half moves counter of a board.
	 * @param board The board
	 * @return an positive or null int
	 */
	protected abstract int getHalfMoveCount(B board);
}