HoangHai-portliofio-projects / Projects / 4.Battleship_java / src / GameHelper.java
GameHelper.java
Raw
import java.util.ArrayList;
import java.util.Arrays;

public class GameHelper {

    ArrayList <int[]> angelaShip = new ArrayList<>(); 
    ArrayList <int[]> hitlerShip = new ArrayList<>();
    ArrayList <int[]> napoleonShip = new ArrayList<>();

    static int countFire = 0;

    public boolean isTheGameEnd() {
        return angelaShip.isEmpty() && hitlerShip.isEmpty() && napoleonShip.isEmpty();
    }

    public void setAngelaLoc(ArrayList <int[]> Ship) {
        this.angelaShip = Ship;
    }

    public void setHitlerLoc(ArrayList <int[]> Ship) {
        this.hitlerShip = Ship;
    }

    public void setNapoleonLoc(ArrayList <int[]> Ship) {
        this.napoleonShip = Ship;
    }


    public String checkTheFireResult(String XY){
        int x = Integer.parseInt(XY.substring(0,1));
        int y = Integer.parseInt(XY.substring(1,2));
        int[] fireLocation = new int[2];
        fireLocation[0] = x;
        fireLocation[1] = y;
        String checkAngela ="";
        String checkHitler ="";
        String checkNapoleon ="";
        if (!angelaShip.isEmpty()) {
            checkAngela = checkShipLocation(angelaShip, fireLocation);
        }
        if (!hitlerShip.isEmpty()) {
            checkHitler = checkShipLocation(hitlerShip, fireLocation);
        }
        if (!napoleonShip.isEmpty()) {
            checkNapoleon = checkShipLocation(napoleonShip, fireLocation);
        }

        if ( checkAngela.equals("HIT") || checkHitler.equals("HIT")|| checkNapoleon.equals("HIT") )  {
            return "HIT";
        } else {
            return "MISS";
        }
    }

    public static String checkShipLocation(ArrayList<int[]> angelaShip, int[]fireLocation) {
        for (int[] scanCheck : angelaShip) {
            if ( Arrays.equals(scanCheck, fireLocation) ) {
                angelaShip.remove(scanCheck);
                return "HIT";
            }
        }
        return "MISS";
    }

    public boolean areShipCrashed(Ship one, Ship two) {
        for (int i =0 ; i < one.shipLocation.size(); i++) {
            int[] checkingLot1 = one.shipLocation.get(i);
            for (int j= 0; j < two.shipLocation.size(); j++){
                int[] checkingLot2 = two.shipLocation.get(j);
                if (Arrays.equals(checkingLot1,checkingLot2)) { 
                    return true;
                }
            }
        }
        return false;
    }

    public static void setNewBoard(String[][] board) {
        for (int i = 0; i < 7; i++) {
            for (int j = 0 ; j < 7; j++){
                board[i][j] = "-";
            }
        }
    }

    public static void printCurrentBoard(String[][] board) {
        System.out.println( "   |0||1||2||3||4||5||6| Y");
        for (int i = 0; i < 7; i++) {
            System.out.print( "|" + i + "|");
            for (int j = 0 ; j < 7; j++){
                System.out.print( "|" + board[i][j] + "|");
            }
            System.out.println();
        }
        System.out.println(" X");
    }

    public static void updateCurrentBoard(String check, String[][] board, String XY) {

        //THIS IS THE SOTUION:
        int x = Integer.parseInt(XY.substring(0,1));
        int y = Integer.parseInt(XY.substring(1,2));

        if (check.equals("HIT")){
            board[x][y] = "H";
        } else {
            board[x][y] = "M";
        }
    }

}