Fool’s Mate Friday: The JustChessEngine

Sometimes, you just have to step out in a new direction. In the 1920’s, chess players began to hold to a new style of play, then called hypermodernism. In this phase, conventional moves were called into question, and they began to invent interesting new ways to control the center of the board, without moving the king’s or queen’s pawns. Similarly, I need to re-evaluate the beginner’s chess engine. Question the conventional method that I am currently using, and step out in a new direction. Is that good or bad? Only time will tell.

While writing the Beginner’s Chess Engine, I came to realize that there were a lot of changes that needed to be made to that engine to really speed it up. These changes would be extremely time consuming to implement by direct editing, and called for a whole new engine scheme. Again, I wanted to create a chess engine that was open source, for anyone to use, as well as keep it simple, small, and workable for Android cell phones. Thus enter the JustChessEngine.

Ironically, if you build it from the repository, it will look identical to the Beginner Chess Engine graphically, as I used the same graphical material to create it. However, under the hood is a whole new animal.

You can check out the new repository at https://github.com/alaskalinuxuser/JustChessEngine.

The big change from the git-go is ditching the array of string arrays that make up the board. Arrays are slow in general, and having them in this immutable fashion required multiple rewrites of the board while making a move. So an array is slow, and the Beginning Chess Engine has 8 arrays arrayed in one for the board. This is slow x 8, inside of a slow array. So, I ditched that and started with one array of characters.

Char[] (character arrays) can be edited on the fly, without rewriting the whole array again. As of this writing, the JustChessEngine outperforms the Beginning Chess Engine in speed tests at various ply by three to one! That’s a big gain! Be sure to check it out and feel free to leave comments.

Linux – keep it simple.

Added the ability to castle to the Beginner Chess Engine

For better or worse, I added the ability to castle to the Beginner Chess Engine.

device-2017-10-04-111213

In theory, that would always be better. However, adding this feature actually caused two problems:

Problem #1 – Even though the castle works great, as expected, and is successful, occasionally, on the NEXT move of the side that castled, then the rook can turn into a king.

Problem #2 – The king cannot castle in check, nor to check, but can currently castle across check, which is an illegal move.

Hmm…. back to the drawing board I guess. You can check out what I did by looking at the commits.

Linux – keep it simple.

Beginner Chess Engine: Solved the pawn/king check problem….

If you’ve been following along, you probably have seen that the king had a slight problem. It was failing to adequately check for pawns when considering it’s own safety.

Here you can see the commit where I fixed the code. Now it will accurately check for a pawn attack in the space it is moving to!

//pawn
 + try {
 + if (“p”.equals(chessBoard[kingPositionC/8+(-1)][(kingPositionC%8)+1])) {
 + return false;
 + }
 + if (“p”.equals(chessBoard[kingPositionC/8+(-1)][kingPositionC%8+(-1)])) {
 + return false;
 + }
 + } catch (Exception e) {}

I also added a TextView display to show the user how long the computer took to “think” about the last move. It helps to know how much time the computer is spending on each move, especially when compared to the number of potential moves available.

Linux – keep it simple.

Added a display for the list of possible moves to the Beginner Chess Engine

Well, if you were wondering what I was doing with my spare time, this is it!

possibleMoves

I just added a TextView to display the move options that the computer had available. Not real fancy, but it allows me to “watch” what’s going on without the debugger running. You can check out the commit here:

https://github.com/alaskalinuxuser/BeginnerChessEngine/commit/c81721cc0d6e8d301e4924d4d2f0de233c06b744

Linux – keep it simple.

Added reset button to the Beginning Chess Engine app, but it doesn’t do what you think….

As you can see, I added a reset button to the app for the Beginning Chess Engine.

added_reset

Originally, I had intended to have the board simply reset back to the starting position. However, for some odd reason, when I would reset the board pieces, odd things would happen….

Like a rook would randomly turn into a king when you move the knight.

So, I decided to have the app close, and then call itself to be opened again a few milliseconds later. It’s a little odd, since the app “closes” and then “opens” again, so it looks like it crashed, only to pop up again.

If this were my chess playing app, I would need to focus in on that a little more, however, I am trying to focus on the engine itself, not the remnants that are somehow misconstrued during recreating the original board.

Here’s the commit:

https://github.com/alaskalinuxuser/BeginnerChessEngine/commit/3f6aae2c2e5327d341e15cb50c9d702dc4356fd7

And one issue closed! Only 9+ more to go!

Linux – keep it simple.

Beginner chess engine has a new repository!

The beginner chess engine has a brand new repository so I can more accurately track issues. Before, it was a small part of a larger repository holding different programs, but I have so many issues that I wasn’t to track, that it became difficult to use in that manner.

So, I’ve opened a fresh repository for it:

https://github.com/alaskalinuxuser/BeginnerChessEngine

Be sure to check it out and see all of the issues that I am tracking! Also, of you try it out, you are welcome to add issues as well.

Note: This beginner chess engine is just a simple chess engine that is programmed by a beginner (me), but also recognize that the goal is a drop in engine that is open source for other programmers to use to make their own chess games on Android (or perhaps other) devices.

With that in mind, the app is supposed to allow controls for the engine tester to try things out, and adjust settings for use in their projects. Not so much for the user to play chess, although I hope to make a chess playing app in the future as well.

Linux – keep it simple.

The trouble with knights….

The trouble with knights….

In the continuing saga of making my own chess engine for Android, I’ve run into a peculiarity: If the king is in check, and the only move is a knight to capture the enemy to save the king, the app crashes. So, I added a try/catch block to the command to keep it from crashing. Then, instead of crashing, it would give me an error of the string being out of bounds. I placed the below log.i commands in to see the value of each character, so I could find the problem.

What I saw was that the king piece has no moves, so it was returning no moves at all. Odd, because if I insert a bishop to solve the problem, it would. The real issue still lies with the knight not reporting a valid move. Ironically, if I removed all other pieces, the knight can capture the enemy piece. Very, very odd….

[QUOTE]
public static void makeMove(String move) {
/*public static void makeMove(String move) {
int xf,yf,xt,yt;
//x1,y1,x2,y2,captured piece
if (move.charAt(4)!=’C’ && move.charAt(4)!=’P’) {
{
xt=Character.getNumericValue(move.charAt(2));
}
yt=Character.getNumericValue(move.charAt(3));
xf=Character.getNumericValue(move.charAt(0));
yf=Character.getNumericValue(move.charAt(1));
}
}*/
Log.i(“WJH”, String.valueOf(move.charAt(1)));
Log.i(“WJH”, String.valueOf(move.charAt(2)));
Log.i(“WJH”, String.valueOf(move.charAt(3)));
Log.i(“WJH”, String.valueOf(move.charAt(4)));

if (move.length() < 4) {
Log.i(“WJH”, “That piece cannot move.”);
} else {

if (move.charAt(4) != ‘P’) {
chessBoard[Character.getNumericValue(move.charAt(2))][Character.getNumericValue(move.charAt(3))] = chessBoard[Character.getNumericValue(move.charAt(0))][Character.getNumericValue(move.charAt(1))];
chessBoard[Character.getNumericValue(move.charAt(0))][Character.getNumericValue(move.charAt(1))] = ” “;
if (“K”.equals(chessBoard[Character.getNumericValue(move.charAt(2))][Character.getNumericValue(move.charAt(3))])) {
kingPositionC = 8 * Character.getNumericValue(move.charAt(2)) + Character.getNumericValue(move.charAt(3));
}
} else {
//if pawn promotion
chessBoard[1][Character.getNumericValue(move.charAt(0))] = ” “;
chessBoard[0][Character.getNumericValue(move.charAt(1))] = String.valueOf(move.charAt(3));
}
}
} // End makeMove
[/QUOTE]

Then, I decided to disable the kingsafe() check protocol, as seen here:
[QUOTE]
public static String posibleN(int i) {
String list=””, oldPiece;
int r=i/8, c=i%8;
for (int j=-1; j<=1; j+=2) {
for (int k=-1; k<=1; k+=2) {
try {
if (Character.isLowerCase(chessBoard[r+j][c+k*2].charAt(0)) || ” “.equals(chessBoard[r+j][c+k*2])) {
oldPiece=chessBoard[r+j][c+k*2];
chessBoard[r][c]=” “;
//if (kingSafe()) {
list=list+r+c+(r+j)+(c+k*2)+oldPiece;
//}
chessBoard[r][c]=”N”;
chessBoard[r+j][c+k*2]=oldPiece;
}
} catch (Exception e) {}
try {
if (Character.isLowerCase(chessBoard[r+j*2][c+k].charAt(0)) || ” “.equals(chessBoard[r+j*2][c+k])) {
oldPiece=chessBoard[r+j*2][c+k];
chessBoard[r][c]=” “;
//if (kingSafe()) {
list=list+r+c+(r+j*2)+(c+k)+oldPiece;
//}
chessBoard[r][c]=”N”;
chessBoard[r+j*2][c+k]=oldPiece;
}
} catch (Exception e) {}
}
}
return list;
} // End possible knight moves.
[/QUOTE]

By disabling kingsafe(), the knight was then able to move and capture the enemy piece that was threatening the king. Odd. Very odd. In the kingSafe() function, it is split for bishop/queen, and rook/queen, so I thought I would check to see if this is a bishop/queen problem, or a more general issue. Turns out it is not a specific bishop/queen attack problem, because a rook/queen attack also does not allow the knight to capture the attacker.

If I disable the kingSafe() check for the knight, then I proved the knight would move to capture a rook, only to open up an attack from a bishop. That obviously will not do. However, a very, very careful review of the rook’s code, versus the knight’s code revealed the mistake! See the code here:

[QUOTE]
++++++++++++++++++ KNIGHT +++++++++++++++++++++++++++
try {
if (Character.isLowerCase(chessBoard[r+j][c+k*2].charAt(0)) || ” “.equals(chessBoard[r+j][c+k*2])) {
oldPiece=chessBoard[r+j][c+k*2];
chessBoard[r][c]=” “;
//if (kingSafe()) {
list=list+r+c+(r+j)+(c+k*2)+oldPiece;
//}
chessBoard[r][c]=”N”;
chessBoard[r+j][c+k*2]=oldPiece;
}
}
++++++++++++++++++ ROOK ++++++++++++++++++++
if (Character.isLowerCase(chessBoard[r+temp*j][c].charAt(0))) {
oldPiece=chessBoard[r+temp*j][c];
chessBoard[r][c]=” “;
chessBoard[r+temp*j][c]=”R”;
if (kingSafe()) {
list=list+r+c+(r+temp*j)+c+oldPiece;
}
chessBoard[r][c]=”R”;
chessBoard[r+temp*j][c]=oldPiece;
}
[/QUOTE]

Notice that the rook “flow chart” as it were:
if the enemy is a bad guy, or blank, then {
write down what was there.
make the rook’s previous position empty.
place the rook in the new position.
NOW CHECK IF KING IS SAFE.
IF IT IS, MAKE THAT A PERMANENT MOVE.
IF NOT, PUT IT BACK.

Now look at the “flow chart for the knight:
if the enemy is a bad guy, or blank, then {
write down what was there.
make the knight’s previous position empty.

NOW CHECK IF KING IS SAFE.
IF IT IS, MAKE THAT A PERMANENT MOVE.
IF NOT, PUT IT BACK.

So, as you can see, the knight is not in the “new” position, thus the old piece is still there! With a quick re-write of that portion of code, then the knight worked as expected!

[QUOTE]
public static String posibleN(int i) {
String list=””, oldPiece;
int r=i/8, c=i%8;
for (int j=-1; j<=1; j+=2) {
for (int k=-1; k<=1; k+=2) {
try {
if (Character.isLowerCase(chessBoard[r+j][c+k*2].charAt(0)) || ” “.equals(chessBoard[r+j][c+k*2])) {
oldPiece=chessBoard[r+j][c+k*2];
chessBoard[r][c]=” “;
chessBoard[r+j][c+k*2]=”N”;
if (kingSafe()) {
list=list+r+c+(r+j)+(c+k*2)+oldPiece;
}
chessBoard[r][c]=”N”;
chessBoard[r+j][c+k*2]=oldPiece;
}
} catch (Exception e) {}
try {
if (Character.isLowerCase(chessBoard[r+j*2][c+k].charAt(0)) || ” “.equals(chessBoard[r+j*2][c+k])) {
oldPiece=chessBoard[r+j*2][c+k];
chessBoard[r][c]=” “;
chessBoard[r+j*2][c+k]=”N”;
if (kingSafe()) {
list=list+r+c+(r+j*2)+(c+k)+oldPiece;
}
chessBoard[r][c]=”N”;
chessBoard[r+j*2][c+k]=oldPiece;
}
} catch (Exception e) {}
}
}
return list;
} // End possible knight moves.
[/QUOTE]

Believe it or not, that was actually the most fun I have ever had programming in Java! Now I just have to solve the other ump-teen problems! Be sure to check out the commit here: https://github.com/alaskalinuxuser/apps_small/commit/0cd506f76578267c027b742e0de40fda392fd163.

Linux – keep it simple.