[FMP Tip-n-Trick] FileMaker Developer Busts Through Stalemate to Create Chess Engine
FileMaker Database App Reigns in Gaming Platform
by Joe Cellino, eXcelisys FileMaker Developer
There are two things that bring me joy in this world — chess and FileMaker. I never thought that these two passions would cross paths in my lifetime, but now, that day has come.
Having spent the past several years immersed in the tech industry, I’ve seen how rapidly machine learning has progressed. From autonomous self-driving vehicles to Google Assistant, which carries on conversations eerily similar to those held by humans, America’s technological landscape is constantly evolving.
While all of this technology is impressive, my favorite “deep learning” champ has to be AlphaGo Zero. AlphaGo is a machine learning program. Give AlphaGo a set of rules for a game and it will play itself repetitively until it masters the game. In other words, AlphaGo learns by playing itself, improving with each game.
In late 2017, AlphaGo was given four hours to “self-train” and learn the game of chess. AlphaGo then played the world’s best computer chess engine, Stockfish. Stockfish has dominated the chess world for years, consistently winning the World’s Chess Engine Championship. But in this matchup, Stockfish performed like a fish out of water. Of the 100 games played, AlphaGo won a triumphant 28 and played to a draw on the rest (find match details here).
Inspired by AlphaGo Zero, I wondered if it would be possible to create a chess engine on the FileMaker Platform. In the five years I’ve been developing with FileMaker, I had never considered using the platform to design games.
I developed a plan of attack:
- Create the data structure
- Set up a board and get the pieces to move
- Develop piece movement logic
- Add more rules like castling, en passant and check
- Add checkmate and stalemate
The start of a game. The FileMaker Go Zero chess app utilizes button bars, container calculations and custom scripts and functions to process each move.
The data structure was easy. Each game can have many moves. Done. Setting up the board was a little more difficult. Initially, I thought I could utilize the drag-and-drop feature of container fields to move pieces and control the game through script triggers. For example, when clicking into a container the OnObjectEnter script trigger would determine what square had been selected. When the image was drag-dropped into another container, the OnObjectModify script trigger would process the move. This seemed to cause more problems than it was worth because the script triggers wouldn’t always run and when they didn’t, the entire game was ruined. To fix this problem, I decided to use button bars instead due to their stability in running scripts.
I also made a decision to store each piece image in a “pieces table” instead of storing each one directly on the board. This made it possible to display a piece’s image using container calculations that were driven by text fields that stored a carriage-return list of the squares occupied by each piece.
After the board was set up and the pieces had the ability to move, the next step involved developing the logic for each piece’s movement. I was able to create each piece’s movement logic using scripts and custom functions to determine if squares were occupied and/or available. The scripting works as follows:
- The player selects a square.
- Based on the image in the square, determine the square, piece and color.
- If a piece is selected and it is that color’s turn, then determine all of the legal squares that type of piece can move to.
- If the player selects one of the legal squares, then move the piece by modifying the appropriate text field that stores the piece’s placement. If the player selects a different square, run steps 2 thru 4 again.
White puts black in check. Notice that the app catalogs each move as it is made.
Castling and en passant were easily handled with additional scripting and logic, but dealing with kings in check proved more complicated. I began by creating a script that calculates all of the squares the opposing color’s pieces are attacking (i.e. protected squares). This script runs after each move. I set it up so the king could not legally be moved to a protected square and if your king is in a protected square at the start of your turn, you are in check.
At first, I thought this was enough but soon realized that you can move other pieces besides the king to end up in check. I therefore had to “check for check” when determining legal moves for every piece. It is important that you determine check after any piece is moved because a piece can be shielding your king from an opponent’s piece and, if moved, will place you in check. This requires a lot more processing power because checking for check requires determining the protected squares after the piece is hypothetically moved.
Checkmate and stalemate require looking one move ahead. If it is your turn and you are in check and you have no legal moves, then it is checkmate. If you remove check from the first scenario, then it is stalemate. Both of these seem to go hand in hand with creating an AI that can make intelligent legal moves, so I decided to leave these features out of this version.
Get a piece of the action. Download theChess App FileMaker Go Zero chess engine today.
In the future my goal is to add computer AI that would be able to determine its legal moves and make an intelligent choice aimed at winning the game. It seems as if I have the right scripting structure to be able to determine legal moves. I would theoretically have to link moves through a child / parent relationship and build a tree data structure in order to see moves ahead and determine if the future positions are good or bad. Then maybe one day my engine could compete against AlphaGo in the computer world chess championships. Stay tuned for my next version.
**This article is provided for free and as-is, use, enjoy, learn, and experiment at your own risk – but have fun! eXcelisys does not offer any free support or free assistance with any of the contents of this blog post. If you would like help or assistance, please consider retaining eXcelisys’ FileMaker Pro consulting & development services.
eXcelisys, Inc. is an independent entity and this website/information/blog post has not been authorized, sponsored, or otherwise affiliated with FileMaker, Inc. FileMaker is a trademark of FileMaker, Inc., registered in the U.S. and other countries.