How you have already noticed, I'm not just bloggers, but also family father. Thus you have to combine his hobby and family life somehow together and program smaller gimmicks for the children. In this case the popular game "Connect Four", which already exists in various forms and designs. In this case, we bring the game to the AZ-Touch Mod with 2.4 "display or 2.8" display, while at the same time you should learn about multi-dimensional arrays and recursive features.
The hardware and software for this blog
So that you can rebuild this blog, you need the components from Table 1:
Table 1: Components for 4 wins
You need software:
- Arduino IDE (https://www.arduino.cc/en/Main/Software), here best download the latest version
- The library TFT_espi with all dependencies. How that works, we show u.a. here.
The multi-dimensional array
If you are not exactly from IT, but programming one of your hobbies, you will probably get the term "multi-dimensional array". With one of my last game blog posts Tictacto, I used a normal array from the data type integer,
See code 1:
This is a so-called one-dimensional array, with only one data series exists. In line 29 at the game "4 wins" a two-dimensional array is created,
See code 2:
From the principle you can imagine a two-dimensional array like an excellist list. You enter a value in each column and row, which can then be read out or described again. Basically, this can be driven so far that you can create a n-dimensional array,
See code 3:
Reading and writing such arrays can be quite difficult, depending on how complex the array is. For resetting the two-dimensional array of "4 wins" are two interleaved for loops,
See code 4:
To understand the nested for loop even better, we can roll it up. In the core we find the command:
BMatrix [Irow] [icolumn] = 0;
The memory locations of an array are specified in the square brackets. The more square brackets, the more levels (as described above). Two levels represent a table as I said. Then you surf with the values in the brackets through the lines and columns starting at 0:
BMatrix   = 0;
BMatrix   = 0;
BMatrix   = 0;
Bmatrix   = 0;
One of the two for loops can now be used to run through a line (iterate). The other for loop then counts through the columns. In this case, the inner loop is responsible for the rows, the exterior for the columns.
A three-dimensional array could now be presented like a second table behind the first. In the head of the for loop, the start value, the end value and then the step size is specified. To define all values in an array with 0, you should go to C or C ++.
Just take a look at the function Checkforwinner () An at which the two-dimensional array is read out to determine a possible winner.
The recursive function call
If already the function Checkforwinner () The topic of recursive function call should also be explained in more detail. In many of our blog posts, you will find something like in code 5.
First, it is defined which return value has the function in the upper example a bool (ie true or false, or high or low, or 0 or 1). Directly follows the name of the function over which the function is called in the source code. Thereafter, in the brackets, which data is used as a copy, used as a reference or pointer. So you first clarified the principle of a "normal" function. A recursive function call is said because the function calls itself again. You can best see that in Code 6:
Code 6: Example of a recursive function
The dangerous in this type of call is that mistakes are difficult to find and they may, if necessary, program an unwanted continuous loop. Therefore, the demolition condition must be cleanly programmed, see the inner IF query from code 6, which returns a true and ends the recursive call. At this point, the reference is isum tested whether isum has reached the value 4.
So be careful when programming recursive functions.
Transfer the game to the microcontroller
To play "4 wins", please download the ZIP package from mine Github page down. Unzip this ZIP file and open the folder directly afterwards. You will find two files there:
- INO: The program for the Arduino IDE
- H: The configuration file for the display
Please replace the user_setup.h in order C: \ user \Your name\ Documents \ arduino \ libraries \ tft_espi, see Figure 1. It makes sense to rename the file located there with the same name, Z, b. in User_setup_old.h.
Figure 1: User_setup.h overwrite
Then open the source code in the Arduino IDE and select the appropriate micro controller eBook instructions out. Transfer the program to the Micro Controller and after a restart you should see the call for calibration, see Figure 2.
Figure 2: Calibration prompt on the display
If an error occurs when compiling, e.g. that TFT_espi the function Calibratetouch () Not knows, then you have not replaced the user_setup.h correctly, or the user_setup.h is in the same directory as the * .ino file, which may not be.
If this step is completed, pay attention to the green arrow at the red boxes in the corner, then you will be shown directly after the start screen, see Figure 3:
Figure 3: Start screen from "4 wins"
By pressing the "Start" button, the playing field is built directly, see Figure 4:
Figure 4: The field
At the top you see the targets to set your gambling stone, right you see which player is just in turn. So you and your game opponents see at any time, who is on the train and of course the stones laid, see Figure 5:
Figure 5: Advanced game with all the necessary information
Do not worry about the game to get the right box, depending on which column you or your game opponents select, the right position is automatically determined by the playstein. If a column is full, another movement in this column is no longer possible, as in the real game.
If one of the two players has won or the field is filled and no one could win, a corresponding end screen is displayed, see Figure 6:
Figure 6: Game to end, player 2 has won
We wish you a lot of fun playing and a small important information just finally:
Further projects for AZ-Delivery from me, see https://github.com/M3taKn1ght/Blog-Repo.