Advertisement
Guest User

Scuffed Battleship Code

a guest
Apr 9th, 2020
447
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.83 KB | None | 0 0
  1. // Created 8 April 2020, Victor Shin
  2.  
  3. import java.util.Random;
  4. import java.util.Scanner;
  5.  
  6. public class battleship {
  7.     public static void main(String[] args) {
  8.         String[][] ocean = new String[10][10];
  9.         Scanner input = new Scanner(System.in);
  10.  
  11. //deploy user ships
  12.  
  13.         for (int deploynumber = 0; deploynumber < 5; deploynumber++) {
  14.             System.out.print("Enter x coordinate for your ship: ");
  15.             int x = input.nextInt();
  16.             if (x > 9) {
  17.                 System.out.println("Please restart and type a number inside the coordinate plane.");
  18.                 System.exit(0);
  19.             }
  20.             System.out.print("Enter y coordinate for your ship: ");
  21.             int y = input.nextInt();
  22.             if (y > 9) {
  23.                 System.out.println("Please restart and type a number inside the coordinate plane.");
  24.                 System.exit(0);
  25.             }
  26.             char t = 'O';
  27.             String userTarget = Character.toString(t);
  28.             ocean[x][y] = userTarget;
  29.         }
  30.  
  31.  
  32. //map~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  33.  
  34.         public static void printMap(){
  35.  
  36.             System.out.println("  0123456789  ");
  37.             for (int row = 0; row < ocean.length; row++) {
  38.                 System.out.print(row + "|");
  39.                 for (int column = 0; column < ocean[row].length; column++) {
  40.                     if (ocean[row][column] == null) {
  41.                         System.out.print(" ");
  42.                     } else {
  43.                         System.out.print(ocean[row][column]);
  44.                     }
  45.                 }
  46.                 System.out.println("|" + row);
  47.             }
  48.  
  49.             System.out.println("  0123456789  ");
  50.            
  51.         }
  52.        
  53.  
  54. //map~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  55.  
  56.  
  57.         // AI deploy 5 ships random
  58.         Random rand = new Random();
  59.         String[][] aiShips = new String[10][10];
  60.  
  61.  
  62.         for(int AIdeploy = 0; AIdeploy<5; AIdeploy++) {
  63.             int AIx = rand.nextInt(9);
  64.             int AIy = rand.nextInt(9);
  65.             aiShips[AIx][AIy] = "aiShip";
  66.         }
  67.  
  68.  
  69. //        while(ship # == 0)
  70.         System.out.print("Type in the x coordinate you wish to attack: ");
  71.         int x = input.nextInt();
  72.         System.out.print("Type in the y coordinate you wish to attack: ");
  73.         int y = input.nextInt();
  74.         if (aiShips[x][y] == "aiShip") {
  75.             System.out.print("You sunk a ship!");
  76.             char h = '#';
  77.             String j = Character.toString(h);
  78.             ocean[x][y] = j;
  79.  
  80.             //map needs to go here
  81.  
  82.         } else if(aiShips[x][y] == null){
  83.             System.out.print("You missed!");
  84.             ocean[x][y] = "N";
  85.             //map needs to go here
  86.         }
  87.  
  88.     }
  89.  
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement