Week 2 – Creating Checkers
In my second Senior Project blog post, I will go over the process for making the checkers game. I was able to get started with making the game as part of my final project for my Advanced Java Topics Capstone in class, though there are still some bugs that needed to be fixed. I made this game using the Pygame library, which let me use Python to write the code, while also allowing me to easily implement graphics and user interaction.
Making The Game
There were three major steps to make the game. First I needed to make the board, including being able to store every piece on the board, along with keep track of the number of pieces left. Second I needed to make the pieces, each with a location, color, and whether or not it’s a king (kings can move forward and backward). The last step is the most complicated, needing to allow pieces to move. For this, I need to create a method that checks each diagonal that a piece is able to move along. Single moves are simple enough, but in checkers, if a piece captures another, it is able to move another time. Implementing that makes this method much more complicated.
Drawing The Board
The first step of this project was to create the board. Checkers is played on an 8×8 checkerboard, typically with red and black squares alternating. Since the actual board never changes, just the pieces on it, my method to draw the board was fairly simple. It draws the board in black first, filling in alternating squares as red.
Because I don’t want a piece to appear in its original location and new location after being moved, I’ll be calling a draw method every time the board updates. Pygame graphics continuously draw on top of what already exists, so drawing a piece in its new location will still leave the image of the piece in its original position. Because I’m calling the draw method again, the new board would get drawn on top of the old board. The old board is now completely covered up, so I can pretend it just doesn’t exist.
Creating Pieces To Play With
This step of creating the pieces wasn’t too difficult. For now all I needed was images for them, as well as a method to draw them. I used a large red or black circle, with two smaller, non-filled-in circles to add some detail. As for king pieces, I tried my best to draw a crown on them. Additionally, the images also have a background with the same color as the black squares on the board. Pygame draws images using the top left corner, so this ensures that the squares with pieces on them are the same color as those without.
Once I had the images of the pieces, I needed them drawn on the board. Each piece had a row, column, and color attribute, so I was able to create a draw method for the pieces. I went back to the board’s draw method, and added code to draw each piece as well.
With the board and pieces drawn, the last step is to allow the pieces to move on the board. I added a move method to the pieces, allowing them to move to a new row and column. As long as the new location is a valid move, the piece will be able to move there. I also need to add a way for the player to actually interact with the board. For this, I used Pygame in order to get the mouse’s x-coordinate and y-coordinate when clicked. I then used these coordinates do calculate which squares a user clicked. If an unselected piece was clicked, it would get selected, with a blue square drawn around it. And if a square was clicked while a piece was already selected, the piece would be moved to that square.
Now that pieces are able to move, I need to ensure that the move is actually valid. In addition to checking if a move is valid, I need to keep track of any captured pieces so they can be removed. While the code for this was probably the most complicated part of making the game, its explanation is fairly straightforward. Since pieces can only go left or right, I checked for possible moves in that direction. If a capture was made, I checked for moves to the left and right from that spot, as well as kept track of the capture. If a path of moves had nowhere else to go, I stored that move and checked the other paths. Once a user chose to make a move, I would check if it was valid. If it was, the piece would get moved, and any captured pieces would be removed.
Final Steps And Fixing Bugs
The last thing to do was ensure the “game” part of this project works. This involved keeping track of whose turn it is, and ending the game when a player runs out of pieces. A few minor changes later, and two people are now able to play checkers against each other.
The game mostly works at this point, though there are still some bugs I found while testing it, and spent much of this week trying to fix. Pieces are able to double-jump (to capture two pieces in a move), but not triple-jump. Kings, which can move in multiple directions, aren’t able to double-jump forward and backward on the same turn. Additionally, if there are two ways to double-jump to the same square (left-right and right-left), the game won’t allow either, and the only available option becomes a single-jump.
The final bug, and only one I could completely fix, was the game not ending when a player has no valid moves available on any of their pieces. In checkers, a player loses if it’s their turn and they can’t move any pieces. To fix this, I added on to the original method to check for a winner. Rather than just checking for a player having no pieces left, I also checked for if at least of the remaining pieces had moves available.
 Pygame Front Page — pygame v2.0.0.dev15 documentation. (n.d.). www.pygame.org. https://www.pygame.org/docs/