From the git-go, one of the hardest parts of making this chess engine and the following gui system has always been the pawns and the king. Both of these pieces have unusual maneuvers that you can’t easily explain with the traditional shorthand that I was using for the engine. Things like castling, en passant, and promotions.

If you recall last time, I made the pieces movable with a simple two touch interface. The problem is, it checks against the engine to see if that is a legitimate move in the form of piece+square from+square to+captured. This works great for queens and bishops, rooks or knights, but doesn’t bode well when someone tries to castle. This brought me to a fork in the road: change my engine, or manipulate my gui.

I think I should have changed my engine, but I already re-wrote it from scratch, twice. I just don’t have the time to do that again. So, I went with the easier/quicker/less skilled method of manipulating the gui.

Here’s the commit:

public void moveablePiece (View view) {

    // Get the clicked squares tag to see what number it is.
    int number = Integer.parseInt(view.getTag().toString());
    String played;
    if (number < 10) {
        played = "0" + String.valueOf(number);
    } else {
        played = String.valueOf(number);

    if (firstClick) {

        String myMove = tryMove + played + String.valueOf(theBoard[number]);
        Log.i("WJH", myMove);

        if (myMove.equalsIgnoreCase("K0406*")){myMove="K-0-0R";}
        else if (myMove.equalsIgnoreCase("K0402*")){myMove="K0-0-0";}
        else if (myMove.equalsIgnoreCase("k6062*")){myMove="k-0-0r";}
        else if (myMove.equalsIgnoreCase("k6058*")){myMove="k0-0-0";}
        Log.i("WJH", myMove);
        moveOptions= terminal("availMoves,"+String.valueOf(wTurn));

        String[] separated = moveOptions.split(",");

        if (Arrays.asList(separated).contains(myMove)) {

            String query = terminal("myMove,"+myMove);
            Log.i("WJH", query);
            wTurn = !wTurn;

        tryMove = "";
        myMove = "";

    } else {

        tryMove = String.valueOf(theBoard[number]) + played;
        Log.i("WJH", tryMove);
        String query = terminal("pieceMoves,"+ String.valueOf(theBoard[number]) +
                "," + played);

} // End clicked piece.

Essentially, I know there are only 4 possible ways for a king to castle, so I make the gui check to see if that is the case, and then pass the correct move verbiage to the engine. Pretty slick trick for the moment, but it may fall short for en passant. I’ll have to check on that…

Linux – keep it simple.

Leave a Reply

Your email address will not be published. Required fields are marked *