-
Notifications
You must be signed in to change notification settings - Fork 0
/
Grid.java
executable file
·268 lines (249 loc) · 9.31 KB
/
Grid.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
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
package battleships;
import java.awt.Point;
import java.util.*;
public class Grid {
private Scanner scan = new Scanner(System.in);
private String[][] playerGrid = new String[Constants.gridSize][Constants.gridSize];
private String[][] attackGrid = new String[Constants.gridSize][Constants.gridSize];
private List<Ship> ships = new ArrayList<>();
private List<Point> pointList = new ArrayList<>();
//launches two grids, one for player to place ships and one to attack
public Grid() {
for(int i = 0; i < Constants.gridSize; i++) {
for(int j = 0; j < Constants.gridSize; j++) {
playerGrid[i][j] = Constants.WAT_SYM;
attackGrid[i][j] = Constants.WAT_SYM;
}
}
}
//calculating player life
public int countX() {
int hp = 0;
for(int i = 0; i < Constants.gridSize; i++) {
for(int j = 0; j < Constants.gridSize; j++) {
if (playerGrid[i][j] == Constants.SHIP_SYM) {
hp++;
}
}
}
return hp;
}
//naming ships instead of numbers
public String shipNbr(int i) {
switch (i) {
case 0:
String first = "first";
return first;
case 1:
String second = "second";
return second;
case 2:
String third = "third";
return third;
case 3:
String fourth = "fourth";
return fourth;
}
String fifth = "fifth";
return fifth;
}
//places ships, called x times depended on game settings
public void placeShips(int shipnbr) {
boolean inputCorrect = true;
int shipSize = 0;
int shipLives = 0;
Position position = null;
String nbr = shipNbr(shipnbr);
Ship ship = new Ship(shipSize, shipLives, position);
ships.add(ship);
printGrid(playerGrid);
while (inputCorrect) {
try {
System.out.println("Please enter the starting coordinates for your " + nbr +" ship (x y):");
Point from = new Point(scan.nextInt(), scan.nextInt());
System.out.println("Please enter the ending coordinates for your " + nbr +" ship (x y):");
Point to = new Point(scan.nextInt(), scan.nextInt());
position = new Position(from, to);
while (isPointFreeForPlacement(from, to) || (isOutsideGrid(from, to)) || isShipTooBig(from, to) || isShipWrongWay(from, to)) {
System.out.println("Incorrect input, try again.");
System.out.println("Please enter the starting coordinates for your " + nbr +" ship (x y):");
from = new Point(scan.nextInt(), scan.nextInt());
System.out.println("Please enter the ending coordinates for your " + nbr +" ship (x y):");
to = new Point(scan.nextInt(), scan.nextInt());
position = new Position(from, to);
}
shipSize = (int) Utils.distanceBetweenPoints(from, to);
ship.setPosition(position);
ship.setShipSize(shipSize);
ship.setShipLives(shipSize);
drawShips(from, to, playerGrid);
inputCorrect = false;
} catch (InputMismatchException e) {
System.out.println("Incorrect input. Only use numbers. Try again.");
System.out.println();
scan.nextLine();
}
}
}
public void placeComputerShips(int shipnbr) {
int shipSize = 0;
int shipLives = 0;
Position position = null;
Ship ship = new Ship(shipSize, shipLives, position);
ships.add(ship);
Point from = new Point(0, 0);
Point to = new Point(0, 0);
int rand1 = new Random().nextInt((9 - 0) + 1) + 0;
int rand2 = new Random().nextInt((9 - 0) + 1) + 0;
int rand3 = 0;
boolean direction = Math.random() < 0.5;
if (direction) {
rand3 = new Random().nextInt((9 - rand2) + 1) + rand2;
from = new Point(rand1, rand2);
to = new Point(rand1, rand3);
} else {
rand3 = new Random().nextInt((9 - rand1) + 1) + rand1;
from = new Point(rand1, rand2);
to = new Point(rand3, rand2);
}
while (isPointFreeForPlacement(from, to) || isShipTooBig(from, to)) {
if (direction) {
rand3 = new Random().nextInt((9 - rand2) + 1) + rand2;
from = new Point(rand1, rand2);
to = new Point(rand1, rand3);
} else {
rand3 = new Random().nextInt((9 - rand1) + 1) + rand1;
from = new Point(rand1, rand2);
to = new Point(rand3, rand2);
}
}
position = new Position(from, to);
shipSize = (int) Utils.distanceBetweenPoints(from, to);
ship.setPosition(position);
ship.setShipSize(shipSize);
ship.setShipLives(shipSize);
drawShips(from, to, playerGrid);
printGrid(playerGrid);
}
//Prints map depending on map (playerGrid or attackGrid
public void printGrid(String[][] grid) {
System.out.println();
System.out.print(" ");
for(int i = 0; i < Constants.gridSize; i++) {
System.out.print(i + " ");
}
System.out.println();
for(int i = 0; i < Constants.gridSize; i++) {
System.out.print((i) + " ");
for(int j = 0; j < Constants.gridSize; j++) {
System.out.print(grid[i][j] + " ");
}
System.out.println();
}
System.out.println();
}
//for printing attackGrid from Player.java
public void printAttackGrid() {
printGrid(attackGrid);
}
public boolean isShipWrongWay(Point from, Point to) {
if (from.getX() > to.getX() || from.getY() > to.getY()) {
System.out.println("You cannot place ships this way. Try again.");
return true;
}
return false;
}
//checks if ship is too big, eg 2x2, 3x3, 4x4
public boolean isShipTooBig(Point from, Point to) {
if (Utils.distanceBetweenPoints(from, to) > 5
|| ((to.getX() == from.getX() + 1) && (to.getY() == from.getY() + 1))
|| ((to.getX() == from.getX() + 2) && (to.getY() == from.getY() + 2))
|| ((to.getX() == from.getX() + 3) && (to.getY() == from.getY() + 3))
) {
return true;
}
return false;
}
//checks if attempted to place ship outside grid
public boolean isOutsideGrid(Point from, Point to) {
if (from.getX() > 9 || from.getX() < 0
|| from.getY() > 9 || from.getY() < 0
|| to.getX() > 9 || to.getX() < 0
|| to.getY() > 9 || to.getY() < 0) {
return true;
}
return false;
}
//checks if ship is attempted to be placed ontop/1 grid too close
public boolean isPointFreeForPlacement(Point from, Point to) {
for (Point p: pointList) {
if (p.equals(from) || p.equals(to))
return true;
}
return false;
}
//add ships to playerGrid
private void drawShips(Point from, Point to, String[][] grid) {
if ((int) from.getY() <= to.getY() && from.getX() <= to.getX()) {
for(int i = (int) from.getX(); i <= to.getX(); i++) {
for(int j = (int) from.getY(); j <= to.getY(); j++) {
grid[j][i] = Constants.SHIP_SYM;
}
}
} else {
for(int i = (int) from.getX(); i >= to.getX(); i++) {
for(int j = (int) from.getY(); j >= to.getY(); j++) {
grid[j][i] = Constants.SHIP_SYM;
}
}
}
addPoint(from, to);
}
//attacking, returns null if miss, true if hit/sunk
public Ship targetShip(Point point, String name) {
Ship hitShip = null;
for(Ship s : ships) {
if(s.getPosition() != null) {
if(Utils.isPointEqual(point, s.getPosition()) || Utils.isPointBetween(point, s.getPosition())) {
s.reduceShipLives();
if (s.isShipSunk() == true) {
System.out.println(name + " targets position (" + (int) point.getX() +", " + (int) point.getY() + ") and...");
System.out.println("HITS!");
System.out.println("Good job! You sunk a ship!");
System.out.println();
hitShip = s;
updateShipOnBoard(point, Constants.HIT_SYM);
return hitShip;
}
System.out.println(name + " targets position (" + (int) point.getX() +", " + (int) point.getY() + ") and...");
System.out.println("HITS!");
System.out.println();
updateShipOnBoard(point, Constants.HIT_SYM);
hitShip = s;
return hitShip;
}
}
}
System.out.println(name + " targets position (" + (int) point.getX() +", " + (int) point.getY() + ") and...");
System.out.println("misses...");
System.out.println();
updateShipOnBoard(point, Constants.MISS_SYM);
return hitShip;
}
//updates playerGrid with 1 or 0, 1 for hit 0 for miss
private void updateShipOnBoard(Point point, String result) {
int x = (int) point.getX();
int y = (int) point.getY();
attackGrid[y][x] = result;
}
//adds points around each point to prevent from too close placement
public List<Point> addPoint(Point from, Point to) {
for (int i = (int) from.getX(); i <= (int) to.getX(); i++) {
for (int j = (int) from.getY(); j <= (int) to.getY(); j++) {
var point = List.of(new Point(i, j), new Point(i+1, j), new Point(i, j+1), new Point(i-1, j), new Point(i, j-1), new Point(i-1, j-1), new Point(i+1, j-1), new Point(i-1, j+1), new Point(i+1, j+1));
pointList.addAll(point);
}
}
return pointList;
}
}