Battleship

mail@pastecode.io avatar
unknown
java
2 years ago
7.6 kB
1
Indexable
Never

        System.out.println("Enter X coordinate of your carrier: ");
        int XCarrier = input.nextInt();
        System.out.println("Enter Y coordinate of your carrier: ");
        int YCarrier = input.nextInt();
        input.nextLine();
        try {
            if ((XCarrier >= 1 && XCarrier <= 10) && (YCarrier >= 1 && YCarrier <= 10)) {
//                BattleshipGame.grid[XCarrier][YCarrier] = " C";
                System.out.println("Would you like your carrier to be placed vertically or horizontally?");
                String choice = input.nextLine();
                if (choice.equals("h")) {
                    System.out.println("test");
                    BattleshipGame.grid[XCarrier][YCarrier] = " C";
                    BattleshipGame.grid[XCarrier][YCarrier + 1] = " C";
                    BattleshipGame.grid[XCarrier][YCarrier + 2] = " C";
                    BattleshipGame.grid[XCarrier][YCarrier + 3] = " C";
                    BattleshipGame.grid[XCarrier][YCarrier + 4] = " C";
                } else if (choice.equals("v")) {
                    BattleshipGame.grid[XCarrier][YCarrier] = " C";
                    BattleshipGame.grid[XCarrier + 1][YCarrier] = " C";
                    BattleshipGame.grid[XCarrier + 2][YCarrier] = " C";
                    BattleshipGame.grid[XCarrier + 3][YCarrier] = " C";
                    BattleshipGame.grid[XCarrier + 4][YCarrier] = " C";
                }
            } else {
                System.out.println("This location has already been taken! Please try again.");
            }
            GameBoard.printAdding();
        } catch (Exception e) {
            System.out.println(e);
            System.exit(1);
        }

        System.out.println("Enter X coordinate of your Battleship: ");
        int XBattleship = input.nextInt();
        System.out.println("Enter Y coordinate of your Battleship: ");
        int YBattleship = input.nextInt();
        input.nextLine();
        try {
            if ((XBattleship >= 1 && XBattleship <= 10) && (YBattleship >= 1 && YBattleship <= 10)) {
                System.out.print("Would you like your carrier to be placed vertically or horizontally?");
                String choice = input.nextLine();
                if (choice.equals("h")) {
                    BattleshipGame.grid[XBattleship][YBattleship] = " B";
                    BattleshipGame.grid[XBattleship][YBattleship + 1] = " B";
                    BattleshipGame.grid[XBattleship][YBattleship + 2] = " B";
                    BattleshipGame.grid[XBattleship][YBattleship + 3] = " B";
                } else if (choice.equals("v")) {
                    BattleshipGame.grid[XBattleship][YBattleship] = " B";
                    BattleshipGame.grid[XBattleship + 1][YBattleship] = " B";
                    BattleshipGame.grid[XBattleship + 2][YBattleship] = " B";
                    BattleshipGame.grid[XBattleship + 3][YBattleship] = " B";
                }
            } else {
                System.out.println("This location has already been taken! Please try again.");
            }
            GameBoard.printAdding();
        } catch (Exception e) {
            System.out.println(e);
            System.exit(1);
        }
        
        
        System.out.println("Enter X coordinate of your Ruiser: ");
        int XCruiser = input.nextInt();
        System.out.println("Enter Y coordinate of your Ruiser: ");
        int YCruiser = input.nextInt();
        input.nextLine();
        try {
            if ((XCruiser >= 1 && XCruiser <= 10) && (YCruiser >= 1 && YCruiser <= 10)) {
                //BattleshipGame.grid[X][Y] = " C";
                //GameBoard.printAdding();

                System.out.print("Would you like your carrier to be placed vertically or horizontally?");
                String choice = input.nextLine();
                if (choice.equals("h")) {
                    BattleshipGame.grid[XCruiser][YCruiser] = " R";
                    BattleshipGame.grid[XCruiser][YCruiser + 1] = " R";
                    BattleshipGame.grid[XCruiser][YCruiser + 2] = " R";
                } else if (choice.equals("v")) {
                    BattleshipGame.grid[XCruiser][YCruiser] = " R";
                    BattleshipGame.grid[XCruiser + 1][YCruiser] = " R";
                    BattleshipGame.grid[XCruiser + 2][YCruiser] = " R";
                }
            } else {
                System.out.println("This location has already been taken! Please try again.");
            }
            GameBoard.printAdding();
        } catch (Exception e) {
            System.out.println(e);
            System.exit(1);
        }
        
        
        System.out.println("Enter X coordinate of your Submarine: ");
        int XSub = input.nextInt();
        System.out.println("Enter Y coordinate of your Submarine: ");
        int YSub = input.nextInt();
        input.nextLine();
        try {
            if ((XSub >= 1 && XSub <= 10) && (YSub >= 1 && YSub <= 10)) {
                //BattleshipGame.grid[X][Y] = " C";
                //GameBoard.printAdding();

                System.out.println("Would you like your carrier to be placed vertically or horizontally?");
                String choice = input.nextLine();
                if (choice.equals("h")) {
                    BattleshipGame.grid[XSub][YSub] = " S";
                    BattleshipGame.grid[XSub][YSub + 1] = " S";
                    BattleshipGame.grid[XSub][YSub + 2] = " S";
                } else if (choice.equals("v")) {
                    BattleshipGame.grid[XSub][YSub] = " S";
                    BattleshipGame.grid[XSub + 1][YSub] = " S";
                    BattleshipGame.grid[XSub + 2][YSub] = " S";
                }
            } else {
                System.out.println("This location has already been taken! Please try again.");
            }
            GameBoard.printAdding();
        } catch (Exception e) {
            System.out.println(e);
            System.exit(1);
        }
        
        System.out.println("Enter X coordinate of your Destroyer: ");
        int XDestroyer = input.nextInt();
        System.out.println("Enter Y coordinate of your Destroyer: ");
        int YDestroyer = input.nextInt();
        input.nextLine();
        try {
            if ((XDestroyer >= 1 && XDestroyer <= 10) && (YDestroyer >= 1 && YDestroyer <= 10)) {
                //BattleshipGame.grid[X][Y] = " C";
                //GameBoard.printAdding();

                System.out.print("Would you like your carrier to be placed vertically or horizontally?");
                String choice = input.nextLine();
                if (choice.equals("h")) {
                    BattleshipGame.grid[XDestroyer][YDestroyer] = " D";
                    BattleshipGame.grid[XDestroyer][YDestroyer + 1] = " D";
                    BattleshipGame.grid[XDestroyer][YDestroyer + 2] = " D";
                } else if (choice.equals("v")) {
                    BattleshipGame.grid[XDestroyer][YDestroyer] = " D";
                    BattleshipGame.grid[XDestroyer + 1][YDestroyer] = " D";
                    BattleshipGame.grid[XDestroyer + 2][YDestroyer] = " D";
                }
            } else {
                System.out.println("This location has already been taken! Please try again.");
            }
            GameBoard.printAdding();
        } catch (Exception e) {
            System.out.println(e);
            System.exit(1);
        }
        
        
        System.out.println("Good job, you have placed all your ships!");
        System.out.println("Here is the current map: ");
        GameBoard.printComplete();