
com.fathzer.chess.utils.model.IBoard Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of chess-test-utils Show documentation
Show all versions of chess-test-utils Show documentation
Some JUnit abstract test classes to test chess related libraries.
The newest version!
package com.fathzer.chess.utils.model;
import java.util.List;
/** An interface representing a board.
* @param the type of the move
*/
public interface IBoard {
/** Gets the list of moves available on the board.
*
The board is free to return legal or pseudo legal moves.
* @return the list of moves
*/
List getMoves();
/** Returns true if this IBoard's getMoves() method returns legal moves.
* @return true if this IBoard's getMoves() method returns legal moves, false (the default value) otherwise (Typically, if it returns pseudo legal moves)
*/
default boolean isGetMovesLegal() {
return false;
}
/** Converts the UCI representation of a move to a move instance.
*
By default, this method returns a move instance from the list returned by {@link #getMoves()}. If the move is not in the list, an {@link IllegalArgumentException} is thrown.
*
There is no guarantee that this method is called only with legal or pseudo-legal moves.
*
If the move is illegal, the method should throw an {@link IllegalArgumentException}.
* @param uciMove the move in UCI format
* @return a pseudo legal (or legal, see {@link #isGetMovesLegal()}) move instance.
* @throws IllegalArgumentException if move is not legal
*/
default M toMove(String uciMove) {
return getMoves().stream().filter(m -> toUCI(m).equals(uciMove)).findFirst().orElseThrow(() -> new IllegalArgumentException("Move " + uciMove + " is not legal"));
}
/**
* Converts a move to its UCI representation.
*
The default implementation returns the move's {@link Object#toString() toString()} value.
*
Subclasses must override this method to return the move's UCI representation.
* @param move the move to convert
* @return the UCI representation of the move
*/
default String toUCI(M move) {
return move.toString();
}
/** Makes a move on the board.
*
It is guaranteed that this method is called only with legal moves or moves returned by {@link #getMoves()}.
* @param mv the move to make
* @return true if the move was legal, false otherwise. If the move was illegal, the board is not modified.
*/
boolean makeMove(M mv);
/** Undoes the last move made on the board.
*/
void unmakeMove();
/** Checks if the given move is legal.
*
By default, this method returns true if {@link #isGetMovesLegal()} returns true or if {@link #makeMove(Object)} returns true.
* @param move The move to check
* @return true if the move is legal, false otherwise
*/
default boolean isLegal(M move) {
if (isGetMovesLegal()) {
return true;
} else {
final boolean result = makeMove(move);
if (result) {
unmakeMove();
}
return result;
}
}
/** Checks if the given uci move is legal.
*
By default, this method returns true if the move is a move returned by {@link #getMoves()} and @link #makeMove(Object)} returns true.
*
Warning: this method requires that the equals
method is overridden to return true for moves are equivalent.
* @param uci The move to check
* @return true if the move is legal, false otherwise
*/
default boolean isLegal(String uci) {
final M move;
try {
move = toMove(uci);
} catch (IllegalArgumentException e) {
return false;
}
return isLegal(move);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy