Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Created 8 April 2020, Victor Shin
- import java.util.Random;
- import java.util.Scanner;
- public class battleship {
- public static void main(String[] args) {
- String[][] ocean = new String[10][10];
- Scanner input = new Scanner(System.in);
- String[][] userShips = new String[10][10];
- // AI deploy 5 ships random
- Random rand = new Random();
- String[][] aiShips = new String[10][10];
- for(int AIdeploy = 0; AIdeploy<5; AIdeploy++) {
- int AIx = rand.nextInt(9);
- int AIy = rand.nextInt(9);
- aiShips[AIx][AIy] = "aiShip";
- }
- //deploy user ships
- for (int deploynumber = 0; deploynumber < 5; deploynumber++) {
- System.out.print("Enter x coordinate for your ship: ");
- int x = input.nextInt();
- if (x > 9) {
- System.out.println("Please restart and type a number inside the coordinate plane.");
- System.exit(0);
- }
- System.out.print("Enter y coordinate for your ship: ");
- int y = input.nextInt();
- if (y > 9) {
- System.out.println("Please restart and type a number inside the coordinate plane.");
- System.exit(0);
- }
- char t = 'O';
- String userTarget = Character.toString(t);
- ocean[x][y] = userTarget;
- userShips[x][y] = "User Ship";
- }
- aiShips[2][3] = "aiShip";
- //first map print
- printMap(ocean);
- int userCounter = 5;
- int aiCounter = 5;
- userAttack(input, aiShips, ocean, userCounter);
- System.out.println(aiCounter);
- //WHY IS aiCounter 5 NOT 4????????
- }
- public static void printMap(String[][] ocean){
- System.out.println(" 0123456789 ");
- for (int row = 0; row < ocean.length; row++) {
- System.out.print(row + "|");
- for (int column = 0; column < ocean[row].length; column++) {
- if (ocean[row][column] == null) {
- System.out.print(" ");
- } else {
- System.out.print(ocean[row][column]);
- }
- }
- System.out.println("|" + row);
- }
- System.out.println(" 0123456789 ");
- }
- public static int userAttack(Scanner input, String[][] aiShips, String[][] ocean, int aiCounter){
- System.out.print("Type in the x coordinate you wish to attack: ");
- int x = input.nextInt();
- System.out.print("Type in the y coordinate you wish to attack: ");
- int y = input.nextInt();
- if (aiShips[x][y] == "aiShip") {
- System.out.println("You sunk a ship!");
- char h = '!';
- String j = Character.toString(h);
- ocean[x][y] = j;
- printMap(ocean);
- aiCounter--;
- //THIS MAKES THE aiCounter 4
- } else if(aiShips[x][y] == null){
- System.out.println("You missed!");
- ocean[x][y] = "X";
- printMap(ocean);
- }
- return aiCounter;
- //I RETURNED IT BUT aiCounter IN MAIN IS STILL 5??
- }
- public static void aiAttack(String[][] aiShips, String[][] ocean, int userCounter, Random rand) {
- int aiAttackX = rand.nextInt(9);
- int aiAttackY = rand.nextInt(9);
- if (aiShips[aiAttackX][aiAttackY] == "User Ship") {
- userCounter--;
- System.out.println("AI bombed one of your ships!");
- System.out.println("You have "+userCounter+" ships left!");
- ocean[aiAttackX][aiAttackY] = "X";
- } else if(aiShips[aiAttackX][aiAttackY] == null) {
- System.out.println("AI missed!");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement