Search code examples
processing

How to swap images in a tile on a grid board upon user click?


I'm making a game that flips a tile in a grid board to flip between images and the user wins when all the tiles become the same image. My current issue is in mousepressed() with the if statement that swaps the tiles around based on what tile the user clicked. I get an error complaining about incompatible types and I am not sure how to work around this error. I will add the images I used for the tiles.

final int NUM_TILES = 4;
PImage image1, image2;
PImage [][] imageShown;
PImage [][] board;
int sqSide;
float randValue = random(1,2);


void setup(){
  size(500, 500);
  sqSide = width/NUM_TILES;
  imageShown = new PImage[NUM_TILES][NUM_TILES];
  board = new PImage[NUM_TILES][NUM_TILES];
  image1 = loadImage("helloseal.jpg");
  image2 = loadImage("very-rotund.jpg");
  displayPuzzle();
  
}

void draw(){
  
}


void displayPuzzle(){
  for(int i=0; i<NUM_TILES; i++){
    for(int j=0; j<NUM_TILES; j++){
      image(board[i][j], j*sqSide, i*sqSide);
    }
  }    
}

int clickedRow, clickedCol;
void mousePressed(){
  clickedRow = int(mouseY/sqSide);
   clickedCol = int(mouseX/sqSide);
   if(board[clickedRow][clickedCol] == 1){
     board[clickedRow][clickedCol] = 2;
     image(image2, clickedCol*sqSide, clickedRow*sqSide);
     }else if(board[clickedRow][clickedCol] == 2){
      board[clickedRow][clickedCol] = 1;
     }
     
      if (checkGameOver() == true){
       System.exit(0);
     }
}

boolean checkGameOver(){
     
  for(int row=0; row<NUM_TILES; row++){
  for(int col=0; col<NUM_TILES; col++){
   int randValue = int(random(1,2));
   if( randValue == 1){
    imageShown[row][col] = 1;
    
    
     return false; 
    }
   }
   return true;
  }
  }

enter image description here enter image description here


Solution

  • Looks like you're trying to compare a PImage to an integer, replacing the 1 and 2 with "image1" and "image2" should fix it.