Need help for a Battleships game in Processing

Hey everybody,

Im new here, Im new to programming with Java and new to Processing. I want to code a Battleships game. It should look something like this in the end. I apologize for my bad english, but I hope you can understand the most I`ve written.


That`s the design I made for now.

The game should be following functions:

  • create two boards with a arraylist - works partly

(here should be other pictures of my game - but as new user I can upload one picture only)

so when I want place my ships it works, but I can place the ships under the board and right from the board too.
My first thought about that was to include a collision detection. That could has the benefit of a kind of “hover” effect and that I could place it maybe better into the limits of the board. But I dont know if thats the right way for that. I don`t know how to write “edges” or “limits” for the ships.

In my mind I had a idea with setting the ships:

  • I want place exactly four ships for now
  • it should begin with a a ship with a length of 5 fields and should end with a ship of a length with 2 fields - works
  • with the left mouseclick you could set it vertical and with the right mouseclick you can set it horizontal on the battlefield - works
    At this point it looks like this:

(here should be other pictures of my game - but as new user I can upload one picture only)

As decribed above I have problems with setting the ships within the edges of the board.

following button activities:

  • “New Game”-Button to start and restart the game - works
  • “Exit Game”-Button - works
  • “Quick Guide”-Button that opens a second window with rules etc. - tried some things but it never worked

If these problems are gone, I want to write a simple AI for the computer opponent. When I placed all my ships I can set the first hit on the opponent`s battlefield. After I set the first hit, my opponent should set a hit at random at my battlefield.
The game is over when the user or the computer has 11 hits on the ships from his or her opponent (5 + 4 + 3 +2 = 11).
A kind of highscore system is imagined for the future.

