Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- public class Battleships {
- private static String[][] cords;
- public static void main(String[] args) {
- introduction();
- twoDimensionalArray();
- coordinates();
- attack();
- }
- public static void introduction() {
- System.out.println();
- System.out.println(" |__\n" +
- " |\\/\n" +
- " ---\n" +
- " / | [\n" +
- " ! | |||\n" +
- " _/| _/|-++'\n" +
- " + +--| |--|--|_ |-\n" +
- " { /|__| |/\\__| |--- |||__/\n" +
- " +---------------___[}-_===_.'____ /\\\n" +
- " ____`-' ||___-{]_| _[}- | |_[___\\==-- \\/ _\n" +
- " __..._____--==/___]_|__|_____________________________[___\\==--____,------' .7\n" +
- "| ---- BATTLESHIPS ----- /\n" +
- " \\_________________________________________________________________________|");
- System.out.println();
- }
- public static void twoDimensionalArray() {
- //creates Array
- String[][] arr = new String[10][10];
- System.out.println(arr[0][0]);
- //fills Array with spaces
- for (int row = 0; row < arr.length; row++) {
- for (int column = 0; column < arr[0].length; column++) {
- arr[row][column] = "+";
- }
- }
- //prints grid
- System.out.println(" 0123456789");
- for (int row = 0; row < arr.length; row++) {
- System.out.print(row + "|");
- for (int column = 0; column < arr[0].length; column++) {
- System.out.print(arr[row][column]);
- }
- System.out.println("|" + row);
- }
- System.out.println(" 0123456789");
- System.out.println();
- }
- public static void coordinates() {
- Scanner Scanner = new Scanner(System.in);
- //creates Array
- cords = new String[10][10];
- System.out.println(cords[0][0]);
- //fills Array with spaces
- for (int row = 0; row < cords.length; row++) {
- for (int column = 0; column < cords[0].length; column++) {
- cords[row][column] = "+";
- }
- }
- System.out.println("***DEPLOY YOUR SHIPS!***");
- System.out.println("They will appear as '@' symbols.");
- //Coordinate declaration
- int x;
- int y;
- for (int j = 0; j < 5; j++) {
- //Gets coordinates
- do {
- System.out.print("Please enter X coordinate: ");
- x = Scanner.nextInt();
- } while (x < 0 || x > 9);
- do {
- System.out.print("Please enter Y coordinate: ");
- y = Scanner.nextInt();
- } while (y < 0 || y > 9);
- if (cords[x][y] == "@") {
- System.out.println("You have entered the same coordinates twice. Please try again.");
- j--;
- }
- //Saves coordinates
- cords[x][y] = "@";
- }
- System.out.println();
- System.out.println("HERE ARE YOUR BATTLESHIPS!");
- System.out.println();
- System.out.println(" 0123456789");
- for (int row = 0; row < cords.length; row++) {
- System.out.print(row + "|");
- for (int column = 0; column < cords[0].length; column++) {
- System.out.print(cords[row][column]);
- }
- System.out.println("|" + row);
- }
- System.out.println(" 0123456789");
- System.out.println();
- System.out.println("DEPLOYING ENEMY SHIPS...");
- //Adding enemy ships to Array
- for (int k = 0; k < 5; k++) {
- //Generating random numbers for Array coordinates
- Random random = new Random();
- int randomNumber1 = random.nextInt(10);
- int randomNumber2 = random.nextInt(10);
- //Checking to make sure they don't overlap players coordinates
- if (cords[randomNumber1][randomNumber2] == "@") {
- k--;
- } else {
- cords[randomNumber1][randomNumber2] = "X";
- }
- }
- //print of complete Array (commented out)
- /*System.out.println();
- System.out.println(" 0123456789");
- for (int row = 0; row < cords.length; row++){
- System.out.print(row + "|");
- for (int column = 0; column < cords[0].length; column++){
- System.out.print(cords[row][column]);
- }
- System.out.println("|" + row);
- }
- System.out.println(" 0123456789");
- System.out.println(); */
- }
- private static void attack() {
- Scanner scanner = new Scanner(System.in);
- System.out.println("ENTER ATTACK COORDINATES!");
- int x;
- int y;
- //Gets coordinates
- do {
- System.out.print("Please enter X coordinate: ");
- x = scanner.nextInt();
- } while (x < 0 || x > 9);
- do {
- System.out.print("Please enter Y coordinate: ");
- y = scanner.nextInt();
- } while (y < 0 || y > 9);
- if (cords[x][y] == "@") {
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement