Start a new topic

Creating a Mastermind Game within Nextion Logic

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>


image


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

 - a white peg for each correct color in correct place
 - a black peg for each correct color but in the wrong place

The game ends when the user either guesses correctly or has no turns remaining.
At this time, the Nextion reveals what was selected as the game started.
Clicking the checkmark or title will reset and start a new game.


1 person likes this idea

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.


image

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.

sc.png
(262 Bytes)
mm.png
(5.32 KB)
c7.png
(366 Bytes)
c6.png
(279 Bytes)
c5.png
(272 Bytes)
c4.png
(269 Bytes)
c3.png
(274 Bytes)
c2.png
(270 Bytes)
c1.png
(289 Bytes)
c0.png
(271 Bytes)

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

  randset 0,1295


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.

  set.val=rand

  round.val=0

  sys0=set.val

  sys1=sys0/216

  sys0=sys0%216

  va0.val=sys1

  sys1=sys0/36

  sys0=sys0%36

  va1.val=sys1

  sys1=sys0/6

  sys0=sys0%6

  va2.val=sys1

  va3.val=sys0


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

  if(round.val>=10)

  {

     page page0

  }

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

   if(round.val==0)

   {

     p0.pic=color.val

   }

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

   color.val=0

p41 Touch Press Event contains

   color.val=1

   etc. etc ....

p45 Touch Press Event contains

   color.val=5


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.

  if(round.val>=10)

  {

     page 0

  }


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

  sys1=1

  for(sys0=0;sys0<4;sys0++)

  {

      if(b[round.val*4+sys0+1].pic==6)

      {

        sys1=0

      }

   }


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


  if(sys1==1)

  {

   wc.val=0

   wp.val=0

   for(colorchk.val=0;colorchk.val<6;colorchk.val++)

   {

      sys1=0

      sys2=0

      for(sys0=0;sys0<4;sys0++)

      {

         if(b[68+sys0].val==colorchk.val)

         {

           sys2=sys2+1

         }

      }

     for(sys0=0;sys0<4;sys0++)

     {

        if(b[round.val*4+sys0+1].pic==colorchk.val)

        {

           if(sys1<sys2)

           {

             sys1=sys1+1

           }

          }

        }

        wc.val=wc.val+sys1

      }

      sys1=0

      for(sys0=0;sys0<4;sys0++)

      {

         if(b[68+sys0].val==b[round.val*4+sys0+1].pic)

         {

           sys1=sys1+1

         }

      }

     wp.val=sys1


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.


   if(wc.val==4)

   {

     cirs b[47+round.val].x+20,b[47+round.val].y+8,3,0

   }

   if(wc.val>=3)

   {

     cirs b[47+round.val].x+9,b[47+round.val].y+8,3,0

   }

   if(wc.val>=2)

   {

     cirs b[47+round.val].x+20,b[47+round.val].y+19,3,0

   }

   if(wc.val>=1)

   {

     cirs b[47+round.val].x+9,b[47+round.val].y+19,3,0

   }

   if(wp.val==4)

   {

     cirs b[47+round.val].x+20,b[47+round.val].y+8,3,65535

   }

   if(wp.val>=3)

   {

     cirs b[47+round.val].x+9,b[47+round.val].y+8,3,65535

   }

   if(wp.val>=2)

   {

     cirs b[47+round.val].x+20,b[47+round.val].y+19,3,65535

   }

   if(wp.val>=1)

   {

      cirs b[47+round.val].x+9,b[47+round.val].y+19,3,65535

   }


Next we need to increment our round

   round.val=round.val+1


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

   if(round.val>=10)

   {

      p56.pic=va0.val

      p57.pic=va1.val

      p58.pic=va2.val

      p59.pic=va3.val

   }

   if(wp.val==4)

   {

      p56.pic=va0.val

      p57.pic=va1.val

      p58.pic=va2.val

      p59.pic=va3.val

      round.val=10

   }


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


Enjoy

Patrick GE Martin

HMI
Login or Signup to post a comment