Menu

(Solved) : Game Battleship Ship Placed Ocean 10 X 10 Grid Shadows Placed Spaces Adjacent Ship Means S Q34187914 . . .

In a game of Battleship, after a ship has been placed on theocean (10 x 10 grid), shadows are placed in the spaces adjacent tothe ship. This means that other ships cannot be placed in thoseshadow spaces. ‘isOccupied’ will return true if a location containsa ship. Is it possible to add some comments, line by line, of whathappens in this block of code please? I cannot work it out and fearthat I am overcomplicating things.

public void setShadow() {
       for (int i = 0; i < 10 ;i++){
           for (int j = 0;j < 10; j++) {
              if (this.isOccupied(i,j)) {
                  for (int k = -1; k < 2;k++) {
                      for (int m= -1; m < 2; m++ ) {
                         if ((i + k >= 0) && (i + k <= 9)&& (j + m >= 0) && (j + m <= 9)) {
                             shadow[i + k][ j+ m] =true;
                         }
                      }
                  }
              }
           }
       }
   }

Expert Answer


Answer to Game Battleship Ship Placed Ocean 10 X 10 Grid Shadows Placed Spaces Adjacent Ship Means S Q34187914 . . .

OR