Ive got troubles When using the Chesspresso 0.9.2 method Position#doMove(short move), in order to add moves. Illegal moves seems to be accepted as well as legal moves, though the method Game#doMove() should throw IllegalMoveException
Here is my experimentation class :
package com.gmail.bernabe.laurent.j2se.chesspresso_test;
import javax.swing.JFrame;
import chesspresso.Chess;
import chesspresso.game.Game;
import chesspresso.game.view.GameBrowser;
import chesspresso.move.IllegalMoveException;
import chesspresso.move.Move;
public class ChessFrame extends JFrame {
public ChessFrame(){
setTitle("Chesspresso gamebrowser test");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
add(gameBrowser);
pack();
addMove(Chess.E2, Chess.E4, false, "Debut pour une partie ouverte");
addMove(Chess.E7, Chess.E5, false, "Reponse symetrique forte");
addMove(Chess.G1, Chess.F3, false, null);
addMove(Chess.B8, Chess.C6, false, null);
addMove(Chess.F1, Chess.B5, false, null);
}
private void addMove(int fromSquareIndex, int toSquareIndex,
boolean isCapturingMove, String comment){
try {
short move = Move.getRegularMove(fromSquareIndex, toSquareIndex,
isCapturingMove);
if (Move.isValid(move)) {
chessGame.getPosition().doMove(move);
if (comment != null && comment.length() > 0)
chessGame.addComment(comment);
}
} catch (IllegalMoveException e) {
e.printStackTrace();
}
}
private Game chessGame = new Game();
private GameBrowser gameBrowser = new GameBrowser(chessGame);
private static final long serialVersionUID = -6856238414376776882L;
}
And here is my main class :
package com.gmail.bernabe.laurent.j2se.chesspresso_test;
public class ChesspressoGraphicalTest {
public static void main(String[] args) {
ChessFrame chessFrame = new ChessFrame();
chessFrame.setVisible(true);
}
}
As you can see, I’ve inherited from the ChessPresso GameBrowser class in my JFrame custom class, in order to navigate in the chess opening I’ve coded (Ruy Lopez). And the method addMove wraps all the Chesspresso calls in order to add move and comment just in one line.
If you launch the class ChesspressoGraphicalTest, you will see a Frame which displays the board with letters for representing the piece (that is the default Chesspresso GameBrowser implementation and behaviour) : you can read moves, go to next move, going back to old move … thanks to the buttons at bottom of board.
So, what’s the problem ? It is very simple : when giving an illegal move (
addPawnMove(Chess.E2, Chess.F8, false, "Debut pour une partie ouverte");
for example in place of the first move), this one isn’t detected at all. I did not get any IllegalMoveException.
Furthermore, if I remove the test Move.isValid(move) (in the addMove() method), nothing ever change : whatever legal status of all moves are.
Is it chesspresso problem, or a simple bad use of it ?
Any help appreciated. Thanks.
The Move.isValidMove(move) checks if the move itself has been created as a valid move or invalid move. It does not check if the move itself is valid during some time of the game.
What you need to do is to use the position of the game to get the list of valid moves (don’t create the moves yourself). You can also use chessGame.getPosition().getPieceMove(…) which will return either a valid or invalid move.