Advertisement
Guest User

ShipProperties.java

a guest
Jan 23rd, 2014
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.90 KB | None | 0 0
  1. class ShipProps
  2. {
  3.   public static boolean canPlace (int locX, int locY, int[][] grid)
  4.   {
  5.     boolean canPlace;
  6.    
  7.     boolean[][] gridTorF = new boolean[9][9];
  8.    
  9.     int[][] locTest = {{1},{1}};
  10.    
  11.     if (locTest[0][0] == (grid[locX][locY]))
  12.     {
  13.       canPlace = false;
  14.     }
  15.     else
  16.     {
  17.       canPlace = true;
  18.     }
  19.     if(canPlace = true)
  20.     {
  21.       for(int i = 0; i < 10; i++)
  22.       {
  23.         if(locX == i)
  24.         {
  25.           for(int i2 = 0; i2 < 10; i2++)
  26.           {
  27.             if(locY == i2)
  28.             {
  29.               grid[locX][locY] = 1;
  30.             }
  31.           }
  32.         }
  33.       }
  34.       return canPlace;
  35.     }
  36.     else
  37.     {
  38.       return canPlace;
  39.     }
  40.   }
  41.   public static int[][] displayOptions (int length, int startLocX, int startLocY, int[][] grid)
  42.   {
  43.     boolean canPlace = false;
  44.     if (canPlace(startLocX, startLocY, grid))
  45.     {
  46.       System.out.println("You can place your ship!");
  47.       System.out.println();
  48.       System.out.println("Which way would you like your ship to face?");
  49.       System.out.println();
  50.       if( canPlace(startLocX, startLocY + (length - 4), grid) == true && canPlace(startLocX, startLocY + (length - 3), grid) == true && canPlace(startLocX, startLocY + (length - 2), grid) == true && canPlace(startLocX, startLocY + (length - 1), grid) == true && canPlace(startLocX, startLocY + length, grid) == true )
  51.       {
  52.         System.out.print("Up?");
  53.         canPlace = true;
  54.       }
  55.       if( canPlace(startLocX, startLocY - (length - 4), grid) == true && canPlace(startLocX, startLocY - (length - 3), grid) == true && canPlace(startLocX, startLocY - (length - 2), grid) == true && canPlace(startLocX, startLocY - (length - 1), grid) == true && canPlace(startLocX, startLocY - length, grid) == true )
  56.       {
  57.         System.out.print("  Down?");
  58.         canPlace = true;
  59.       }
  60.       if( canPlace(startLocX - (length - 4), startLocY, grid) == true && canPlace(startLocX - (length - 3), startLocY, grid) == true && canPlace(startLocX - (length - 2), startLocY, grid) == true && canPlace(startLocX - (length - 1), startLocY, grid) == true && canPlace(startLocX - length, startLocY, grid) == true )
  61.       {
  62.         System.out.print("  Left?");
  63.         canPlace = true;
  64.       }
  65.       if( canPlace(startLocX + (length - 4), startLocY, grid) == true && canPlace(startLocX + (length - 3), startLocY, grid) == true && canPlace(startLocX + (length - 2), startLocY, grid) == true && canPlace(startLocX + (length - 1), startLocY, grid) == true && canPlace(startLocX - length, startLocY, grid) == true )
  66.       {
  67.         System.out.print("  Right?");
  68.         canPlace = true;
  69.       }
  70.     }
  71.     else
  72.     {
  73.       System.out.println("The ship you chose cannot be placed here!");
  74.       canPlace = false;
  75.     }
  76.     String ans = " ";
  77.     if(canPlace == true)
  78.     {
  79.       ans = In.getString();
  80.       if(ans.equals("Left"))
  81.       {
  82.         for(int i = length; i > 0; i--)                               //Dead Code
  83.         {
  84.           grid[startLocX - i][startLocY] = 1;
  85.           return grid;
  86.         }
  87.       }
  88.       else if(ans.equals("Down"))
  89.       {
  90.         for(int i = length; i > 0; i--)                               //Dead Code
  91.         {
  92.           grid[startLocX][startLocY - i] = 1;
  93.           return grid;
  94.         }
  95.       }
  96.       else if(ans.equals("Right"))
  97.       {
  98.         for(int i = length; i > 0; i--)                               //Dead Code
  99.         {
  100.           grid[startLocX + i][startLocY] = 1;
  101.           return grid;
  102.         }
  103.       }
  104.       else if(ans.equals("Up"))
  105.       {
  106.         for(int i = length; i > 0; i--)                               //Dead Code
  107.         {
  108.           grid[startLocX][startLocY + i] = 1;
  109.           return grid;
  110.         }
  111.       }
  112.     }
  113.     else
  114.     {
  115.       return grid;
  116.     }
  117.     return grid;
  118.   }
  119.   public static void main (String[] args)
  120.   {
  121.     //System.out.println( canPlace(6,5) );
  122.   }
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement