top of page

A curious case of a Chess game!

Updated: Sep 15, 2021


It is very easy to think of a game that is as famous as Chess to be available to download on any online marketplace.

This post is about just another Chess game but the first ever built inside Google Sheets using Google Apps Script.

Enjoy the new chess experience in Google sheets.

Here is how this chess game was built,

  • To start with, we need to design a board with 8 x 8 squares to hold the pieces. Google Sheets is the easiest place for grid creation.

  • Next we need some icons for our pieces. You can easily download these pieces from stock images online. Image reference is super easy using the IMAGE formula in Google Sheets. Just store the image in Drive and display it using it's URL.

  • To move the pieces on the board, we then create valid moves for each piece. To do this we first map the pieces to the formulas of images they correspond to. For example if a rook image is in the D7 cell of my sheet, then I will call it D7 and refer as D7 in the script.

  • To build the AI, we can create a simple logic of looking for the pieces under attack and defending them first. The AI should take a decision based on the next move predicted to be safe.

  • And there you have it. Your chess game is now ready to play!

Oh wait! there is more to it....

Seriously there are a few things we need to keep in consideration before jumping into the AI creation,

  • The possibility of castle or special moves like "En Passant"

  • The AI move should be stored in property service in order for the AI to not repeat the same move in a row when there are other possible moves.

  • The AI should first count and evaluate all possible moves, separate them into attacking and non-attacking moves, then select the best possible move out of several possible moves.

  • On every move, the AI should calculate not only the moves but also check to see if the AI king is under "Check" and try to defend the king.

  • On every move the opponent makes, the AI should find the best move possible considering the points it can make if it attacks the opponent piece. Try going for the max points while being safe.

You many ask, how do I move these pieces ?

A simple answer to this is using Google sheet formulas for images and computing the right formula to use in the right cell at the right time. The images are stored in the sheet which are called by a cell reference.

One major challenge faced,

Move generation and prediction of moves and finding the best move is challenging when you take into consideration the level of search into the future moves for both parties and yes this search is resource hungry.

Apps Script can easily handle it as long as the depth level of search for a possible move is 2 or less. It slows down as soon as you go beyond a search level of 3. This happens because predicting a move ahead into the future would be thousand different move possibilities for both players.

This can be optimized and the time can be improved by implementing a Good algorithm like Mini-Max.

Keeping all this in mind a lame AI was built which is still not perfect by any means but just works.

I do not intend to bore you with any technical details here so I have put everything into a Google Sheets file and will let you try and enjoy it on your own.

Good luck!

512 views0 comments

Recent Posts

See All


bottom of page