This thread will show how to create the Mastermind Game and keep the game scoring all within the Nextion logic without the need for an external microprocessor. As we go through the process of creating this game, I will deal with a few techniques that may be useful in your projects.
This project was created with Nextion 3.5" Enhanced NX4832K035_011 <Order Here>
As the game starts the Nextion will randomly select 4 colored pegs from 6 colors.
The user has ten turns to guess the correct color and order of these hidden pegs.
Clicking the checkmark will score each round - placing
So getting started on our Mastermind Game within Nextion Logic, I will put the layout of the game - the ordering of the component .ids is crucial to success. Below you will find the ordering of the picture components.
We will be placing 61 30x30 Picture components on the canvas.
10 picture resources, 10 numeric Variable components plus code.
- 1st row p0 .id 1 to p3 .id 4 has .y positioning 398
- 1st row p4 .id 5 to p7 .id 8 has .y positioning 360
- 1st row p8 .id 9 to p11 .id 12 has .y positioning 322
- 1st row p12 .id 13 to p15 .id 16 has .y positioning 284
- 1st row p16 .id 17 to p19 .id 20 has .y positioning 246
- 1st row p20 .id 21 to p23 .id 24 has .y positioning 208
- 1st row p24 .id 25 to p27 .id 28 has .y positioning 170
- 1st row p28 .id 29 to p31 .id 32 has .y positioning 132
- 1st row p32 .id 33 to p35 .id 36 has .y positioning 94
- 1st row p36 .id 37 to p39 .id 40 has .y positioning 56
- bottom row p40 .id 41 to p45 .id 46 has .y positioning 443
- right column p46 .id 47 to p55 .id 56 has .x positioning of 260
** create 1st numeric variable .objname set .id 57
- top row p56 .id 58 to p59 .id 61 has .y positioning 9
- p60 .id of 62 .x position of 268 and .y positioning of 443
** create numeric variables round, color, wc, wp, colorc
** create numberic variables va0,va1,va2 and va3
The five columns are at x positions 84, 125, 166, 207 and 260.
The 10 picture resources need to be added in the following order
c0, c1, c2, c3, c4, c5, c6, c7, mm and sc.
Multi-select the column p46 through p55 and set .pic to 9 (sc.png)
Multi-select p0 to p39 and p56 to p59 and .set .pic to 6 (c6.png)
Set page .sta to image and .pic to 8 (mm.png)
Set p60 .pic to 7 (c7.png)
Set p40 .pic to 0 (c0.png) through p45 .pic to 5 (c5.png)
This will complete setting up the Mastermind board.
Compare with the screenshot above (less plays made of course)
Now we get into the code. The objective of Mastermind is to guess the color of the pegs selected by the opposing component -> this will be the Nextion. To select from 6 colors 4 times (one for each peg) we get 6*6*6*6 (1296) so we need assign this into code.
In the Preinitialize Event of the page we put the line
We will store the resulting peg values in va0 to va3.
We will put the following code lines in the Postinitalize Event of the page.
In the page Touch Release Event will will place the code to restart the game
This should only trigger if the game is completed and not mid-game, so
Good news is there is no code for
- the right column p55 to p46 or
- the top row p56 to 59
But the tedious starts
Row p36 to p39 is for round 9
Row p32 to p35 is for round 8
Row p28 to p31 is for round 7
etc etc ....
Row p0 to p3 is for round 0
In each of the 40 pegs p0 to p39 the Touch Press Event code
swap with the proper round and p0 is swapped with .objname of the picture.
When this is completed we deal with putting the color.val in when the bottom row
of colors is selected by the user. This represents the picture number.
p40 Touch Press Event contains
p41 Touch Press Event contains
etc. etc ....
p45 Touch Press Event contains
This allows the user to select a peg color and then
assign that color to the pegs in the proper round's row.
All that is left is the code for our round checking in the Press Event of p60.
First, if the game is over, we will restart the game. The game is over if round is >= 10.
Next we check that the round is indeed over and we have four pegs in our round row.
Setting sys1=1 will and then setting sys1=0 if peg pic is still 6 will catch an incomplete round
Next we begin scoring only if sys1 is still 1 ensuring the round is over
We first check the six colors if the opponents peg has any and how many
We then check if and how many the player has.
The number of correct can not exceed the number of the opponent.
- Correct color count is stored in the wc variable
- Correct position is stored in the wp variable
To finish up the code for the p60 Touch Press Event
- We must score with the small black and white pegs
- one black peg for each correct color and incorrect position
- one white peg for each correct color in the correct position
- For this we go from the highest to lowest to ensure we overwrite on correct color correct pos.
Next we need to increment our round
We must check now if the game is over either because
- we are on round 10 or
- the player has correctly guessed all four of the opponents pegs
Finally the missing brace from above.
So now you have it, a Mastermind Game within Nextion Logic
I hope you have found this tutorial both entertaining and educational
Patrick GE Martin