Kill the King

A web app that calculates forced checkmates in a chess position

Background

I was always fascinated by the idea of building a chess related app. I do play chess casually, and being able to acurately describe the game in abstract terms seems an exciting design challenge. The huge, (but not infinite![1]) number of possible positions adds a spicy performance touch in the mix.

I was mainly interested in the deterministic side of the game, were I wouldn't have to delve into the depths of AI and statistics. Checkmate analysis is a great candidate since

  • Checkmating is the objective goal of the game
  • There is a plethora of checkmate in N moves puzzles where the app could find a practical use

The goal of the app is finding all moves that result in a forced checkmate for a given position and depth.

A forced checkmate occurs when the attacking player can checkmate for every possible move of their opponent.

Its depth is the maximum number of moves the opponent can make before a checkmate occurs.

Checkmate in 2

Let's assume that white plays first in the above position. Ra7 results to a forced checkmate (and effectively wins the game), since

  • if black plays Kg8, white can play Rb8 => checkmate
  • if black plays Kg7, white can play Rb8 => checkmate

Implementation

There are two aspects of the implementation that particularly interested me:

  • Describing the board and pieces using Object Oriented Programming
  • Using a fast and accurate algorithm to allow for bigger move depth analysis

There are incredible chess engines out there that I could use to my advantage. I decided to "reinvent the wheel" here for the fun and knowledge that comes out of discovery. Describing the relationships between each piece and the board proved to be a great class design exercise. The next step would be investigate ways to improve the performance and allow for a maximum move depth.

Back-end

The language of choice is Java 8, which offered me a good balance between familiarity of syntax and performance. Interesting aspects of the implementation include

  • A fluent interface approach in the Traverser class, which is responsible for moving pieces around
  • Good unit testing coverage with the usage of JUnit 5 and Mockito
  • (De)serializing objects to/from json using Jackson, for easier front-end integration

Front-end

The usual suspects React.js and Redux.js form the front-end core. I chose Material-UI for styling/components, and I have to say it provides a great developer experience. React DnD proved handy for moving pieces around the board.

I used Cypress for my first attempt to automated end-to-end tests. I got a great impression out of it: it is a modern tool, with great documentation and a plethora of best practice guidelines and tips. I also found the Cypress courses in https://egghead.io very helpful.

Deployment

Kill the King is fully deployed in AWS:

  • The front-end is deployed in S3 buckets
  • The back-end is exposed via lambda functions
  • The CI/CD pipeline is built on CircleCI, and executes linting checks, unit tests and end-to-end tests

Future Improvements 🚀

  • Make the app mobile friendly
  • Increase the maximum analysis depth
  • Simplify the UX and add more options (undo/redo buttons, save results etc.)

[1] How many chess games are possible?, YouTube video about the Shannon number (spoiler: at least 10120 😲 )