Advertisement
Guest User

Battleship Grid Class

a guest
Nov 22nd, 2014
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.31 KB | None | 0 0
  1. public class Grid{
  2.        
  3.         public static final int UNKNOWN = 0;
  4.         public static final int HIT = 1; //true
  5.         public static final int MISS = 2; //false
  6.         public static final int GRIDSIZE = 10;
  7.          /*  array holding status of each grid location   */  
  8.         int [][] grid = new int[GRIDSIZE][GRIDSIZE];
  9.  
  10.         public Grid(){
  11.             //Setting UNKNOWN values
  12.             for(int row = 0; row < GRIDSIZE; row++)
  13.                 for(int column = 0; column < GRIDSIZE; column++){
  14.                     grid[row][column] = 0;
  15.                 }
  16.         }
  17.                    
  18.         public void print(){
  19.             int [][] grid = new int[GRIDSIZE][GRIDSIZE];
  20.             System.out.print("\n");
  21.             for(int row = 0; row < GRIDSIZE; row++){
  22.                 System.out.print("\n");
  23.                 for(int column = 0; column < GRIDSIZE; column++){
  24.                     System.out.print(grid[row][column] + "\t");
  25.                     }
  26.                 }
  27.             }
  28.        
  29.         public void set(int row, int column, int status) {     
  30.             //Outside boundary error
  31.             if((row >= GRIDSIZE) || (row < 0)){
  32.                 System.out.println("Row value is out of bounds.");
  33.             }else{
  34.                 if((column >= GRIDSIZE) || (column < 0)){
  35.                 System.out.println("Column value is out of bounds.");
  36.                 }else{
  37.                     //Set HIT or MISS
  38.                     grid[row][column] = status;
  39.                 }
  40.             }  
  41.         }
  42.        
  43.         public static void main(String []args) {  
  44.             Grid g = new Grid();  
  45.             g.set(0,0,HIT);  
  46.             g.set(1,1,MISS);  
  47.             g.print();  
  48.         }
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement