Queen’s Gambit

Loved The Queen’s Gambit? Learn To Code Your Own Chess Engine

Sololearn
Sololearn
Published in
7 min readOct 15, 2021

--

Certain TV shows have captured the collective hearts (and eyes) of the millions of people working and staying at home during the pandemic. Starting with Tiger King, a number of shows have exploded in popularity, and perhaps none bigger than The Queen’s Gambit. Based on the novel by Walter Tevis, The Queen’s Gambit follows fictional chess prodigy Beth Harmon as she battles her own demons (and some of the best chess players in the world) on her quest to become a world champion.

The story itself is captivating, but the massive enthusiasm for chess that has followed the series has been equally impressive. Thousands upon thousands of people bought up chess boards online, and Chess.com has seen an explosion in new users and active players for its popular digital chess app and desktop software. This rise in interest for quality chess apps and software has led a number of programmers to start considering building their own chess engine, to create a new platform for people to practice their chess openings.

But maybe you are a programmer wondering how to make a chess board of your own, or how to use programming languages you are currently mastering to create your ideal chess engine. In this guide, we’ll look at how The Queen’s Gambit can help serve as an inspiration for designing your own chess app, and building it to match the realistic needs and conditions of high level chess to deliver a quality experience for your potential app users. Let’s explore some of the common questions surrounding coding your own chess app, and some answers to help you begin your brainstorming and coding process.

What Programming Language Should I Choose?

As with any software project, different programming languages offer their own benefits for building an ideal chess engine. C++ is often the most popular choice, since it offers a variety of features needed to program AI algorithms for your computer chess players and features the same simplicity that has driven the language’s widespread use for years.

For those looking for some modern alternatives, many also look to more recent languages like Ruby and Python, which also feature intuitive structures that make programming pieces, moves, and responses that mirror the actual games featured in The Queen’s Gambit. In simple terms, Python is probably the best choice for simplicity, while Java offers benefits for portability, and C++ stands out for programmers who want the most efficient option.

Player VS. Player or Player VS. AI?

Obviously, building a player-versus-player game involves a bit less work, since you aren’t responsible for building an AI that can mirror the skill and expertise of the Russian masters that Beth must overcome during the course of The Queen’s Gambit. Player-versus-player games require more focus on the programming elements needed for simulating an in-person game, such as a chat window for interacting, timers for players to play against, and a user interface that makes move making and tracking easy.

For player-versus-AI games, you will need to build in “decision making” elements that allow for your AI to respond at different skill levels to simulate a true chess-playing experience. This will involve creating things like search trees for your algorithm to select the best available move, alpha-beta pruning to allow the algorithm to eliminate unnecessary and needless moves, and other decision-making back-end coding to give your AI a human-like element to their decisions. The complexity of this type of coding is also why certain languages may be more preferable for you to use than others.

What Art Should You Use For The Visual Interface?

The concept of an interaction between a chess engine and its user interface is similar to the interaction between a car engine and the car frame around it. Put simply, an engine doesn’t do any good without the rest of the car. You need wheels, a place to sit, a steering wheel, brakes, etc., to actually operate an engine or, in this case, actually play a chess game. The GUI program provides a way for the user to interact with the engine, which does all the actual work.

There are many different GUIs to choose from, some paid and some free. Here are a few of the most popular ones that are currently being used by programmers:

  • Lucas Chess GUI
  • Arena Chess GUI
  • Tarrasch Chess GUI
  • SCID vs. PC Chess Program
  • ChessBase Reader 12

What Skills Do I Need To Learn?

While there are plenty of programming skills needed for different individual elements of the chess game you are building, there are some essential steps that any programmer needs to be able to accomplish to develop a truly Queen’s Gambit-level professional chess engine:

Create A Playable Chess Board

Fortunately, there are plenty of out-of-the-box solutions available for any of the common programming languages throughout the open-source hubs on the web

Who Is Playing

Decide whether you want to build an app for player-versus-player games, player-versus-AI games, or both (and the level of complexity required for the option you choose)

Machine Learning Or No

Create a chess engine without machine learning OR create a machine-learning chess engine

Significant knowledge of the basics of one of the key chess programming languages like Ruby, Python, or C++ is also a prerequisite.

Translating Chess Rules Into Game Logic

  • Start with a “board” object, that will be translated into a visual representation of a standard chess board (with the appropriate squares, different pieces, and different move types that are featured in a standard physical chess game)
  • Develop a process for board evaluation. In other words, create a way for a position on the chess board to be evaluated if it is won by one side or if it is a draw. If neither of these conditions is satisfied, you will also need logic that can estimate the probabilities for each side and for each subsequent move. In a simple implementation, the estimate is done via two factors: the “material” (pieces on board) and the positions of the pieces. For each piece type, different values are calculated depending on the squares the pieces are located, which is done by using so-called “piece-square” tables.
  • Develop a “lookahead” process for your chess engine to evaluate future moves and navigate move “decision trees”. This is the logic that will allow the AI to respond like a human chess player (and allow for different AI difficulty levels by tweaking the ability to interpret future moves accurately and effectively).

Building The Logic Behind The Game

The easiest way to think about the logic needed for a chess game that will let you step into Beth Harmon’s shoes is to examine the different classes needed for a chess game to function:

Spot

A spot will represent one block of the 8×8 grid as well as optional pieces.

Piece

The basic building block of any chess engine, every piece will be placed on a spot. This makes Piece class an abstract class. The extended classes (Pawn, King, Queen, Rook, Knight, Bishop) allows the player and AI to implement the abstracted operations.

Board

Board is an 8×8 set of boxes containing all active chess pieces, mirroring a traditional physical chess board.

Player

Player class represents one of the participants playing the game, and can be tailored to either human players or AI.

Move

Represents a game move, containing the starting and ending spot. The Move class will also keep track of the player who made the move, allowing the app to record moves just as actual players write down moves, as seen in The Queen’s Gambit.

Game

This class controls the actual flow of a game. It allows the app to keep track of all the game moves, which player has the current turn (and thus, which timer should be ticking down), as well as the final result of the game.

There are codified strategies for playing chess, which is where The Queen’s Gambit gets its name — how do you embed those strategies into the game experience?

Building In Game Strategy

Building an AI that can make its own moves isn’t excessively difficult, but if you want your AI to mimic the actual strategies of expert players like those featured in The Queen’s Gambit (since all of the games in the show mirror real games that happened at major tournaments in history), you will need to add some more advanced logic to your AI back-end, including:

  • Move-ordering logic, which can help an AI perform more strategically based on probabilities and historical strategies as an expert human player would
  • Faster move generation, which allows an AI to put time pressure on a human player as a real player might
  • Specific end-game evaluation, which can allow the AI to examine a board with different end-game options in mind and play strategically (or mirror human strategy) to deliver a real-life competitive experience

There are obviously intricate coding processes needed to get to this level of logic, and programmers developing high-end chess apps will often take painstaking time to code in logic that mirrors actual historical games. Fortunately, there are a ton of chess-building wikis that have collected many of these shortcuts and programming processes for you to draw from.

Key Points About Chess UI

In summary, there are some essential elements of any good chess UI that future coders and programmers learning to code right now should focus on mastering to build their own game in the future:

  • Becoming familiar with how the classes noted above work, and the specific elements needed to get them each to function properly and realistically
  • Exploring the web for pre-built solutions (like the board and pieces, for example) that can save you coding time and allow you to cobble together your game by programming needed logic
  • Understanding the benefits and limitations of whatever particular language you are learning, and what the language offers for building a chess UI specifically
  • Choosing the type of chess app you want to offer (player versus player, player versus AI etc) and the unique programming needs required to make that app function properly

--

--

Sololearn
Sololearn

The #1 platform to learn to code · Available anytime & anywhere for free · Join our community of millions of learners worldwide where no questions go unanswered