repl.it
@AidanMundle/

Battleship

Java

Test your battleship skills against an intelligent AI.

fork
loading
Files
  • Main.java
  • AI.java
  • CompInputShips.java
  • Move.java
  • UserGuess.java
  • UserInputShips.java
Main.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import java.util.ArrayList;
import java.util.Scanner;

class Main {
    public static void main(String[] args) { 

        //creates multiple boards

        String[][][]userBoard = new String[3][10][10];
        String[][][]boardAI = new String[3][10][10];
        intFillBoard(userBoard,0);
        intFillBoard(boardAI,1);

    /*
    5 aircraft carrier  - a
    4 battleship        - b
    3 cruser            - c
    3 submarine         - s
    2 destroyer         - d
    */

        //creates history of moves made by user and the AI
        ArrayList<Move> moveHistAI= new ArrayList<Move>();
        ArrayList<String> usersShips= new ArrayList<String>();
        ArrayList<String> aiShips= new ArrayList<String>();
        shipLeftFill(usersShips);
        shipLeftFill(aiShips);
        Scanner enter = new Scanner(System.in);
        space();


        System.out.print("\n\n\t\u274CWelcome to battleship\u274C \n\t\tBy Michael F\n\n\n\n\n\tThis is your battleship board. Let's place your ships.\n\n");
        printBoard(userBoard,0);
        System.out.print("\n\nPRESS ENTER TO PLAY\n");
        enter.nextLine();

        //hide ships in hiddenAI board
        //hides user ships

        CompInputShips.autoFill(boardAI);
        UserInputShips.userFill(userBoard);
        int hit=-1;
        boolean gameWon=false;
        boolean userWon=false;
        for(int r = 0;r<10;r++){
            for(int c = 0; c<10;c++){
                boardAI[2][r][c]=boardAI[0][r][c];
                userBoard[2][r][c]=userBoard[1][r][c];
            }
        }

        while(!gameWon){
            hit=UserGuess.UserInput(userBoard,boardAI,aiShips,hit);
            if(aiShips.isEmpty()){
                gameWon= true;
                userWon=true;
            }
            if(!gameWon){
                AI.aiMove(userBoard,usersShips,moveHistAI);
                if(usersShips.isEmpty()){
                    gameWon= true;
                    userWon=false;
                }
            }
        }
        space();
        if(userWon){
            System.out.print("\t\t------You Won-----\n\nCongrats!! ");
        }
        else{
            System.out.print("\t\tYou lost this one");
        }
        System.out.print("\n\nPlay Again!");


    }


    //Fills board with "-" for ships to be placed on
    private static void intFillBoard(String[][][] grid, int e){
        for(int r = 0;r<10;r++){
            for(int c = 0; c<10;c++){
                grid [e][r][c] = "-";
            }
        }
    }

    //Prints coordinates on the side of the board
    public static void printBoard(String[][][] grid, int a){
        for(int r = 0;r<10;r++){
            System.out.print(10-r +"   ");
            if(r!=0){
                System.out.print(" ");
            }
            for(int c = 0; c<10;c++){
                System.out.print(grid[a][r][c] + " ");
            }
            System.out.print("\n");
        }
        System.out.print("\n     1 2 3 4 5 6 7 8 9 10");
    }

    //Method for spacing screen out
    public static void space(){
        for (int i=1;i<=54;i++){
            System.out.print("\n");
        }
    }
    //fills ship list
    private static void shipLeftFill(ArrayList<String> list){
        list.add("a");
        list.add("b");
        list.add("c");
        list.add("s");
        list.add("d");
    }

    //Returns coordinates of grid since indexes are +1
    public static int gTOaX(int graphX){
        return graphX-1;
    }
    public static int gTOaY(int graphY){
        return 10-graphY;
    }
}

Fetching token
?