Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class StarWars {
- public static void main(String[] args) {
- userInterface();
- }
- public static void userInterface() {
- System.out.println("Let's play Torpedo!");
- boolean[][][] map = getMap();
- System.out.println("How many ships do you want to demolish?\nNumber of ships: ");
- int numberOfShips = getNumberOfShips(map);
- placeTheShipsOnTheMap(map, numberOfShips);
- game(map, numberOfShips);
- }
- public static boolean[][][] getMap() {
- System.out.println("Type in each sides length(These should be positive numbers: ");
- int x, z, y;
- do {
- System.out.print("Length of x: ");
- x = extra.Console.readInt();
- System.out.print("Length of y: ");
- y = extra.Console.readInt();
- System.out.print("Length of z: ");
- z = extra.Console.readInt();
- } while (x <= 0 || y <= 0 || z <= 0);
- boolean[][][] map = new boolean[x][y][z];
- for (int i = 0; i < map.length; i++) {
- for (int j = 0; j < map[i].length; j++) {
- for (int k = 0; k < map[i][j].length; k++) {
- map[i][j][k] = false;
- }
- }
- }
- return map;
- }
- public static int getNumberOfShips(boolean[][][] map) {
- int numberOfShips = 0;
- numberOfShips = extra.Console.readInt();
- while (numberOfShips <= 0 || (numberOfShips > map.length * map[0].length * map[0][0].length)) {
- System.out.print("Wrong number (too much / too less)\nType in another one: ");
- numberOfShips = extra.Console.readInt();
- }
- return numberOfShips;
- }
- public static void placeTheShipsOnTheMap(boolean[][][] map, int ships) {
- int shipsPlaced = 0;
- int x, y, z;
- while (shipsPlaced < ships) {
- x = getRandomCoordinate(map.length);
- y = getRandomCoordinate(map[0].length);
- z = getRandomCoordinate(map[0][0].length);
- if (!map[x][y][z]) {
- map[x][y][z] = true;
- shipsPlaced++;
- }
- }
- }
- public static int getRandomCoordinate(int max) {
- return (int) (Math.random() * max);
- }
- public static boolean guess(boolean[][][] map) {
- System.out.println("Type in the coordinate: ");
- System.out.print("x: ");
- int x = extra.Console.readInt();
- System.out.print("y: ");
- int y = extra.Console.readInt();
- System.out.print("z: ");
- int z = extra.Console.readInt();
- if ((x <= map.length && y <= map[0].length && z <= map[0][0].length) && (x > 0 && y > 0 && z > 0)) {
- if (map[x - 1][y - 1][z - 1]) {
- map[x - 1][y - 1][z - 1] = false;
- return true;
- } else {
- return false;
- }
- }
- return false;
- }
- public static void game(boolean[][][] map, int numberOfShips) {
- System.out.println("The fight begins!!!");
- int guessesRemained = 10;
- int shipsRemained = numberOfShips;
- while (guessesRemained > 0 && shipsRemained > 0) {
- boolean nextGuess = guess(map);
- if (nextGuess) {
- System.out.print("HIT!!! ");
- shipsRemained--;
- } else {
- System.out.print("MISS!!! ");
- }
- guessesRemained--;
- System.out.println("You have " + guessesRemained + " guesses left.\n"
- + shipsRemained + " ships had left.");
- }
- if (shipsRemained == 0) {
- System.out.println("YOU WON!");
- } else {
- System.out.println("YOU FAILED!");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement