All through a military reserve obligation, my buddies and I spent a great deal of time collaborating in backgammon. Sometime, a pal challenged me to assemble a program which may beat everyone on the game. Intrigued by the issue, I received all the way down to create a solution which may analyze a backgammon board and counsel the optimum switch. Proper right here’s how I made it happen.
I envisioned a system the place I’d take a picture of the backgammon board with my phone and acquire the proper switch in return. To comprehend this, I divided the endeavor into 4 principal parts:
- Capturing an image with the phone and sending it to a laptop.
- Processing the image to know the game state.
- Feeding the game state into an algorithm to get the optimum switch.
- Sending the optimum switch once more to the phone.
I decided to start with the second half: processing the image to know the game state.
The backgammon board consists of 24 elements, two types of avid gamers (black and white), and 15 checkers per participant. The game state is printed by:
- The positions of the checkers on the board (on one in every of many 24 elements, throughout the heart “bar,” or off the board).
- The dice roll.
- The current participant’s flip.
Preliminary Makes an try
My first attempt involved fundamental image processing strategies using OpenCV. I made only a few assumptions:
- Checkers are glorious black or white circles.
- There are 24 triangles on the board, representing the 24 elements (positions) throughout the sport.
- The board is a rectangle, and captured photographs would current it completely.
- Checkers on the bar are in the midst of the board.
I started by detecting checkers using the HoughCircles function and detecting the board using rectangle detection methods. Nonetheless, I shortly encountered challenges:
- Triangles (elements) have been often occluded by checkers, making them arduous to detect.
- Parameter tuning for options like HoughCircles labored correctly for one image nonetheless failed on others.
Switching to Object Detection
Realizing the constraints of classical methods, I decided to utilize an object detection model. After researching, I chosen YOLO (You Solely Look As quickly as) and expert it with my very personal dataset.
To create the dataset, I took 30 photographs of varied board states and annotated them using LabelImg. I labeled the checkers, and the board. Nonetheless, the preliminary outcomes have been unsatisfactory because of the board detection was imprecise — captured rectangles have been larger than the exact board, inflicting factors in subsequent calculations.
Iterative Enhancements
Initially, I expert YOLO to detect every the board and the checkers. Nonetheless, a lot of the photographs in my dataset have been taken at angles, inflicting the detected rectangle for the board to be larger than the exact board. This led to inaccuracies in checker placement and sport state extraction.
To deal with this, I tried detecting solely the corners of the board with YOLO. The idea was to make use of those corners to precisely decide the board’s edges and improve the subsequent placement of checkers. Nonetheless, YOLO struggled to detect the corners reliably, and this technique didn’t resolve the problem.
Realizing the constraints of nook detection, I modified strategies:
- I decided to take all photographs instantly from above to eliminate angle distortion.
- I retrained YOLO to detect the board as two halves (left and correct) and the bar individually.
- I expanded the dataset with further annotated photographs, ensuring numerous lighting conditions and positions.
Lastly, this iterative course of resulted in a model that exactly detected the board and all checkers. Now, I had the (x, y) positions of all components throughout the image.
With the game state extracted, the next step was to remodel it proper right into a format that an algorithm may understand. I regarded for a software program which may take the current sport state and counsel the proper switch.
Choosing the Algorithm
I discovered GNU Backgammon, a sturdy backgammon engine in a position to analyzing board states and providing optimum strikes. GNU Backgammon accepts board states in a specific string format, akin to:
set board straightforward 0 -2 0 0 0 0 5 0 3 0 0 0 -5 5 0 0 0 -3 0 -5 0 0 0 0 2 0
This string represents:
- The number of checkers on the bar for each participant.
- The board state all through 24 elements (optimistic values for one participant, opposed for the alternative).
I wrote code to:
- Course of the detected (x, y) positions of checkers and map them to the corresponding elements (1–24) on the board. I divided the board into 4 quadrants and extra subdivided each quadrant into six sections.
- Assemble the string format required by GNU Backgammon.
- Use Python’s subprocess module to ship directions to GNU Backgammon, retrieve the output, and parse the immediate switch.
With the core efficiency working, I moved to the next half: integrating each half proper right into a client-server system.
Server
I constructed the server using Flask, which listens for POST requests containing an image and completely different sport parameters. The server processes the image, makes use of GNU Backgammon to calculate the optimum switch, and sends the consequence once more to the patron.
Shopper
Initially, I created a straightforward app in MIT App Inventor to ship photographs and parameters to the server. Nonetheless, its limitations led me to develop a main HTML interface served by Flask. Clients can add an image, select the dice roll and participant flip, and acquire the optimum switch in response.
The output from GNU Backgammon is verbose and by no means user-friendly. As an example:
12/14 18/23
13/16* 18/17(2)
24/18*/15*/12*
I parsed this output to extract solely most likely probably the most associated switch and translated it proper right into a readable format for the buyer. For instance:
*
signifies a hit (opponent’s checker despatched to the bar).(2)
means shifting two checkers from one stage.- A sequence like
24/18*/15*/12*
means a single checker continues shifting and hits a lot of opponents alongside one of the best ways.
From a straightforward drawback all through a backgammon sport, this endeavor was a fascinating journey via machine learning, laptop imaginative and prescient, and software program program progress. It taught me the importance of problem-solving, adaptability, and integrating quite a few utilized sciences to realize a purpose.
Once you’re fascinated with making an attempt this technique or developing one factor comparable, be pleased to reach out.
You may even be a part of with me on LinkedIn or uncover my duties on GitHub.
Let the video video games begin!