# Automated Dice Game

166

1

1

Published

## Introduction: Automated Dice Game

Hello everyone,

Welcome to our instructable where we will explain you how to make an automated dice in just a few steps.

First, let me introduce you our team. We're a group of five engineering students : Simon Coulon, Gaetan Dainotti, Sebastien Gilis, Antoine Demanet, Joachim Yazigi and Mathieu Colpin. We are currently in the last year of our master degree and we've been asked to develop a project using an Arduino board and a Raspberry Pi.

As we enjoy playing dice games between friends, we decided to make an automated dice game called : 421. The goal of our project is to automate the flow of the game while displaying all the needed information to the players. Indeed, sometimes as we're talking and laughing while playing, it happens that we forget our scores or who's turn it is ! Our project will prevent us from losing the run of the game while not having to count the dice, the scores, etc.

As you can imagine, the major part of our project is to develop the software but we also plan on building a model to make our work more entertaining. To do so, we will mainly use recycled materials and recovery equipments.

Next, you will find all the information you should need to do our project by yourself. It will be explain in 5 differents steps :

- How did we manage the image processing ?

- How does the game work ?

- How does the interface work ?

- How do we drive our DC motor ?

- How to make your own model ?

And finally, to conclude, you will find a video we made to completely explain you our final result ! Hope you will enjoy it !

## Step 1: How Did We Manage the Image Processing ?

First of all the Python software we’re developing has to include all the functions used by the dice counting. The counting is achieved thanks to an image processing code using the OpenCVand the Numpy libraries. The first one stands for Open Source Computer Vision Library and is widely used for computer vision applications as described here: http://opencv.org/about.html.The second one is a Python library mostly dedicated to the handling of N-dimensional array objects such as pictures. Other information about it can be found here: http://opencv.org/about.html.The

The installation of these libraries is a bit tricky. So we decided to follow a complete tutorial to install them on the Raspberry Pi 3: http://www.pyimagesearch.com/2016/04/18/install-g... This tutorial recommends to create a virtual environment dedicated to OpenCV. Luckily we spared a lot of time by finding a complete code using both libraries and counting dice from a jpg file. This code returns the number of dice, a vector containing the results and the number of erroneous objects. On another hand it also provides the result of the image processing. Our job is to sort and keep the parts of the code that are going to be useful for our application and to implement them properly.

To guarantee a transparent communication we joined the URL providing the original code we didn’t develop: https://www.reddit.com/r/raspberry_pi/comments/28... You can also watch the video to learn a bit more about this code.

As you can imagine, we didn't need whole the features of that code for our project. Therefore we studied the code to fully understand it and only use the useful parts for our project.

Here's a simple explanation of how it works. We always use white dice on a black / darkgreen background. It helps a lot for the contrast and reflection issues. The code will use a jpg file from the USB camera.
It will first use a BGR filter to only have the "green part" of the picture (the first code used the "red part"). Afterwards, the code finds the contour of the 3 dice thanks to a green threshold. The last step is then to find and count the pips inside those contours. If the pips are too small or too big then they are counted as wrong dice and erroneous objects.

You can find the code we use in the added files.

## Step 2: How Does the Game Work ?

First, we are going to quickly explain you the rules of the game 421. 421 is played with 3 dice and a few tokens (usually 11 or 13 depending on how many people are playing).

The game is divided into two phases. The goal of the first one (the "charge") is to share all the tokens between the losers. During the first phase, every player has to roll the dice once and whoever gets the lowest combination receives a certain amount of tokens. The number of tokens he gets 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 "discharge").

