This is the fourth article in a weekly series of tutorials explaining simple programming tasks on the Waves blockchain with the help of Google Blockly. We would like to thank Silas and Marc Jansen for writing this series of articles and are happy to reward their effort with a small grant from the WavesGo Education Fund.

This chapter explains how to program a simple game in which cryptocurrencies can be won. The game can be thought of as a raffle. One guesses a random number and gets, if one guesses the number at the first attempt 6, at the second attempt 3 and at the third attempt 2 waves. You could also program a game where you have to deposit a certain number of waves or another asset first, and for example, if you guess the number with the first three attempts, the paid-in amount gets doubled. Here, we would have to be waited accordingly for the deposit. This could be implemented similarly to, for example, Chapter 2, where we were waiting to create an asset. For the sake of simplicity, however, we have renounced this. If you would like to try it yourself, you can find the tool here

Initialization

First, a variable (here random) is set to a random number of, for example, 1–20. The number of attempts is set to 0. Then a question is asked where you should guess the number. The answer is stored again in a variable. The necessary program code for this purpose is shown in Figure 1

Figure 1: The variables are initialized to their respective values.

Game Play

Next, a loop is used which repeats its content (explained later) as long as the correct number has not yet been guessed (answer not equal to random), see Figure 2.

Figure 2: Loop for controlling the actual game.

Then an “if” checks if the answer is smaller than the random number, if that is the case, the player gets the feedback: “Your named number is too small” and he can guess again. Subsequently, the variable which counts the experiments is increased by one. The implementation is seen in Figure 3.

Figure 3: Program part if the number is too small.

Then another “if” checks if the answer is larger than the random number, if this is the case the player gets the message: “Your named number is too big” and may guess again. Also here is the variable for counting the attempts increased by one. This is seen in Figure 4.

Figur 4: Check if the number is too big.

Finally, a last “if” checks whether the answer is exactly as large as the random number. If so, it says: “Yes, that’s the right number.”, but then it checks if “tries” is less than or equal to 3. If so, 6 divided by tries waves will be sent. This part can be seen in Figure 5.

Figure 5: If the number of tries is below 3 attempts, the payout is sent.

If more than three attempts to guess the number were needed, the “else” branch will say that the player has lost the game.

Finally, Figure 6 shows the complete program.

Figure 6: The complete program of the game.

Disclaimer

The aim of this tutorial is to demonstrate how a simple game logic can be implemented that uses a blockchain based technology for storing and gratification of game results and users. Here, we did not focus on security or anti fraud techniques, but focussed on the game play and the blockchain integration.

Find us online

Connect with us on social media to stay up to date:

Discord: https://discord.gg/cnFmDyA

Twitter: https://twitter.com/GoWavesgo

Facebook: https://www.facebook.com/WavesGo/

Email: go@wavesgo.com