Menu

Concise code using ChessHelper

Alcibiade

The ChessHelper class provides methods allowing to directly apply a move on a given position, returning the destination position. This is used to avoid having to apply all board updates in a nested loop.

@Autowired
private ChessRules chessRules;

@Autowired
private PgnMarshaller pgnMarshaller;

@Test
public void testGame() throws PgnMoveException, IllegalMoveException {
    String[] history = {"f3", "e5", "g4", "Qh4"};
    ChessPosition position = chessRules.getInitialPosition();

    for ( String pgnMove: history) {
        ChessMovePath path = pgnMarshaller.convertPgnToMove(position, pgnMove);
        position = ChessHelper.applyMove(chessRules, position, path);
    }

    System.out.println(position);
}

This will output:

Running org.alcibiade.chess.integration.ShortestGameTest
WHITE KQkq
r n b . k b n r 
p p p p . p p p 
. . . . . . . . 
. . . . p . . . 
. . . . . . P q 
. . . . . P . . 
P P P P P . . P 
R N B Q K B N R

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.856 sec

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.