Fool’s Mate Friday: A friend helps speed things up….

January 30th, 2004 marked the Guinness world record for the largest networked chess computer. Using the internet to connect with 2070 computers, this networked conglomerate challenged Grand Master Peter Heine Nielsen, and the game ended in a draw!

All that to say that sometimes, we all need a little help. A “phone a friend”, if you will.

As you know, I have been working on the Beginning Chess Engine, an open source engine based in part on a tutorial by Logic Crazy, and have adapted for use on Android phones. Since the start, I have been trying to focus my efforts on making a fully functional and also responsive engine.

Unfortunately, what I have in place right now has a few bugs and is very slow. My Samsung Galaxy S4 can only handle a ply of 3 reliably, with an occasional ply of 4. Even then, these moves can take up to 3 minutes to perform, which is very slow for very little gain, by way of intelligence. However, I just received some great help from a fellow enthusiast, and GitHub-er HenriDellal.

He proposed using editable string buffer’s rather than immutable strings to speed up the computations, and it made a big difference, shaving about 10-20% off of the time needed to calculate the moves!

Using strings, which are immutable (unchanging) caused my old code to create an entirely new string, rather than just edit the existing one. The string buffer can be changed on the fly, speeding the process up immensely. Thanks HenriDellal!

You can see the commit here.

Linux – keep it simple.

Advertisements

Geany: the little IDE that could!

geany_ide.png

Recently, while working with some older equipment, I needed a lightweight java IDE to compile some java projects. I really wasn’t looking for something quite as big as eclipse or netbeans, I just wanted a simple, one stop solution that would allow me to build java programs on an older laptop.

So, I rubbed a magic lamp, and out popped Geany!

Well, more specifically, the laptop is running Debian Wheezy, and had openJDK already installed. With that alone, I could have built java programs from the command line, but it can get a little tedious. So, I looked on the repositories for an IDE that wasn’t too large.

Thus enter Geany. I first used Geany a long time ago as a text editor on #! (CrunchBang) Linux, but revisiting the program revealed that I was not using it to it’s full potential. Specifically, you can compile, build, and run various program types from within Geany. Of course, it only outsources the commands for you, so you need a java jdk in order to compile java, or a c++ compiler to compile c++. Geany just does all the command line work for you.

What Geany does accomplish for you, however, is a great user interface with templates and the ability to organize projects or view code with different filters. As you can see in the picture, I was testing out Logic Crazy’s Alpha Beta Chess program.

As well as showing you the code, it can do parenthesis highlighting, colors for different code type, and a class and method tree on the side. It really is a great program if you need a lightweight IDE. I know that I’ll be putting it to good use.

Linux – keep it simple.

Fool’s Mate Friday!

Occasionally, I do a theme, or idea, on a given day for a period of time. Currently, I have been spending quite a bit of time working on chess engines and their play. So, I am going to create a “Fool’s Mate Friday” post. That’s right, every Friday, I’ll be bringing some small tidbit about programming chess engines, or chess games, or chess play in general. So, stay tuned for Fool’s Mate Friday!

This does bring up two important questions, though:

#1. What is a fool’s mate? Well, a fool’s mate is technically a check mate that happens in 2 moves. However, by extension, it can be used to refer to any set of opening moves that lead you to a checkmate very early in the game. Typically, it is specifically a reference to being checkmated by making poor moves on the fool’s part, rather than tactful moves on the winner’s part.

fools_mate

Technically, this would require 3 moves, and is thus a fool’s mate by extension, rather than by definition. 1. e3 f6 2. Qe2 g5 3. Qh5#.

#2. Why name it “Fool’s Mate Friday”? Two reasons, really. I needed a chess related term that started with F, and this is fairly accurate, as I am an unrated player of low standing (and understanding) in the game of chess, and my programming skills are not that good yet. Both of these lead me to make many mistakes, both in the game and in the programming thereof, which is certainly on par with the fool’s mate.

Linux – keep it simple.

Checking the internet connection speed from the command line

Typically, if we want to check the internet connection speed, we can just use a web browser with any of the usual internet speed test sites. However, the other day I actually needed to test the internet connection speed on a machine without using a browser.

In this case the machine was old and the browser did not support flash, nor HTML5, which really cut down on available websites. A quick Google search led me to some options. If you are like me, you know how to use Google too, however, like myself, you may run into several options that lead to depreciated links, or missing files. So I thought I’d save you the trouble and post the working answer here.

I wrote a bash script to make it simple, you can see the contents of it here:

user@crunchbang:~$ cat checkinternetspeed.sh

#!/bin/bash

wget -O – https://raw.githubusercontent.com/sivel/speedtest-cli/master/speedtest.py –no-check-certificate | python

exit 0

And after giving it the proper execution attributes, I simply run the script:

user@crunchbang:~$ ./checkinternetspeed.sh

–2017-10-07 08:17:04–  https://raw.githubusercontent.com/sivel/speedtest-cli/master/speedtest.py
Resolving raw.githubusercontent.com… 151.101.52.133
Connecting to raw.githubusercontent.com|151.101.52.133|:443… connected.
WARNING: certificate common name “www.github.com” doesn’t match requested host name “raw.githubusercontent.com”.
HTTP request sent, awaiting response… 200 OK
Length: 47228 (46K) [text/plain]
Saving to: “STDOUT”

100%[========================================================================>] 47,228       116K/s   in 0.4s

2017-10-07 08:17:05 (116 KB/s) – written to stdout [47228/47228]

Retrieving speedtest.net configuration…
Testing from GCI (206.174.115.164)…
Retrieving speedtest.net server list…
Selecting best server based on ping…
Hosted by GCI (Anchorage, AK) [424.45 km]: 32.192 ms
Testing download speed……………………………………………………………………..
Download: 2.62 Mbit/s
Testing upload speed……………………………………………………………………………………
Upload: 3.58 Mbit/s
user@crunchbang:~$

Wow! 2.6 Mbit download speed! I was really cruising today! That’s a really simple way to check your internet connection speed from the command line, if you ever need it.

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.