Build a MiniChess 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 piece values. Each element of the array stores what piece (if any) is at that spot on the board.

You can use anything you like for piece values. Some folks use integers, and create symbolic constants. I suggest using characters: 'r', 'n' etc. This can make debugging easier. You will need to keep track of the color of each piece. When using characters, it is normal to have white's pieces be upper case and black's pieces be lower case.

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.

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

  • Which side is "on move" (gets to move next)
  • What move it is, for the draw rule.

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

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 30-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 operations.