Datasets:

ArXiv:
License:
File size: 13,300 Bytes
c574d3a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
//Csaba
package battleship.Logic;

import java.awt.Point;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 *
 * @author Csaba
 */
public class Board {

    private static final Point[] relativeCoords = {
        new Point(-1, -1),
        new Point(-1, 0),
        new Point(-1, 1),
        new Point(0, -1),
        new Point(0, 0),
        new Point(0, 1),
        new Point(1, -1),
        new Point(1, 0),
        new Point(1, 1)
    };

    CellStatus[][] cellstatus = new CellStatus[10][10];

    public Board() {
        for (int i = 0; i < 10; i++) {
            for (int j = 0; j < 10; j++) {
                cellstatus[i][j] = CellStatus.Empty;
            }
        }
    }

    public Board(String a) {
        String[] row = a.split(";");
        for (int i = 0; i < 10; i++) {
            String[] column = row[i].split(":");
            for (int j = 0; j < 10; j++) {
                cellstatus[i][j] = CellStatus.valueOf(column[j]);
            }
        }
        System.out.println(this);
    }

    public void setCell(int i, int j, CellStatus status) {
        cellstatus[i][j] = status;
    }

    public int getNLength() {
        return cellstatus.length;
    }

    public CellStatus[][] getCellstatus() {
        return cellstatus;
    }

    public String convertToString() {
        String re = "";
        for (int i = 0; i < 10; i++) {
            for (int j = 0; j < 10; j++) {
                re += cellstatus[i][j] + ":";
            }
            re += ";";
        }
        return re;
    }

    @Override
    public String toString() {
        String a = "";
        for (int i = 0; i < 10; i++) {
            for (int j = 0; j < 10; j++) {
                //a += cellstatus[j][i].toString().charAt(0) == 'S' ? 1 + " " : 0 + " ";
                a += cellstatus[j][i].toString().charAt(0) == 'S' ? "▓ " : "░ ";
            }
            a += "\n";
        }
        return "Board:\n" + a;
    }

    public static Board TesztBoard() {
        Board board = new Board();
        Random rnd = new Random();
        for (int i = 0; i < 10; i++) {
            for (int j = 0; j < 10; j++) {
                if (rnd.nextBoolean()) {
                    board.getCellstatus()[i][j] = CellStatus.Ship;
                }
            }
        }

        return board;
    }

    public static Board RandomBoard() {
        //System.out.println("\n\n\n");
        Random rnd = new Random();
        Board board = new Board();
        int[] shipSizes = {4, 3, 2, 1};
        int[] shipPieces = {1, 2, 3, 4};
        ArrayList<Point> availableCells = new ArrayList<Point>();
        for (int i = 0; i < 10; i++) {
            for (int j = 0; j < 10; j++) {
                availableCells.add(new Point(i, j));
            }
        }

        for (int i = 0; i < shipSizes.length; i++) {
            //System.out.println("##############\nShip size: " + shipSizes[i]);
            for (int dbI = 0; dbI < shipPieces[i]; dbI++) {
                //System.out.println("Darab: " + dbI);
                ArrayList<Integer> availableCellsIntegers = new ArrayList<Integer>();
                for (int s = 0; s < availableCells.size(); s++) {
                    availableCellsIntegers.add(s);
                }

                boolean probalkozz = true, horizontal = rnd.nextBoolean();
                int cellI, cellJ;

                do {
                    int rndnum = rnd.nextInt(availableCellsIntegers.size());
                    //System.out.println("size: " + availableCellsIntegers.size() + "\trndnum: " + rndnum);
                    int sgd = availableCellsIntegers.get(rndnum);

                    cellI = availableCells.get(sgd).x;
                    cellJ = availableCells.get(sgd).y;

                    if (Board.isEmptyPlace(board.cellstatus, cellI, cellJ, shipSizes[i], horizontal)) {
                        if (horizontal) {
                            for (int s = 0; s < shipSizes[i]; s++) {
                                board.setCell(cellI + s, cellJ, CellStatus.Ship);
                            }
                        } else {
                            for (int s = 0; s < shipSizes[i]; s++) {
                                board.setCell(cellI, cellJ + s, CellStatus.Ship);
                            }
                        }
                        probalkozz = false;
                    }
                    availableCellsIntegers.remove(rndnum);
                } while (probalkozz && availableCellsIntegers.size() > 0);

            }
        }

        return board;
    }

    private static boolean isEmptyPlace(CellStatus[][] cell, int ccelli, int ccellj, int selectedShipSize, boolean shipPlaceHorizontal) {
        //System.out.println("#######################");
        if (shipPlaceHorizontal) {
            if (ccelli + selectedShipSize - 1 <= 9) {
                for (int i = 0; i < selectedShipSize; i++) {
                    //System.out.println("");
                    for (Point relativeCoord : relativeCoords) {
                        int cellI = ccelli + relativeCoord.y;
                        int cellJ = ccellj + relativeCoord.x;

                        cellI += i;
                        //System.out.println("H_ CellI:" + cellI + " cellJ:" + cellJ);
                        if (cellI >= 0 && cellI <= 9 && cellJ >= 0 && cellJ <= 9) {
                            if (cell[cellI][cellJ] == CellStatus.Ship) {
                                //System.out.println("H_ Invalid place: " + cellI + " - " + cellJ);
                                return false;
                            }
                        }
                    }
                }
            } else {
                //System.out.println("H_ Hahó ez kiesne");
                return false;
            }
        } else {//Vertical
            if (ccellj + selectedShipSize - 1 <= 9) {
                for (int i = 0; i < selectedShipSize; i++) {
                    //System.out.println("V_ ");
                    for (Point relativeCoord : relativeCoords) {
                        int cellI = ccelli + relativeCoord.y;
                        int cellJ = ccellj + relativeCoord.x;

                        cellJ += i;
                        //System.out.println("V_ CellI:" + cellI + " cellJ:" + cellJ);
                        if (cellI >= 0 && cellI <= 9 && cellJ >= 0 && cellJ <= 9) {
                            if (cell[cellI][cellJ] != CellStatus.Empty) {
                                //System.out.println("V_ Invalid place: " + cellI + " - " + cellJ);
                                return false;
                            }
                        }
                    }
                }
            } else {
                //System.out.println("V_ Hahó ez kiesne");
                return false;
            }
        }
        return true;
    }

    public boolean hasCellStatus(CellStatus status) {
        for (int i = 0; i < 10; i++) {
            for (int j = 0; j < 10; j++) {
                if (cellstatus[i][j] == status) {
                    return true;
                }
            }
        }
        return false;
    }

    //Egy hajó részének koordinátáját megadva kiszámolja az összes olyan koordinátát, ahol az a hajó van.
    private List<Point> shipCoords(int i, int j) {
        List<Point> shipsCoords = new ArrayList<>();
        Point[] relativeCoordsVertical = {
            new Point(-1, 0),
            new Point(1, 0)
        };
        Point[] relativeCoordsHorizontal = {
            new Point(0, -1),
            new Point(0, 1)
        };
        //eldöntés melyik irányban kell ellenőriznie
        boolean horizontal = false;
        for (Point point : relativeCoordsHorizontal) {
            int x = i + point.x;
            int y = j + point.y;
            if (x >= 0 && x < 10 && y >= 0 && y < 10) {
                CellStatus cs = cellstatus[x][y];
                if (cs == CellStatus.Ship || cs == CellStatus.ShipHit || cs == CellStatus.ShipSunk) {
                    horizontal = true;
                }
            }
        }

        //hajó koordináták hozzáadása a listához
        shipsCoords.add(new Point(i, j));

        //Eddig jó a kód
        if (horizontal) {
            //System.out.println("HORIZONTAL SHIP");
            CellStatus cs;
            int x, y;
            boolean existShip;

            for (Point point : relativeCoordsHorizontal) {
                x = i;
                y = j;
                existShip = true;
                do {
                    x += point.x;
                    y += point.y;
                    if (x >= 0 && x < 10 && y >= 0 && y < 10) {
                        cs = cellstatus[x][y];
                        //System.out.print("Ellenőrzés: " + cellstatus[x][y]);
                        if (cs == CellStatus.Ship || cs == CellStatus.ShipHit || cs == CellStatus.ShipSunk) {
                            //System.out.println(" ez jó");
                            Point p = new Point(x, y);
                            if (!shipsCoords.contains(p)) {
                                //System.out.println(" és hozzáadtuk.");
                                shipsCoords.add(p);
                            } else {
                                //System.out.println(" de már létezik");
                            }
                        } else {
                            //System.out.println(" nem jó");
                            existShip = false;
                        }
                    } else {
                        //System.out.println(" nem jó");
                        existShip = false;
                    }
                } while (existShip);
            }
        } else {
            //System.out.println("VERTICAL SHIP");
            CellStatus cs;
            int x, y;
            boolean existShip;

            for (Point point : relativeCoordsVertical) {
                x = i;
                y = j;
                existShip = true;
                do {
                    x += point.x;
                    y += point.y;
                    if (x >= 0 && x < 10 && y >= 0 && y < 10) {
                        cs = cellstatus[x][y];
                        //System.out.print("Ellenőrzés: " + cellstatus[x][y]);
                        if (cs == CellStatus.Ship || cs == CellStatus.ShipHit || cs == CellStatus.ShipSunk) {
                            //System.out.println(" ez jó");
                            Point p = new Point(x, y);
                            if (!shipsCoords.contains(p)) {
                                //System.out.println(" és hozzáadtuk.");
                                shipsCoords.add(p);
                            } else {
                                //System.out.println(" de már létezik");
                            }
                        } else {
                            //System.out.println(" nem jó");
                            existShip = false;
                        }
                    } else {
                        //System.out.println(" nem jó");
                        existShip = false;
                    }
                } while (existShip);
            }
        }
//        System.out.println("######### EGÉSZ HAJÓ:");
//        for (Point shipsCoord : shipsCoords) {
//            System.out.println("I: " + shipsCoord.x + " J: " + shipsCoord.y);
//        }
//        System.out.println("######## END");
        return shipsCoords;
    }

    public boolean isSunk(int i, int j) {
        List<Point> shipCoords = shipCoords(i, j);

        for (Point shipCoord : shipCoords) {
            if (cellstatus[shipCoord.x][shipCoord.y] == CellStatus.Ship) {
                return false;
            }
        }

//        for (Point shipCoord : shipCoords) {
//            for (Point relativeCoord : relativeCoords) {
//                int x = shipCoord.x + relativeCoord.x;
//                int y = shipCoord.x + relativeCoord.y;
//                if (x >= 0 && x < 10 && y >= 0 && y < 10) {
//                    if (cellstatus[x][y] == CellStatus.Ship) {
//                        return false;
//                    }
//                }
//            }
//        }
        return true;
    }

    public List<Point> nearShipPoints(int i, int j) {
        List<Point> nearShipPoints = new ArrayList<>();

        for (Point shipCoord : shipCoords(i, j)) {
            for (Point relativeCoord : relativeCoords) {
                int x = shipCoord.x + relativeCoord.x;
                int y = shipCoord.y + relativeCoord.y;
                if (x >= 0 && x < 10 && y >= 0 && y < 10) {
                    if (cellstatus[x][y] == CellStatus.Empty) {
                        Point p = new Point(x, y);
                        if (!nearShipPoints.contains(p)) {
                            nearShipPoints.add(p);
                        }
                    }
                }
            }
        }
//        System.out.println("######### HAJÓ KÖRÜLÖTTI COORD:");
//        for (Point nearShipPoint : nearShipPoints) {
//            System.out.println("I: " + nearShipPoint.x + " J: " + nearShipPoint.y);
//        }
//        System.out.println("######## END");
        return nearShipPoints;
    }

}