Build a Mini-Chess Board

It's time to start building our adversary! I will work in the Java programming language, although you may choose something different.

The first thing to do is to build a class for game states. The state is the position of pieces on the board, plus a tiny bit more.

The easy way to describe the state is as a 5x6 array of integers. Each element of the array stores what piece (if any) is at that spot on the board.

Build a State class with such a board as a field. Add methods to print the current board to a stream in the style of the previous notes, to read the current board from a stream, and to create a starting board. Include public constants for the pieces including a bit for their color, or just make the array of characters.

The state is a little more than just the board, though. Other things that matter:

Modify your class with new fields to remember these things, and modify your print, read, and init methods to work with a format like
37 W
.....
...k.
.P...
.K...
.....
where the first line says what move number it is and whether White (W) or Black (B) is to move. [Who wins this game with best play?]

Missed Opportunity: There are many other data structures than an array of pieces to save the game state. Some of these ways make the code we will write go much faster. By choosing the simple way early, speeding up later will mean going back to the beginning and starting over.

For example, a "bit board" has a 25-bit integer for each piece, showing where that piece is. This sounds stupid, but now you can compute things by using bitwise operations on bit boards, which fit in machine words. This is very fast compared to array lookups.


prev index next