How to mouseclicked()

How does mouseClicked() work? Im trying to make it so that when a mouse is clicked, it ends.

void mouseClicked(){
   if(shot){
     end();
   }
   else{
     return;
   }
}

Do you have to use 1D arrays? A 2D array would be a much better option!

You were pretty close. Maybe try this…

int CELLSIZE = 50; //size each square

final int NUM_ROWS = 10; //rows * CELLSIZE = height 

final int NUM_COLS = 12; //columns * CELLSIZE = width

int[] shipX = new int[NUM_ROWS]; //ship rows

int[] shipY = new int[NUM_COLS]; //ship columns

int shipLength; //size ship

int[] shipHitX = new int[NUM_ROWS]; //ship hit rows

int[] shipHitY = new int[NUM_COLS]; //ship hit columns

void setup() {

  size(600, 500); //size MUST be (NUM_COLS*CELLSIZE) by (NUM_ROWS*CELLSIZE);

  setupGrid();

  randomShip(); // generate a random ship...once

  drawGrid();

}

void draw() {

}

void setupGrid(){

  for(int x=0; x<NUM_COLS; x++)

  {

    for(int y=0; y<NUM_ROWS; y++)

    {

        shipX[x] = -1;

        shipY[y] = -1;

    }

  }

}

void drawGrid(){

  for(int x=0; x<NUM_COLS; x++)

  {

    for(int y=0; y<NUM_ROWS; y++)

    {

        int shipRowX = x*CELLSIZE;

        int shipRowY = y*CELLSIZE;

        fill(0,0,255);

        if(shipX[x]>-1 && shipY[y]>-1){

        fill(127,0,0); //for debugging, shows the location of the ship...

        }

        rect(shipRowX,shipRowY,CELLSIZE,CELLSIZE); 

    }

  }

}

void randomShip(){

  int direction = (int)random(-10,10);  

  shipLength = (int)random (3, 8); //random ship size

 

   if(direction >= 0) //horizontal ship

  {

    startX = int(random(NUM_COLS-shipLength)); //random starting column

    startY = int(random(NUM_ROWS)); //random row

    for(int a = 0; a < shipLength; a++)

    {

      shipX[startX+a] = 1; 

      shipY[startY] = 1;

    }

  }

else //vertical

  {

    startY = int(random(NUM_ROWS-shipLength)); //random starting row

    startX = int(random(NUM_COLS)); //random column

    for(int a = 0; a < shipLength; a++)

    {

    shipX[startX] = 1; 

    shipY[startY+a] = 1;

    }

  }

}

void mouseClicked(){

  int mouseShipX = int(mouseX / CELLSIZE);

  int mouseShipY = int(mouseY / CELLSIZE);

 

  fill(127); //fill in grey if missed

  if (shipX[mouseShipX] > 0 && shipY[mouseShipY] > 0) {

  //HIT..

  fill(255,0,255);

}

  rect(mouseShipX*CELLSIZE,mouseShipY*CELLSIZE,CELLSIZE,CELLSIZE); 

    //shipHitX[x]=shipY[x];

    //shipHitY[y]=shipX[y];

}