Speed up development with full-stack environments for every branch.

Learn More

Passionproject Battleship in Java

Forked from Basic Java Example.

4 Runs 5 Views 0 Copies
Saved

Saved

AtheistGod 4

AtheistGod
published 25 days ago

    import java.util.*;
    import java.io.*;
    
    public class HelloWorld {
        public final static int EMPTY = 0; // a cell that is empty, thus the default for zero val
        public final static int EMPTY_HIT = 2; // an empty cell that has been hit (missed)
        public final static int OCCUPIED = 3; // a cell that has been occupied by a ship
        public final static int OCCUPIED_HIT = 4; // a cell that has been occupied and hit
        public static Random rand = new Random();
    
        public static void main(String[] args) {
            Scanner kb = new Scanner(System.in); // creates a scanner system to detect used coordinates
    
            int[][] board = new int[5][5]; // creates 5x5 (25 block) board
            populateBoard(board); // populates the public class 'board'
    
            System.out.println("Jackson Ourada presents... a Console Battleship Game");
            System.out.println("");
            System.out.println("To play, enter the x and y coordinates by \"x,y\"");
            System.out.println("Remember, the board is only 0x4, so make sure you");
            System.out.println("only enter values between those numbers, or you'll");
            System.out.println("crash the game, and if you do, reload the page");
            System.out.println("- is nothing, 0 is a miss, and X is a hit.");
            System.out.println("");
            System.out.println("Good Luck!");
            System.out.println("");
    
            boolean won = false;
            do {
                System.out.println("Fire to a coordinate:");
                String attemptString = kb.nextLine();
    
                String[] splitAttemptString = attemptString.split(",");
    
                int attemptX = Integer.parseInt(splitAttemptString[0]);
                int attemptY = Integer.parseInt(splitAttemptString[1]);
    
                boolean fi = fireAt(board, attemptX, attemptY);
                if (fi) {
                    System.out.println("");
                    System.out.println("HIT!");
                    System.out.println("");
                } else {
                    System.out.println("");
                    System.out.println("MISS!");
                    System.out.println("");
                }
    
                System.out.println("Board state:"); // prints the board state
                String[][] sBoard = boardState(board);
    
                for (int i = 0; i < sBoard.length; i++) {
                    String thing = "";
                    for (int j = 0; j < sBoard[i].length; j++) {
                        thing += sBoard[i][j];
                    }
    
                    System.out.println(thing);
                }
    
                boolean occupied = false;
                for (int i = 0; i < board.length; i++) {
                    for (int j = 0; j < board[i].length; j++) {
                        if (board[i][j] == OCCUPIED) {
                            occupied = true;
                        }
                    }
                }
    
                if ( ! occupied) {
                    won = true;
                }
            } while ( ! won);
    
            System.out.println("Congratulations!!! You have won the game :)");
        }
    
        public static String[][] boardState(int[][] board) { // board characters
            String[][] bo = new String[5][5];
    
            for (int i = 0; i < board.length; i++) {
                for (int j = 0; j < board[i].length; j++) {
                    if (board[i][j] == EMPTY_HIT) {
                        bo[i][j] = "0";
                    } else if (board[i][j] == OCCUPIED_HIT) {
                        bo[i][j] = "X";
                    } else {
                        bo[i][j] = "-";
                    }
                }
            }
    
            return bo;
        }
    
        public static boolean fireAt(int[][] board, int x, int y) {
            if (board[y][x] == OCCUPIED) {
                board[y][x] = OCCUPIED_HIT;
                return true;
            }
    
            board[y][x] = EMPTY_HIT;
            return false;
        }
    
        // this method will populate 3 ships onto the board
        public static void populateBoard(int[][] board) {
            int[] usedY = new int[3];
    
            int bigShipY = generateNotTheSameNumber(usedY, 0);
            populateRow(board, bigShipY, 4);
    
            int medShipY = generateNotTheSameNumber(usedY, 1);
            populateRow(board, medShipY, 3);
    
            int smallShipY = generateNotTheSameNumber(usedY, 2);
            populateRow(board, smallShipY, 2);
    
        }
    
        public static void populateRow(int[][] board, int row, int size) {
            System.out.println(row);
            for (int i = 0; i < size; i++) {
                board[row][i] = OCCUPIED;
            }
        }
    
        // provide a store of all the generated numbers and the iteration count (lazy)
        public static int generateNotTheSameNumber(int[] store, int itera) {
            int randInt;
            do {
                randInt = rand.nextInt(4) + 1;
            } while (Arrays.asList(store).contains(randInt));
    
            store[itera] = randInt;
            return randInt;
        }
    }
    Please login/signup to get access to the terminal.

    Your session has timed out.

    Dismiss (the page may not function properly).