Advertisement
Guest User

Untitled

a guest
Oct 8th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 3.79 KB | None | 0 0
  1. public class StarWars {
  2.  
  3.     public static void main(String[] args) {
  4.         userInterface();
  5.     }
  6.  
  7.     public static void userInterface() {
  8.         System.out.println("Let's play Torpedo!");
  9.         boolean[][][] map = getMap();
  10.         System.out.println("How many ships do you want to demolish?\nNumber of ships: ");
  11.         int numberOfShips = getNumberOfShips(map);
  12.         placeTheShipsOnTheMap(map, numberOfShips);
  13.         game(map, numberOfShips);
  14.  
  15.     }
  16.  
  17.     public static boolean[][][] getMap() {
  18.         System.out.println("Type in each sides length(These should be positive numbers: ");
  19.         int x, z, y;
  20.  
  21.         do {
  22.             System.out.print("Length of x: ");
  23.             x = extra.Console.readInt();
  24.             System.out.print("Length of y: ");
  25.             y = extra.Console.readInt();
  26.             System.out.print("Length of z: ");
  27.             z = extra.Console.readInt();
  28.         } while (x <= 0 || y <= 0 || z <= 0);
  29.  
  30.         boolean[][][] map = new boolean[x][y][z];
  31.  
  32.         for (int i = 0; i < map.length; i++) {
  33.             for (int j = 0; j < map[i].length; j++) {
  34.                 for (int k = 0; k < map[i][j].length; k++) {
  35.                     map[i][j][k] = false;
  36.                 }
  37.             }
  38.         }
  39.         return map;
  40.     }
  41.  
  42.     public static int getNumberOfShips(boolean[][][] map) {
  43.         int numberOfShips = 0;
  44.         numberOfShips = extra.Console.readInt();
  45.         while (numberOfShips <= 0 || (numberOfShips > map.length * map[0].length * map[0][0].length)) {
  46.             System.out.print("Wrong number (too much / too less)\nType in another one: ");
  47.             numberOfShips = extra.Console.readInt();
  48.         }
  49.         return numberOfShips;
  50.     }
  51.  
  52.     public static void placeTheShipsOnTheMap(boolean[][][] map, int ships) {
  53.         int shipsPlaced = 0;
  54.         int x, y, z;
  55.         while (shipsPlaced < ships) {
  56.             x = getRandomCoordinate(map.length);
  57.             y = getRandomCoordinate(map[0].length);
  58.             z = getRandomCoordinate(map[0][0].length);
  59.  
  60.             if (!map[x][y][z]) {
  61.                 map[x][y][z] = true;
  62.                 shipsPlaced++;
  63.             }
  64.         }
  65.     }
  66.  
  67.     public static int getRandomCoordinate(int max) {
  68.         return (int) (Math.random() * max);
  69.     }
  70.  
  71.     public static boolean guess(boolean[][][] map) {
  72.  
  73.         System.out.println("Type in the coordinate: ");
  74.         System.out.print("x: ");
  75.         int x = extra.Console.readInt();
  76.         System.out.print("y: ");
  77.         int y = extra.Console.readInt();
  78.         System.out.print("z: ");
  79.         int z = extra.Console.readInt();
  80.  
  81.         if ((x <= map.length && y <= map[0].length && z <= map[0][0].length) && (x > 0 && y > 0 && z > 0)) {
  82.  
  83.             if (map[x - 1][y - 1][z - 1]) {
  84.                 map[x - 1][y - 1][z - 1] = false;
  85.                 return true;
  86.             } else {
  87.                 return false;
  88.             }
  89.         }
  90.         return false;
  91.     }
  92.  
  93.     public static void game(boolean[][][] map, int numberOfShips) {
  94.         System.out.println("The fight begins!!!");
  95.         int guessesRemained = 10;
  96.         int shipsRemained = numberOfShips;
  97.  
  98.         while (guessesRemained > 0 && shipsRemained > 0) {
  99.             boolean nextGuess = guess(map);
  100.  
  101.             if (nextGuess) {
  102.                 System.out.print("HIT!!! ");
  103.                 shipsRemained--;
  104.             } else {
  105.                 System.out.print("MISS!!! ");
  106.             }
  107.  
  108.             guessesRemained--;
  109.             System.out.println("You have " + guessesRemained + " guesses left.\n"
  110.                     + shipsRemained + " ships had left.");
  111.         }
  112.  
  113.         if (shipsRemained == 0) {
  114.             System.out.println("YOU WON!");
  115.         } else {
  116.             System.out.println("YOU FAILED!");
  117.         }
  118.     }
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement