Fool’s Mate Friday: Getting “board” already?

chess_board

They say that the folding chessboard was invented by a priest who was forbidden to play chess. This priest found a way to hide his game by making a folding chessboard. When folded together and put on a bookshelf, it looked like two ordinary books. Wow! The lengths people will go through, just to play chess!

As we continue adding to our engine, we actually don’t need a visual representation of the board for the engine to work. However, by including it, other open source users who want to use the engine can see how it is implemented. It also makes testing the engine easier. Well, at least more fun, because instead of looking at boring logs that are displaying the board as text, the board is visually represented and we can make sure it is working correctly.

The fourth commit in our series is where we added the graphical pieces. I’d like to take a moment to cite my sources and thank them for creating this really great artwork! Not only that, but they are sharing it under the cc license!

The graphical images of the chess pieces came from:
https://www.behance.net/gallery/10018309/Chess-Artwork-Pieces-and-Board-Art-Assets
https://creativecommons.org/licenses/by/4.0/
https://www.behance.net/DaniDiLena

Then, our fifth commit shows how we draw the board through java, rather than through the xml. This allows for dynamically updating our board at any given time. Notice that after “arranging” the board in the layout file, we assign each block a unique id. With this id, we can put alternating color squares, and then top them with either blanks or pieces.

public class TheUserInterface {
 
 
 
public static void drawBoardPieces() {
 
 
 
for (int i=0; i<64; i++) {
 
 
 
switch (theBoard[i]) {
 
case '*': chessImage[i].setImageResource(R.drawable.empty);
 
break;
 
case 'P': chessImage[i].setImageResource(R.drawable.wp);
 
break;
 
case 'R': chessImage[i].setImageResource(R.drawable.wr);;
 
break;
 
case 'N': chessImage[i].setImageResource(R.drawable.wn);;
 
break;
 
case 'B': chessImage[i].setImageResource(R.drawable.wb);;
 
break;
 
case 'Q': chessImage[i].setImageResource(R.drawable.wq);;
 
break;
 
case 'K': chessImage[i].setImageResource(R.drawable.wk);;
 
break;
 
case 'p': chessImage[i].setImageResource(R.drawable.bp);
 
break;
 
case 'r': chessImage[i].setImageResource(R.drawable.br);;
 
break;
 
case 'n': chessImage[i].setImageResource(R.drawable.bn);;
 
break;
 
case 'b': chessImage[i].setImageResource(R.drawable.bb);;
 
break;
 
case 'q': chessImage[i].setImageResource(R.drawable.bq);;
 
break;
 
case 'k': chessImage[i].setImageResource(R.drawable.bk);;
 
break;
 
}
 
} //Done for loop for drawing board.
 
 
 
} // End draw board pieces
 
 
 
}

Above is the magic that makes adding the pieces a breeze. All this method does is “read” the board, and place an appropriate piece on the number square based on what symbol was in it’s block. For example, the chessImage[] array is holding the unique id of each square of the physical board. It then reads each square of theBoard[] array with the logical board representation, and places white knights on the N squares, and black queens on the q squares. Really simple, really quick. This works well for a style where you don’t have to “move/drag” the pieces, but simply “transport” them from square a to square b.

Linux – keep it simple.

Fool’s Mate Friday: the Just Chess Engine is just getting started…..

There are only 20 first moves in any standard game of chess. 20 choices, 20 options. Statistically, only 10 of those options regularly result in a win, or at worse, a draw among experienced players. 10 of those moves statistically result in a draw or loss. So, choosing that first move is actually one of the most important things you can do in the game of chess. If we extrapolate that, I’m sure there are a lot of tie ins to life itself.

Likewise, when starting to create a chess engine the opening moves are going to set the stage for the remainder of your work. Here’s how I am getting started:

Back to the basics, getting started on the Just Chess Engine. I decided to save my work sequentially to GitHub as I meander through this process to make it easy to follow. This also has bailed me out numerous times when I royally messed up my code, and I simply reverted back with my commits.

The first commit was just making a blank app in Android Studio. In the second commit, I added the board, logically. By logically, I mean what the engine holds as the board. This can differ than what is displayed physically to the user. Depending on the set up of the Android app, whomever is using this engine may also retain their own copy of the board, and simply feed data into the engine.

One might wonder why we would do that, but this could be quite handy. For instance, if I enable the Universal Chess Interface, the app creator may use a Beginning Chess Engine style of board for player interface, or a bitmap board, or whatever style they choose, and just ask the engine for a move suggestion from the computer. So, you need a way to ask the engine what it thinks, and then the engine needs it’s very own board to work with. So, keeping the logical board separate from the physical board is (to me) a good idea.

Finally, the third commit is adding a way to call for a move. At this point, the move call is empty. It doesn’t actually return anything, but sets the stage for when we start populating move options. Here, we call for the engine to tell us “all available moves”. The engine then searches through the 64 squares of the board, and for each character that matches a given symbol (k for king, r for rook, etc.) it will call a method() to check for that pieces available moves.

So far, our engine really doesn’t do much, but we are really just laying the foundation to build the engine from the ground up.

Linux – keep it simple.

 

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.

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.

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.