Implementation of a Simple Checkers Game Study Tool

Checkers is a game that is easy to learn but hard to master. It requires hours of concentrated study. There are some free Checkers software available on the Web. However, it seems to us that none of them allows easy study of arbitrary game positions, such as the Famous Positions from the U.S. Checkers Federation. In this article, we describe how to design and implement a text-based checkers study program. Most of the time we just follow our intuition with simpleness and correctness in mind. We will see how much intuition can guide us throughout the process. Efficiency is not a goal because the program is used for solitary study only. However, please do contact us if you find a much simpler way to implement the program. We use Perl as the implementation language because it is very easy to construct multi-level data structures with Perl.

Since the program is simple, we don't use any object orientation techniques. However, we don't exclude the possibility of moving to OO style when it helps to manage the complexity should we have more complex requirements.

Before writing any functions, let's briefly describe the notations used in Checkers. The pieces are put in the black squares and the black ones are at the lower three rows and the white ones upper three rows. The positions are numbered from 1 to 32, left to right, top to bottom. So in the beginning, white pieces occupy positions 1-12 and black ones 21-32. We also number the columns 1-8 from left to right and rows 1-8 from top to bottom and use the (row, column) notation as an alternative way to record a piece's location. For example, black piece 9 can be recorded as (3, 2) alternatively.

In the beginning, all the pieces in one side can only move toward the other side. They cannot move backward. However, once they reach the end of the other side, they are promoted to kings and can move both forward and backward.

Since this is a text-based program, we use x to denote an ordinary black piece and X a black king. Similarly, o denotes an ordinary white piece and O a white king. We also use - to denote that a position is empty. So in the beginning, the board looks like this:

   1 2 3 4 5 6 7 8
 1   o   o   o   o  (1-4)
 2 o   o   o   o    (5-8)
 3   o   o   o   o  (9-12)
 4 -   -   -   -    (13-16)
 5   -   -   -   -  (17-20)
 6 x   x   x   x    (21-24)
 7   x   x   x   x  (25-28)
 8 x   x   x   x    (29-32)

Board representation

At this time, we only consider 8x8 board size. However, it can be extended to other sizes as well. We use hash reference variable $board to store all the information about a board:

That's all the information we want to track for a board so far. Accordingly we can define the following functions that manipulate a board.

Now we are ready to implement moves/jumps. There are two types of moves in Checkers. One is normal move that doesn't involve capturing any piece of the opposite color. The other is jump that captures at least one piece of the opposite color. We use normal move and jump to different the two cases. When we state "move," it means either type. There are two things we need to note.

One is that before a piece becomes a king, it cannot move backward. Second, once a piece becomes a king, the move stops. Here we use the variant from the U.S. Checkers Federation.

Similar to board, We use hash reference variable $move to store all the information about a move:

Knowing the $move structure, we can describe some functions that manipulate the moves.

Now we need to glue all the above together so users can interact with the study tool. First we define a function move_piece that accepts a board, a starting position and an ending position and then return the move if it is eligible. For the time being, we just assume that a starting position and an ending position can identify a move uniquely. So we can just call the get_all_moves function with the given board and the starting piece, and then we can iterate through all the moves and return the one that has the same ending position as the given one.

It is very trivial to implement a loop that reads user input and then process it accordingly. We can define q or Q to quit the tool, p or P to print the current board, s <filename> or S <filename> to save the current board to the specified file. Otherwise, a user can specify a move in the format of <start>-<end> and then we can call the move_piece function to check whether the move is eligible and if so update and print the board.

You can download the Perl script for studying the Checkers game and the sample Checkers position file and adapt it to your need.

Back to articles on development