In this post we are going to globally explain you the code we need to make our project work but first we are going to quickly explain you how the game works.
About the game :
The game is divided in two phases. The goal of the first one is to share all the tokens between the losers. Depending on how many people are playing, we usually play with either 11 or 13 tokens. During the first phase, every player has to roll the dice once and whoever gets the lowest combination receives tokens. The number of tokens he receives depends on what the biggest score is. We keep doing that until there is no more tokens to give.
Now the second phase begins. The only people participating are the ones who received tokens (therefore who lost at least one turn during the first phase). The goal now is to get rid of all your tokens. The player who starts a turn of table is now allowed to choose if he rolls the dice once, twice or three times as he is playing. Once he’s done all the following players have to try and beat the best score with the same amount of rolls. Whoever gets the lowest score loses and receives tokens from the player who got the best score (the number of tokens once again depends on the best score achieved). When one player has all the tokens, the game is over.
About our code :
We can divide it in 3 parts. The first two are used by the Raspberry to make the interface and the game. The third one is used by the Arduino to drive the motor.
First, we made a code which we use to follow the flow of the game. To do so, we translated the rules of the game into an algorithm and then added the code that counts the dice and the score of each player.
The tricky part here is what treatment we have to carry out to be able to count the dice and the score thanks to the picture our camera has taken.
The first step is to crop the picture so all we see is the gaming plate with the 3 dice on it. Afterwards, we have to use a RGB filter and a threshold (we are using white dice on a green background) to split the colors and fetch the dice (using contours). Once we found the 3 dice, we can start counting how many pips there are in each dice using smaller contours. To make it simple, we find big contours who symbolize the dice and then smaller contours who symbolize the pips. To count the score, we only count contours between a minimum size (size of a pip) and a maximum size (size of a dice).
We then have our result stored in a matrix [nbr of 1’s; nbr of 2’s;…;nbr of 6’s]. The code is also able to count how many dice there are on the gaming plate and to know if a dice is erroneous.
The second part is the one we use to make our interface. We were fairly new to making interface using python so we basically learned ourselves how it works.
Our interface is composed of a few pages. The first one is the “home” page where you can choose what game you wanna play. For now, the only game we developed is the one we explained earlier. The second page is used to choose how many people are playing and what’s their name. After that we have another page for the first phase and one for the second phase. Their goal is to show the score of each player, how many tokens players have and all the information useful to the game itself. Moreover, we are also planning on making a “settings” page that would allow the players to change the sense and speed of rotation of the gaming plate, for example, but we’re not quite sure yet.
The third and last part we made is the one that’s going to drive the motor to make the gaming plate rotate in the right position at the right time. The arduino receives information about the position the gaming plate has to be in next. It then uses this piece of information to drive the motor until the gaming plate is in the right spot.
Our code could be updated a bit later because our model isn’t 100% finished right now so we might have to make small adjustments.
Here’s Simon’s video about our software and game : Software and game
We hope you will enjoy it,
See you soon !