The only people participating are the ones who received tokens (therefore who lost at least one round 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 and he is the loser.

If at some point during the game, two players have the same score, only one has to receive tokens if they are losers or give tokens if they are winners. To do so, they have to roll the dice once and whoever gets the better score wins the duel.

Here is the list of the different combinations and their value in tokens :

- 421 (best score) = 8 tokens

- MAC x (1 ; 1 ; x) = x tokens unless it is three 1's then it is worth 7 tokens

- Three of a kind = 3 tokens

- Straight = 2 tokens

- Any random other score = 1 token

You can then imagine that we had to translate all those rules into a Python algorithm. Once we finished that code, we combined it with the image processing. Indeed, we will need the first part to be able to count the scores and to determine the different losers and winners.

## Step 3: How Does the Interface Work ?

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 want to play. For now, the only game we developed is the one we explained you in the last step but you could easily add another dice game if you wish. Once you've selected the game, it opens a second page where you can either start a new game or access the settings.

If you decided to start a game without changing the settings, you then have to enter how many people are playing and afterwards enter their name. If you hit the "Confirm" button, the next page is used to get the position of each player around the table. This will allow us to make the gaming plate rotate in front of the right player at the right time but it will be explained later in the "DC motor" section. To get all those positions, you simply have to put the gaming plate in front of the player and hit the corresponding "Confirm" button.

After that, you are all set and the game can now finally begin. A new page is opened and displays all the information during the two phases of the game (Players' name, who's turn it is, tokens received, tokens at stake, etc.)

Finally, the last page shows the name of the loser at the end of the second phase.

You can find screenshots of all those pages in the added files. Those were taken during a game we played a few days ago.

As you can see now, this part of the code is based on an event logic. Everytime something particular happens, the interface is updated with the right information. You will find in our full code how to make those pages, labels and buttons. Those lines will also be implemented into the gaming algorithm. A particularity is that we had to use a "subprocess" to make the buttons "next player" and "roll again" when, during the second phase, the player can choose to roll again or to keep his score. We had to do so otherwise the sequential code would not wait for the player to decide and push the right button.

You'll find the whole code at the last step of our instructable.

## Step 4: How Do We Drive Our DC Motor ?

As we told you during our introduction, the main goal of our project outside of making the software part was to build a model. To be able to create a rotating movement of the gaming plate, we decided to use a wheel DC motor and a rotary encoder. The motor is the only component we had to buy in ordre to make our project.

In addition we needed a driver for our motor and therefore, we used a H-Bridge driver (L293 module).

Here is how it should be. The Raspberry Pi is connected to the Arduino Uno by a USB cord. The Arduino receives position information from the rotary encoder and sends control digits to the H-bridge motor driver. That wiring allows the plate to turn clockwise or counterclockwise depending on the game part.

We added the datasheet of the driver and you can find an electric scheme of the wiring here : http://perso.iut-nimes.fr/fgiamarchi/?p=740

The code we made to drive this motor is quite simple and is composed of 7 functions. All it does is reading the information sent from the Raspberry. It looks like this "g" or "sh25".

If it reads "g" then it means that we are setting the players' position (G stands for for "Get (position)). Therefore it sends the information from the rotary encoder to the Raspberry (case 103).

If it reads "sh25", it means that it has to set the gaming plate to another position. S stands for for "set", H stands for "clockwise rotation (or A for counter clockwise), 25 stands for the targeted position (25th step of the encoder) (case 115).

You can find the arduino code in the added files.

## Step 5: How to Make Your Own Model ?

As you understood, our project is based on the Arduino and Raspberry Pi. We also talked about the motor, its driver and rotary encoder but here is the list of everything you need to make this project :

- A wooden (or an aluminium) board to make the playing board with some fixations (it has to be as light as possible) ;

- 3 dice ;

- A fulcrum to support the board and allow the rotation with a stepper motor ;

- A DC motor (a stepper motor could also work) ;

- A motor driver to allow the communication between the motor and the arduino board ;

- An arduino to control the motor ;

- A USB webcam or a camera for the Raspberry Pi to recognize and count the dice ;

- A Raspberry Pi to control the camera and the sequence of the game ;

- A screen to display all the needed information (players, scores, etc.) ;

It is evident that the screen can be replaced by a laptop to display the interface and that's what we did because of financial reasons.

Our goal was to make a model made of the moving gaming plate and a support for both boards and the camera. We decided to make the gaming plate with 5 wooden planks. Four would be pretty thin and make the four sides of the plate. The last one is thicker and is used to make the bottom. We also painted it in green for esthetical reasons and because it worked great against the contrast and reflection issues with white dice.
Once the paint was dried, we made a steel support with recycled materials. You can obviously use other materials but make sure it is strong enough to support the camera and both boards. You should also make sure that the camera is placed high enough to see the whole gaming plate otherwise sometimes dice won't be pictured.

You can find the picture of the mode we made as en example.

## Step 6: Conclusion

Here we are. With those five steps you should have enough information to be able to make your own automated dice game.

We sincerely hope this will help you and do not hesitate to ask us if you have any questions regarding our project ! We will be glad to answer them !

You will find here the 3 differents codes (game and interface ; motor ; subprocess "Next Button" as explained at the interface step) and a final video we made to introduce our project.

Thanks for reading us, see you !

## Recommendations

• ### Minecraft Class

4,484 Enrolled

• ### Woodworking Contest

We have a be nice policy.