Bug with int[]={};

Here is a program I wrote for MASTERMIND game. When I was trying a way to get the correct placing & correct numbers.

Here is the code which always returns OutOfBounds exception

//mastermind
int code[] = {0, 3, 1, 2};
int attempt[] = {0, 0, 1, 0};

void setup() {
  size(600,600);
  
  printArray(code);
  printArray(attempt);
  println(code.length,attempt.length);
}

void draw() {
  background(0);
  check();
}

void check() {
  //checking the right positions
  boolean cc[] = new boolean[code.length+1];
  int ccNum = 0;
  for (int i = 0; i < code.length; i++) if (code[i] == attempt[i]){
    cc[i] = true;
    ccNum++;
  }
  int cNum = 0;
  boolean c[] = new boolean[code.length+1];
  println(ccNum);
  
  for(int i = 0; i < attempt.length; i++) {
    
    for(int j = 0; j < code.length; i++) {
      if( attempt[i] == code[j]) {
        cNum++;
      }
    }
    
    
  }
  println(cNum);
}

Hi @CodeMasterX,

I guess the error is here

for(int j = 0; j < code.length; i++) {

it should be j++ instead

Best regards

1 Like

turns out I am just an idiot : P