And here is the code for now. I`m sure that this code is chaotic and hard to understand. I taken the basic code of this from: https:
//forum.processing.org/two/discussion/15952/just-a-little-question-to-how-to-make-the-game-battleships

// ArrayList fĂĽr battlefield
ArrayList<Sea> seas = new ArrayList();
// Logo Header
PImage battleshipLogo;
PImage screenLogo1;
PImage screenLogo2;
// Font
PFont font;
// Ship Settings
int shipSizeXY = 250;
// Buttons
Button newGameButton;
Button quickGuideButton;
Button exitGameButton;

//Quick Guide
 
void setup() {
  size(1060, 810);
  background(0);
    battleshipLogo = loadImage("battleship_logo2.jpg");
    
    screenLogo1 = loadImage("ScreenLogo_part1-3.jpg");
    screenLogo2 = loadImage("ScreenLogo_part2-3.jpg");
    image(screenLogo1, 20, 100, 500, 500);
    image(screenLogo2, 540, 100, 500, 500);

    
   
  //for (int i=0; i<10; i++) {
  //  for (int j=0; j<10; j++) {
  //    seas.add(new Sea(20+50*i, 100+50*j, false) );
  //    seas.add(new Sea(540+50*i, 100+50*j, true) );
  //  }
  //}
  
  // Button
  font = loadFont("space_font.vlw");
  textFont(font);
  textSize(26);
  fill(104,240,211);
  quickGuideButton = new Button(20, 620, 1020, 100, "QUICK GUIDE", 128,128,255);
  newGameButton = new Button(20, 740, 500, 50, "NEW GAME", 128,128,255);
  exitGameButton = new Button(540, 740, 500, 50, "EXIT GAME", 128,128,255);
  
}
 
void draw() {
  // position for the sketch window
  if(1==frameCount) surface.setLocation(width/4,0);
  
  //image(screenLogo1, 20, 100, 500, 500);
  //image(screenLogo2, 540, 100, 500, 500);
  imageMode(CENTER);
  image(battleshipLogo, width/2, 50);
  

  for ( int i = 0; i < seas.size(); i++) {
    seas.get(i).draw();
  }
  
  noFill();
  stroke(152,20,152);
  strokeWeight(3);
  rect(20,100,500,500);
  rect(540,100,500,500);
  
  if(newGameButton.isClicked()) {
   //setup();
   for (int i=0; i<10; i++) {
    for (int j=0; j<10; j++) {
      seas.add(new Sea(20+50*i, 100+50*j, false) );
      seas.add(new Sea(540+50*i, 100+50*j, true) );
    }
  }
  }
  
  if(quickGuideButton.isClicked()) {
   
  }
  
  if(exitGameButton.isClicked()) {
   exit();
  }
  
  newGameButton.update();
  newGameButton.render();
  
  quickGuideButton.update();
  quickGuideButton.render();
  
  exitGameButton.update();
  exitGameButton.render();
}
 
void mousePressed() {
  for ( int i = 0; i < seas.size (); i++) {
    seas.get(i).setShip();
  }
}

 
class Sea {
  boolean has_ship;
  boolean secret;
  boolean clicked;
  int x, y;
  int shipSizeXY = 250;
  
  Sea(int ix, int iy, boolean isecret) {
    secret = isecret;
    has_ship = secret && random(1)<.2;//false;
    clicked = false;
    x = ix;
    y = iy;
  }
  void draw() {
    pushMatrix();
    translate(x, y);
    fill(128, 128, 255);
    strokeWeight(1.5);
    stroke(51, 242, 170);
    rect(0, 0, 50, 50);
    
    // ships
    if ( !secret && has_ship ) {
      fill(255,244,139);
      stroke(255, 84, 167);
      rect(5, 5, 40, 40);
    }
    
    // if ship is not hit
    if ( !has_ship && clicked ) {
      fill(255);
      stroke(0);
      ellipse(25, 25, 30, 30);
    }
    
    // if ship is hit
    if ( has_ship && clicked ) {
      fill(255, 0, 0);
      stroke(0);
      ellipse(25, 25, 30, 30);
    }
    popMatrix();
  }
  void setShip() {
    if(mousePressed && (mouseButton == LEFT)) {
      if ( mouseX > x && mouseX < x+shipSizeXY && mouseY > y && mouseY < y+50 ) {
        if( ship_place_state && !secret ){
          has_ship = !has_ship;
          
        if( secret && !ship_place_state ){
          clicked = true;
          computers_turn();
        }
      }
      
    }
   }
   
    else if(mousePressed && (mouseButton == RIGHT)) {
      if ( mouseX > x && mouseX < x+50 && mouseY > y && mouseY < y+shipSizeXY ) {
        if( ship_place_state && !secret ){
          has_ship = !has_ship;
           
        }
        else if( secret && !ship_place_state ){
          clicked = true;
          computers_turn();
        }
      }
    }
    // four ships in ascending size: 5, 4, 3, 2
    shipSizeXY -= 50;
    
    if(shipSizeXY == 50) {
    shipSizeXY = 0;
    }
   }  
  
 
void computers_turn(){
  for(int i = 0; i < seas.size(); i++){
    if( !seas.get(i).secret && !seas.get(i).clicked ){
      seas.get(i).clicked = true;
      return;
    }
  }
}
 
boolean  ship_place_state = true;
}

and here is my button-class:

class Button {
  
  PVector Pos = new PVector(0,0);
  float Width = 0;
  float Height = 0;
  color ButtonColor;
  color TextColor;
  String Text;
  boolean Pressed = false;
  boolean Clicked = false;
  
  // constructor for creating buttons
  Button(int x, int y, int w, int h, String t, int r, int g, int b) {
   
    Pos.x = x;
    Pos.y = y;
    Width = w;
    Height = h;
    ButtonColor = color(r,g,b);
    Text = t;
    TextColor = color(r,g,b);
  }
  
  // 
  void update() {
    
    if(mousePressed == true && mouseButton == LEFT && Pressed == false) {
     Pressed = true;
     if(mouseX >= Pos.x && mouseX <= Pos.x+Width && mouseY >= Pos.y && mouseY <= Pos.y+Height) {
      Clicked = true; 
     }
    } else {
     Clicked = false;
    }
    
    if(mousePressed != true) {
     Pressed = false; 
    }
  }
  
  // 
  void render() {
   
    fill(ButtonColor);
    rect(Pos.x, Pos.y, Width, Height);
   
    textAlign(CENTER, CENTER);
    fill(66,210,149);
    text(Text, Pos.x+(Width/2), Pos.y+(Height/2));
    
  }
  
  //
  boolean isClicked() {
    
    return Clicked;
  }
  
}

Ok these are my problems for now. Maybe there is anyone that can help me in a way.
Thank you very much for reading this.

1 Like

please repair above code posting.
( delete all and paste again inside the )

</> code tag

looks like
```
type or paste code here
```

also make sure it is usable ( as runable ) not only readable.
( select code here, copy paste back to processing IDE and try to run it )

oh, how about that many needed files? fonts images…
github… or make a .zip available


nice that you make a long project description,
but somehow i got lost
what was the problem you need help with?


Thank you for your answer @kll

Is that now better with the code?

I will upload a zip-file of the missing images on monday.

1 Like

You have shared a nice design overview of your goals. For a forum request for help, I’d recommend starting with a very specific next problem – one thing your code doesn’t do yet that is the next thing you want it to do, and ask for help on that. Say:

  1. what it currently does
  2. what you want it to do next
  3. what you tried that isn’t working, or what your question is.

To get good feedback, I’d also recommend getting rid of the pictures – logos, etc. – for forum code. Not necessary, and it makes running your code a pain for others. No logos. For a boat, draw a rectangle or a triangle.

2 Likes

Thank you for the tips. That`s really helpful.

While reading your words I askling myself would it be better to begin at the really beginning of the game.
In my currently game I using a Arraylist for creating the battlefield. What do you think would it be better to use that Arraylist or a two-dimensional Array? Maybe you can explain your thoughts about that so I can understand a little bit deeper.

For my currently version of game:
I can click my ships into the battlefield. I can set exactly four ships in a ascending size from 5 to 2. With the mouseButton left I can set it vertical and with the right mouseButton I can set it horizontal.

 Sea(int ix, int iy, boolean isecret) {
    secret = isecret;
    has_ship = secret && random(1)<.2;//false;
    clicked = false;
...
}

void draw() {

 // Ships
    if ( !secret && has_ship ) {
      fill(255,244,139);
      stroke(255, 84, 167);
      rect(5, 5, 40, 40);
}

void setShip() {
    if(mousePressed && (mouseButton == LEFT)) {
      if ( mouseX > x && mouseX < x+shipSizeXY && mouseY > y && mouseY < y+50 ) {
        if( ship_place_state && !secret ){
          has_ship = !has_ship;
          
        if( secret && !ship_place_state ){
          clicked = true;
          // computers_turn();
        }
      }
      
    }
   }
   
    else if(mousePressed && (mouseButton == RIGHT)) {
      if ( mouseX > x && mouseX < x+50 && mouseY > y && mouseY < y+shipSizeXY ) {
        if( ship_place_state && !secret ){
          has_ship = !has_ship;
           
        }
        else if( secret && !ship_place_state ){
          clicked = true;
         // computers_turn();
        }
      }
    }
    // ascending size of the ships: 5er, 4er, 3er, 2er
    shipSizeXY -= 50;
    
    if(shipSizeXY == 50) {
    shipSizeXY = 0;
    }
   }  

If I consider the shipsize and set the ships within the battlefield it looks like this:

My problem is that there are no limits for the ships on the battlefield. That means if I click outside the field (right from or beneath the field) it will set the ships anyway.

It would looks like this for example:

For that problem I thought about a collision detection where I have a rect over the players battlefield. And I can only place ships when I`m over that rect.

I saw that code here and ask myself if this could work in a way as a basis for my problem:

float rectX;
float rectY;
float rectWidth;
float rectHeight;

void setup() {
  size(300, 300);
  rectX = 50;
  rectY = 100;
  rectWidth = 200;
  rectHeight = 100;
}

void draw() {
  background(64);
  
  if (mouseX > rectX && mouseX < rectX + rectWidth && mouseY > rectY && mouseY < rectY + rectHeight) {
    fill(255, 0, 0);
  } 
  else {
    fill(0, 255, 0);
  }
  
  rect(rectX, rectY, rectWidth, rectHeight);
}

Is that collision detection a possible solution or do you have any other ideas?

Use a 2D array

Check if you exceed the boundaries

Yes, it is. Conceptually, you could approach this in a few ways, but here is one that is flexible:

  1. do point-rect click detection on a list of grids (did I click the left grid or the right grid, or no grid?)
  2. if you clicked on a grid, and that grid is active (correct turn), then: resolve the mouse coordinates to a grid coordinate on that grid.
  3. now do all your move logic with grid coordinates – is that space occupied, does attempting to use it cause the boat to leave the grid, etc. etc.

When I say flexible, I mean that this approach means you could also have 3-player battleship, or make the active and inactive boards change sizes each turn, etc. and all you really need to do is change the dimensions of each grid object.

The third step is integer-based, but is can also be expressed as rect-rect collision detection-- you can express the boat as a rectangle, and ask if it would fit within the grid rectangle.

With this approach you could check for boats of any size – double-wide, or 1.5 units long.

Hey There!

As @chrisir said I would use 2D array and use map for setting cell places.

Hey it me again !

This is what I meant in my last post.

Square a[][];
void setup() {
  size(600, 480);
  a = new Square[10][10];
  int size = 10;
  for (int j = 0; j < a.length; j++) {
    for (int i = 0; i < a[0].length; i++) {
      a[j][i] = new Square( j*size, i*size, size);
    }
  }
}
void draw() {
  background(0);
  int x = (int)map(mouseX, 0, a[a.length-1][a.length-1].getPos().x , 0, a[0][0].getSize())%a[0][0].getSize();
  a[x][0].show();
}

public class Square{
  
  int x;
  int y;
  int size;
  Square(int posX , int posY , int setSize){
    x = posX;
    y = posY;
    size = setSize;
  }
  void show(){
    fill(255,0,0);
    rect(x , y , size , size);
  }
  int getSize(){
    return size;  
  }
  PVector getPos(){
    return new PVector(x,y);  
  }
}

Also intuitively you should swap j * size with i and vice because then it easier to understand the grid to int co-ordinates translation.

1 Like



// the ocean 
Square a[][];

// placing a new ship
boolean hold=false; 
int x, y;
int x2, y2;

void setup() {
  size(600, 480);
  a = new Square[10][10];
  int size = 10;
  for (int j = 0; j < a.length; j++) {
    for (int i = 0; i < a[0].length; i++) {
      a[j][i] = new Square( j*size+25, i*size+25, size);
    }
  }
}

void draw() {
  background(0);

  for (int j = 0; j < a.length; j++) {
    for (int i = 0; i < a[0].length; i++) {
      a[j][i].show(false);
    }
  }

  fill(255);
  text("To place a ship click and hold mouse and drag to any direction (vertical and horizontal). Esc to abort during dragging.", 
    333, 333, 
    222, 999); 

  if (hold) {
    holdManager() ;
  }//if
}//func 

void holdManager() {
  // manage placement of a new ship by drag and drop 

  if (x==-1||y==-1) 
    return; 

  // search where the mouse is 
  for (int j = 0; j < a.length; j++) {
    for (int i = 0; i < a[0].length; i++) {
      if (a[j][i].over()) {
        x2=j;
        y2=i;
      }
    }
  }

  // Eval : just show potential ship 
  if (x==x2) {
    // ship in y
    if (y<y2) {
      for (int i = y; i<y2; i++) {
        a[x][i].show(true);
      }//for
    }//if 
    else if (y>y2) {
      for (int i = y2; i<y; i++) {
        a[x][i].show(true);
      }//for
    }//if 
    //
  } else if (y==y2) {
    //ship in x (y is the same)
    if (x<x2) {
      for (int i = x; i<x2; i++) {
        a[i][y2].show(true);
      }//for
    }//if 
    else if (x>x2) {
      for (int i = x2; i<x; i++) {
        a[i][y2].show(true);
      }//for
    }//if 
    //
  }//else 

  a[x][y].show(true);

  //
}//func 

void mousePressed() {
  hold=true;
  for (int j = 0; j < a.length; j++) {
    for (int i = 0; i < a[0].length; i++) {
      if (a[j][i].over()) {
        x=j;
        y=i;
      }
    }
  }
}

void mouseReleased() {

  if (!hold) 
    return; 

  hold=false;

  if (x==-1||y==-1) 
    return; 

  // search where the mouse is 
  for (int j = 0; j < a.length; j++) {
    for (int i = 0; i < a[0].length; i++) {
      if (a[j][i].over()) {
        x2=j;
        y2=i;
      }
    }
  }

  // Eval : place ship for good 
  if (x==x2) {
    // ship in y direction 
    if (y<y2) {
      for (int i = y; i<=y2; i++) {
        a[x][i].setShip();
      }//for
    }//if 
    else if (y>y2) {
      for (int i = y2; i<=y; i++) {
        a[x][i].setShip();
      }//for
    }//if 
    //
  } else if (y==y2) {
    //ship in x direction (y is the same)
    if (x<x2) {
      for (int i = x; i<=x2; i++) {
        a[i][y2].setShip();
      }//for
    }//if 
    else if (x>x2) {
      for (int i = x2; i<=x; i++) {
        a[i][y2].setShip();
      }//for
    }//if 
    //
  }//else 
  //
}

void keyPressed() {
  if (key==ESC) {
    if (hold) {
      hold=false;
      x=-1;
      y=-1;
      x2=-1;
      y2=-1;
    }//if
    key=0;
  }
}// 

// =====================================================================================================

public class Square {

  int x;
  int y;
  int size;

  boolean hasShip = false; 

  // constr
  Square(int posX, int posY, int setSize) {
    x = posX;
    y = posY;
    size = setSize;
  }// constr

  // ------

  void show(boolean mouseIsPlacingANewShip) {
    fill(255, 0, 0);
    if (over())
      fill(0, 255, 0);
    if (mouseIsPlacingANewShip) 
      fill(0, 0, 255);
    if (hasShip) 
      fill(255, 244, 55); 
    rect(x, y, size, size);
  }
  boolean over() {
    return 
      mouseX>x&&
      mouseX<x+size&&
      mouseY>y&&
      mouseY<y+size;
  }
  int getSize() {
    return size;
  }
  PVector getPos() {
    return new PVector(x, y);
  }
  void setShip() {
    hasShip = true;
  }
}
2 Likes

@Chrisir shows a real good way but I think you could save alot of code by utilizing map.

Square a[][];
void setup() {
  size(600, 480);
  a = new Square[10][10];
  int size = 10;
  for (int j = 0; j < a.length; j++) {
    for (int i = 0; i < a[0].length; i++) {
      a[j][i] = new Square( j*size, i*size, size);
    }
  }
}
void draw() {
  background(0);
  showGrid();
  int x = (int)map(mouseX, 0, a[a.length-1][a.length-1].getPos().x, 0, a[0][0].getSize())%a[0][0].getSize();  
  int y = (int)map(mouseY, 0, a[a.length-1][a.length-1].getPos().y, 0, a[0][0].getSize())%a[0][0].getSize();
  showCell(x, y);
}
void showGrid() {
  for (int j = 0; j < a.length; j++) {
    for (int i = 0; i < a.length; i++) {
      a[j][i].show(color(255, 0, 0));
    }
  }
}

void showCell(int x, int y) {
  a[x][y].show(color(0, 255, 0));
}
public class Square {

  int x;
  int y;
  int size;
  Square(int posX, int posY, int setSize) {
    x = posX;
    y = posY;
    size = setSize;
  }
  void show(color rgb) {
    fill(rgb);
    rect(x, y, size, size);
  }
  int getSize() {
    return size;
  }
  PVector getPos() {
    return new PVector(x, y);
  }
}

With my sketch you can place ships in a cool way and even abort with ESC…

1 Like

During this placement you have to check which ship sizes are already placed and which ones are still allowed to be placed on the ocean

1 Like

Hey ya all, thank you for all your answers.

I had a run yesterday and solved some problems.
Now I can place the ships on my field in a ascending size and that just within the field. Furthermore I can place the ships in a way that they can`t overlapping each other. And with the right or left mousebutton I can rotate the ships.

I have to say that I began the whole project from zero and use a 2d array now. And I particularly emphazing on functionalities for design for now.

The looks ölike this at this point:

Cell[][] players_grid;
Cell[][] opponents_grid;

int cols = 10;
int rows = 10;
int[][] battlefield = new int[cols][rows];

// PImage battleshipLogo;

int shipSize = 250;

void setup() {
  
  size(1060, 780);
  background(0);
  
 // battleshipLogo = loadImage("battleship_logo2.jpg");
  
  players_grid = new Cell[cols][rows];
  for (int i = 0; i < cols; i++) {
    for (int j = 0; j < rows; j++) {
      players_grid[i][j] = new Cell(20+i*50, 100+j*50, 50, 50);
    }
  }
  
  opponents_grid = new Cell[cols][rows];
  for (int i = 0; i < cols; i++) {
    for (int j = 0; j < rows; j++) {
      opponents_grid[i][j] = new Cell(540+i*50, 100+j*50, 50, 50);
    }
  }
  
}


void draw() {
  
  // position of the sketch window
  if(1==frameCount) surface.setLocation(width/4,0);
  
  //imageMode(CENTER);
  //image(battleshipLogo, width/2, 50);
  
  for (int i = 0; i < cols; i++) {
    for (int j = 0; j < rows; j++) {
      players_grid[i][j].display_player();
    }
  }
  
  for (int i = 0; i < cols; i++) {
    for (int j = 0; j < rows; j++) {
      opponents_grid[i][j].display_opponent();
    }
  }
  
}

  void mousePressed() {
    isClicked();
  }
  
    void isClicked() {
    
      for(int i = 0; i < cols; i++) {
      for(int j = 0; j < rows; j++) {
        if (mouseButton == LEFT && ((mouseX > players_grid[i][j].x && mouseX < players_grid[i][j].x+players_grid[i][j].w)  && (mouseX > 20 && mouseX < 520 - shipSize+50))
        && ((mouseY > players_grid[i][j].y && mouseY < players_grid[i][j].y+players_grid[i][j].h ) && (mouseY > 100 && mouseY < 600))) {
          
          
          Cell[] test = new Cell[shipSize/50];
          for(int bla =0 ; bla < shipSize/50; bla++){
          test[bla] = players_grid[i+bla][j];
          }
          
          if(!isdanschiff(test, shipSize/50))
          {
          for (int k = 0; k < shipSize /50 ; k++) {
            change_color2(players_grid[i+k][j],128,128,255);
            players_grid[i+k][j].isOccupied = true;
          }
           shipSize-=50;
          }
        }
          
        else if (mouseButton == RIGHT && ((mouseX > players_grid[i][j].x && mouseX < players_grid[i][j].x+players_grid[i][j].w)  && (mouseX > 20 && mouseX < 520))
        && ((mouseY > players_grid[i][j].y && mouseY < players_grid[i][j].y+players_grid[i][j].h ) && (mouseY > 100 && mouseY < 600-shipSize+50))) {
          for (int k = 0; k < shipSize /50 ; k++) {
            if(players_grid[i][j+k].isOccupied == false) {
            change_color2(players_grid[i][j+k],128,128,255);
            players_grid[i][j+k].isOccupied = true;
            }
          }
           shipSize-=50;
        }
      }
    }
  }
  
    void change_color(int x, int y , int w, int h, int r, int g, int b){
    
    stroke(255);
    fill(r,g,b);
    rect(x,y, w, h);
  }
  
  void change_color2(Cell cell, int r, int g, int b){
    
    stroke(255);
    fill(r,g,b);
    rect(cell.x, cell.y, cell.w, cell.h);
  }
  
boolean isdanschiff(Cell[] cellarray, int shipSize){
  
  for(int i = 0 ; i<shipSize; i++){
  if(cellarray[i].isOccupied)return true;
  }
  return false;
  
}


class Cell {
  
  int x, y;
  int w, h;
  boolean isOccupied;

  
  Cell(int tempX, int tempY, int tempW, int tempH) {
    
    x = tempX;
    y = tempY;
    w = tempW;
    h = tempH;
    isOccupied = false;
    

    
  }
  
  void display_player() {
    
    stroke(255);
    noFill();
    rect(x, y, w, h);
  }
  
  
  void display_opponent() {
    
    stroke(255);
    noFill();
    rect(x, y, w, h);
  }
  
}

How to proceed at this point? Should I program the shots I can set or should I program the computers turn? And how I could do this the best way?

It might be easier to let two players play each other

So two human player play each other using your sketch

So you can do the shots

1 Like

Yes a neat way could also be to use Server within Processing.

See here for further analysis.

Square a[][];
void setup() {
  size(600, 480);
  int size = 20;
  a = new Square[size][size];
  for (int j = 0; j < a.length; j++) {
    for (int i = 0; i < a[0].length; i++) {
      a[j][i] = new Square( j*size, i*size, size);
    }
  }
}
void draw() {
  background(0);
  showGrid();
  int x = (int)map(mouseX, 0, a[a.length-1][a.length-1].getPos().x, 0, a[0][0].getSize())%a[0][0].getSize();  
  int y = (int)map(mouseY, 0, a[a.length-1][a.length-1].getPos().y, 0, a[0][0].getSize())%a[0][0].getSize();
  a[x][y].show(color(255,255,0));
}
void showGrid() {
  for (int j = 0; j < a.length; j++) {
    for (int i = 0; i < a.length; i++) {
      a[j][i].show(color(255, 0, 0));
    }
  }
}
void mousePressed() {
  int x = (int)map(mouseX, 0, a[a.length-1][a.length-1].getPos().x, 0, a[0][0].getSize())%a[0][0].getSize();  
  int y = (int)map(mouseY, 0, a[a.length-1][a.length-1].getPos().y, 0, a[0][0].getSize())%a[0][0].getSize();
  showCell(x, y);
}
void showCell(int x, int y) {
  a[x][y].setShipCell(true);
}
public class Square {

  int x;
  int y;
  int size;
  boolean shipCell;
  Square(int posX, int posY, int setSize) {
    x = posX;
    y = posY;
    size = setSize;
    shipCell = false;
  }
  void show(color rgb) {
    if (!shipCell)fill(rgb);
    else fill(0, 255, 0);
    rect(x, y, size, size);
  }
  int getSize() {
    return size;
  }
  PVector getPos() {
    return new PVector(x, y);
  }
  void setShipCell(boolean setCell) {
    shipCell = setCell;
  }
}

Added putting down of ship cells.