Mastermind Project

Hello,

I want to code the mastermind game with LED and everything… I did something to verify the player’s guess but it’s not quite right.

Here’s the code :

#include <Adafruit_NeoPixel.h>

int buttonPin1 = 10;
int buttonPin2 = 11;
int buttonPin3 = 12;
int buttonPin4 = 13;
int buttonPin[] = {buttonPin1,buttonPin2,buttonPin3,buttonPin4};

int counter1=0;
int counter2=0;
int counter3=0;
int counter4=0;
int counter[] = {counter1,counter2,counter3,counter4};

int bien_place=0;
int mal_place=0;
int nb_tour=0;
bool code_secretValide[4]; 
bool codeValide[4];

int buttonState1=0;
int buttonState2=0;
int buttonState3=0;
int buttonState4=0;
int buttonState[] = {buttonState1,buttonState2,buttonState3,buttonState4};


int lastbuttonState1=0;
int lastbuttonState2=0;
int lastbuttonState3=0;
int lastbuttonState4=0;
int lastbuttonState[] = {lastbuttonState1,lastbuttonState2,lastbuttonState3,lastbuttonState4};

int buttonbloqPin = 9;
int buttonbloqState = 0;
int lastbuttonbloqState = 0;
int counterbloq = 0;

char c1;
char c2;
char c3;
char c4;
char code[4];

char code_secret[4] = {'j','b','b','r'};

Adafruit_NeoPixel strip1 = Adafruit_NeoPixel(10, 2);
Adafruit_NeoPixel strip2 = Adafruit_NeoPixel(10, 3);
Adafruit_NeoPixel strip3 = Adafruit_NeoPixel(10, 4);
Adafruit_NeoPixel strip4 = Adafruit_NeoPixel(10, 5);
Adafruit_NeoPixel strip[] = {strip1, strip2, strip3, strip4};

Adafruit_NeoPixel stripC1 = Adafruit_NeoPixel(10, 6);
Adafruit_NeoPixel stripC2 = Adafruit_NeoPixel(10, 7);
Adafruit_NeoPixel stripC3 = Adafruit_NeoPixel(10, 8);
Adafruit_NeoPixel stripC4 = Adafruit_NeoPixel(10, A5);
Adafruit_NeoPixel stripC[] = {stripC1, stripC2, stripC3, stripC4};



void setup() {
  Serial.begin(9600);
  for (int i=0; i <= 3; i++){
    pinMode(buttonPin[i], INPUT);
  }
  for (int j=0; j <=3; j++) {
    strip[j].begin();
    strip[j].show(); // Initialize all pixels to 'off'
  }
  for (int j=0; j <=3; j++) {
    stripC[j].begin();
    stripC[j].show(); // Initialize all pixels to 'off'
  }
}

       

void loop() {

  while (bien_place<4) {
    buttonbloqState = digitalRead(buttonbloqPin);
    if (buttonbloqState != lastbuttonbloqState) {
      if (buttonbloqState == HIGH) {
        counterbloq++;
      }
      else{
        counterbloq =counterbloq;
      }
      lastbuttonbloqState = buttonbloqState;
    }
    
    for (int i=0; i <= 9; i++){
      
      if (counterbloq == i) { 

      
        for (int j=0; j <= 3; j++){
          bien_place=0;
          mal_place=0;
          uint32_t rose = strip[j].Color(255, 0, 255);
          uint32_t bleu = strip[j].Color(1, 102, 102);
          uint32_t vert = strip[j].Color(0, 255, 0);
          uint32_t jaune = strip[j].Color(255, 255, 0);
          
          
          buttonState[j] = digitalRead(buttonPin[j]);
          
          if (buttonState[j] != lastbuttonState[j]) {
            if (buttonState[j] == HIGH) {
              counter[j]++;
            }
            else{
              counter[j]=counter[j];
            }
            lastbuttonState[j] = buttonState[j];
          }
          
          if(counter[j]==0){
            strip[j].setPixelColor(i, 0, 0, 0);         //noir
            strip[j].show();
            
          }
          else if (counter[j]%4==1){
            strip[j].setPixelColor(i, rose);        //rose
            strip[j].show();
            code[j]='r';
          }
          else if (counter[j]%4==2){
            strip[j].setPixelColor(i, bleu);        //bleu
            strip[j].show();
            code[j]='b';
          }
          else if (counter[j]%4==3 ){
            strip[j].setPixelColor(i, vert);          //vert
            strip[j].show();
            code[j]='v';
          }
          else if (counter[j]%4==0){
            strip[j].setPixelColor(i, jaune);       //jaune
            strip[j].show();
            code[j]='j';
          }
          
                    
          //VERIF BP
          if(code_secret[j] == code[j]){
            codeValide[j] = true;
            code_secretValide[j] = true;
            bien_place++;
          }
          else{
            codeValide[j] = false;
            code_secretValide[j] = false;
          }
          
          //VERIF MP
          if(code_secretValide[j] == false){
            for(int k=0; k<4; k++){
              if(j!= k){
                if(code_secret[j] == code[k] && codeValide[k] == false){
                  codeValide[k] = true;
                  codeValide[j] = true;
                  mal_place++;
                }
              }
            }
          }
        }
        if (bien_place==4) {
          stripC1.setPixelColor(i, 255,0,0);
          stripC2.setPixelColor(i, 255,0,0);
          stripC3.setPixelColor(i, 255,0,0);
          stripC4.setPixelColor(i, 255,0,0);
        }
        else if (bien_place==3) {
          stripC1.setPixelColor(i, 255,0,0);
          stripC2.setPixelColor(i, 255,0,0);
          stripC3.setPixelColor(i, 255,0,0);
        }
        else if (bien_place==2) {
          stripC1.setPixelColor(i, 255,0,0);
          stripC2.setPixelColor(i, 255,0,0);
        }
        else if (bien_place==1) {
          stripC1.setPixelColor(i, 255,0,0);
        }

        if (mal_place==4) {
          stripC1.setPixelColor(i, 165,42,0);
          stripC2.setPixelColor(i, 165,42,0);
          stripC3.setPixelColor(i, 165,42,0);
          stripC4.setPixelColor(i, 165,42,0);
        }
        else if (mal_place==3) {
          stripC2.setPixelColor(i, 165,42,0);
          stripC3.setPixelColor(i, 165,42,0);
          stripC4.setPixelColor(i, 165,42,0);
        }
        else if (mal_place==2) {
          stripC4.setPixelColor(i, 165,42,0);
          stripC3.setPixelColor(i, 165,42,0);
        }
        else if (mal_place==1) {
          stripC4.setPixelColor(i, 165,42,0);
        }
        if (buttonbloqState == HIGH){
          stripC1.show();
          stripC2.show();
          stripC3.show();
          stripC4.show();
        }
      }
    }
    Serial.print(" BP: ");
    Serial.print(bien_place);
    Serial.print(" MP: ");
    Serial.print(mal_place);
  }
}

For example, if I try ‘vrbr’ I only get 1 color in good place instead of 2
Thank you

Add comments to your code or a detailed description svp.

I can't see clearly how you are representing the player's input.

The game of Mastermind I know is that the player must put 4 pegs (with 8 colours to choose from) in a row and confirms the input. The system then compares the player's input with the hidden code and scores the result. The player get a score indicating (a) correct colours in correct position and (b) correct colours but in wrong position. Something like: https://www.webgamesonline.com/mastermind/

In principal, if you have the secret code in a 4 element array and the player's attempt in another 4 element array, it should be straight forward (but not trivial).

Phase 1 is to check for and score exact matches. That is both colour and position match. If you get a match, add it to the sum of exact matches, then delete (or mark as already used) the matching elements from both arrays so these are not double counted in the next phase.

Phase 2 is to check for the correct colour in the wrong position. For each element (that has not already been deleted from the player's array), scan each (non deleted) element in the secret code array and, if there is a match, add that to the sum of matched colour but not matched position then, delete the matching elements from both arrays so these are not counted again.

In your development, you should try to break the problem down into separate functions, that is separate the display logic from the logic receive the users input and the logic to score the user's input.