Advertisement
Guest User

scuffed return

a guest
Apr 9th, 2020
322
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.85 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.         String[][] userShips = new String[10][10];
  12.  
  13.  
  14.         // AI deploy 5 ships random
  15.         Random rand = new Random();
  16.         String[][] aiShips = new String[10][10];
  17.  
  18.  
  19.  
  20.  
  21.         for(int AIdeploy = 0; AIdeploy<5; AIdeploy++) {
  22.             int AIx = rand.nextInt(9);
  23.             int AIy = rand.nextInt(9);
  24.             aiShips[AIx][AIy] = "aiShip";
  25.         }
  26.  
  27.  
  28.         //deploy user ships
  29.  
  30.         for (int deploynumber = 0; deploynumber < 5; deploynumber++) {
  31.             System.out.print("Enter x coordinate for your ship: ");
  32.             int x = input.nextInt();
  33.             if (x > 9) {
  34.                 System.out.println("Please restart and type a number inside the coordinate plane.");
  35.                 System.exit(0);
  36.             }
  37.             System.out.print("Enter y coordinate for your ship: ");
  38.             int y = input.nextInt();
  39.             if (y > 9) {
  40.                 System.out.println("Please restart and type a number inside the coordinate plane.");
  41.                 System.exit(0);
  42.             }
  43.             char t = 'O';
  44.             String userTarget = Character.toString(t);
  45.             ocean[x][y] = userTarget;
  46.             userShips[x][y] = "User Ship";
  47.         }
  48.  
  49.         aiShips[2][3] = "aiShip";
  50.  
  51.  
  52.         //first map print
  53.         printMap(ocean);
  54.  
  55.         int userCounter = 5;
  56.         int aiCounter = 5;
  57.             userAttack(input, aiShips, ocean, userCounter);
  58.             System.out.println(aiCounter);
  59.             //WHY IS aiCounter 5 NOT 4????????
  60.  
  61.  
  62.  
  63.         }
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
  72.  
  73.  
  74.  
  75.  
  76.  
  77.     public static void printMap(String[][] ocean){
  78.  
  79.         System.out.println("  0123456789  ");
  80.         for (int row = 0; row < ocean.length; row++) {
  81.             System.out.print(row + "|");
  82.             for (int column = 0; column < ocean[row].length; column++) {
  83.                 if (ocean[row][column] == null) {
  84.                     System.out.print(" ");
  85.                 } else {
  86.                     System.out.print(ocean[row][column]);
  87.                 }
  88.             }
  89.             System.out.println("|" + row);
  90.         }
  91.  
  92.         System.out.println("  0123456789  ");
  93.  
  94.     }
  95.  
  96.     public static int userAttack(Scanner input, String[][] aiShips, String[][] ocean, int aiCounter){
  97.         System.out.print("Type in the x coordinate you wish to attack: ");
  98.         int x = input.nextInt();
  99.         System.out.print("Type in the y coordinate you wish to attack: ");
  100.         int y = input.nextInt();
  101.         if (aiShips[x][y] == "aiShip") {
  102.             System.out.println("You sunk a ship!");
  103.             char h = '!';
  104.             String j = Character.toString(h);
  105.             ocean[x][y] = j;
  106.             printMap(ocean);
  107.             aiCounter--;
  108.             //THIS MAKES THE aiCounter 4
  109.  
  110.         } else if(aiShips[x][y] == null){
  111.             System.out.println("You missed!");
  112.             ocean[x][y] = "X";
  113.             printMap(ocean);
  114.         }
  115.         return aiCounter;
  116.         //I RETURNED IT BUT aiCounter IN MAIN IS STILL 5??
  117.     }
  118.  
  119.  
  120.  
  121.     public static void aiAttack(String[][] aiShips, String[][] ocean, int userCounter, Random rand) {
  122.  
  123.         int aiAttackX = rand.nextInt(9);
  124.         int aiAttackY = rand.nextInt(9);
  125.  
  126.         if (aiShips[aiAttackX][aiAttackY] == "User Ship") {
  127.             userCounter--;
  128.             System.out.println("AI bombed one of your ships!");
  129.             System.out.println("You have "+userCounter+" ships left!");
  130.             ocean[aiAttackX][aiAttackY] = "X";
  131.         } else if(aiShips[aiAttackX][aiAttackY] == null) {
  132.             System.out.println("AI missed!");
  133.         }
  134.  
  135.     }
  136.  
  137.  
  138.  
  139.  
  140.  
  141.  
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement