SOS: Battleship Java (for statement & array dimensions) -


people! so, i'm trying make battleship game (classic!) in java, basic array , method knowledge. created boolean board 2 dimensions place boats , here's code:

public void placerbateau(int x, int y, boolean dir, int longueur){     int abs = this.x;     int ord = this.y;     this.longueur = longueur;      if(!tabplat[x][y]){         if(dir){             for(abs=ligne; ligne<ligne+this.longueur; abs++){                 tabplat[abs][ord] = true;             }         } else {             for(ord=colonne; colonne<colonne+this.longueur; ord++){                 tabplat[abs][ord] = true;             }         }     } }   public class bataillenavale { public static void main(string [] args){     boolean [][] tabplat = { { false , false , false , false, false } , { false , false , false , false, false } , { false , false , false , false, false } , { false , false , false , false, false } , { false , false , false , false, false } ,  { false , false , false , false, false } };     plateau plat1 = new plateau(tabplat);     plat1.placerbateau(2, 2, true, 2);     plat1.tir(2, 2);     system.out.println(plat1.tostring()); } 

basically, place boat, want cycle, , make true (change initial value) every cell within interval defined in statement. this:

exception in thread "main" java.lang.arrayindexoutofboundsexception: 6 @ plateau.placerbateau(plateau.java:39) @ bataillenavale.main(bataillenavale.java:6) 

which refers following line of code:

tabplat[abs][ord] = true; 

i understand stands for, don't how can have problem this.

ps - i'm sorry english.

the error here:

ligne<ligne+this.longueur 

you never change ligne or longueur goes on until reaches beyond array.

same colonne<colonne+this.longueur


Comments