4 wins with the AZ-Touch Mod

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 "4 wins", 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:

Pos

number

Component

1

1

ESP32 NODEMCU Module WLAN WIFI Development Board with CP2102 (Successor Model for ESP8266)

2

or

1

AZ-Touch Mod wall housing set with 2.4 inch Touch screen for ESP8266 and ESP 32

3

1

AZ-Touch Mod wall housing set with 2.8 inch touchscreen for ESP8266 and ESP32

4

(optional)

1

12V power supply for power supply

Table 1: Components for 4 wins

You need software:

The basics

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:

 int IsetMark[] = {0, 0, 0, 0, 0, 0, 0, 0, 0};

Code 1: Array from the data type integer

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:

 byte bmatrix[Numrov][Numcolumn];

Code 2: Two-dimensional array

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:

 Datatype array [first level] [second level] [third level] .... [N-1 level] [nth level];

Code 3: N-Dimensional Array

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:

 /*
 * =================================================================
 * FUNCTION: ResetMatrix
 * RETURNS: Void
 * Description: Reset the Matrix
 * =================================================================
 */
 void resetmatrix()
 {
   Serial.Println("----- Reset Matrix -----");
   for(int icolum = 0; icolum < int(Numcolumn); icolum++)
     for(int Irov = 0; Irov < int(Numrov); Irov++)
       bmatrix[Irov][icolum] = 0;
   showmatrix();
   Serial.Println("------------------------");
 }

Code 4: Reset the two-dimensional array of "4 wins"

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] [0] = 0;

BMatrix [0] [1] = 0;

BMatrix [1] [0] = 0;

Bmatrix [1] [1] = 0;

etc.

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.

 boot checkvertical(int Irov, int icolum)

Code 5: Example of a function

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:

 /*
 * =================================================================
 * FUNCTION: CHECKVERTICAL
 * RETURNS: TRUE IF There is a Winner Else False
 * INPUT IROW: CURRENT ROW
 * INPUT ICOLUMN: CURRENT COLUMN
 * Ref Isum: Sum of Current Equal position
 * Description: RECURSIVE FUNCTION TO CHECK VERTICAL WIN
 * =================================================================
 */
 boot checkvertical(int Irov, int icolum, int &isum)
 {
   IF(bmatrix[Irov][icolum] != Bplayermove || bmatrix[Irov][icolum] == 0)
     return false;
   Else
  {
     isum++;
     IF(isum == 4)
       return true;
     Else
       return checkvertical(Irov, icolum+1, isum);
  }
 }

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.

User_setup.h override

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.

Calibration prompt on the display

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:

Startscreen from "4 wins"

Figure 3: Start screen from "4 wins"

By pressing the "Start" button, the playing field is built directly, see Figure 4:

The playing field

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:

Advanced game with all the necessary information

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:

Game to End, Player 2 has won

Figure 6: Game to end, player 2 has won

We wish you a lot of fun playing and a small important information just finally:

Since the 2.4 "and the 2.8" display have the same resolution does not have to be cumbersome in the source code. Due to the calibration at the beginning, you can use either the AZ-Touch with 2.4 "or 2.8" display.

Further projects for AZ-Delivery from me, see https://github.com/M3taKn1ght/Blog-Repo.

DisplaysEsp-32Projects for beginners

Leave a comment

All comments are moderated before being published

Recommended blog posts

  1. Install ESP32 now from the board manager
  2. Lüftersteuerung Raspberry Pi
  3. Arduino IDE - Programmieren für Einsteiger - Teil 1
  4. ESP32 - das Multitalent
  5. OTA - Over the Air - ESP programming via WLAN