-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameBoard.java
More file actions
165 lines (145 loc) · 3.77 KB
/
GameBoard.java
File metadata and controls
165 lines (145 loc) · 3.77 KB
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
package cs3500.pa03.Model;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
/**
* Represents the game board.
*/
public class GameBoard {
private final int height;
private final int width;
private final char[][] board;
private int sunkShips;
Random rand = new Random();
private List<Ship> shipsPlace = new ArrayList<>();
Coord firstCoord;
/**
* @param height the height of the board
* @param width the width of the board
*/
public GameBoard(int height, int width) {
this.height = height;
this.width = width;
this.board = new char[height][width];
createBoard();
}
/**
* Creates the blank board.
*/
public void createBoard() {
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
this.board[i][j] = '0';
}
}
}
public int getHeight() {
return height;
}
public int getWidth() {
return width;
}
public char getBoard(int x, int y) {
return board[x][y];
}
public void setBoard(int x, int y, char h) {
this.board[x][y] = h;
}
public void sinkShip() {
sunkShips++;
}
public int getShipsSunk() {
return sunkShips;
}
/**
* @param ship the ship to be placed
* @return the coordinates of the ship
*/
public List<Coord> placeShip(ShipType ship, DirectionType direction) {
int length = ship.getLength();
boolean vertical = direction == DirectionType.VERTICAL;
char symbol = ship.getSymbol();
int x = 0;
int y = 0;
boolean validPosition = false;
ArrayList<Coord> points = new ArrayList<>();
firstCoord = null;
while (!validPosition) {
if (vertical) {
y = rand.nextInt(height - length + 1);
x = rand.nextInt(width);
} else {
y = rand.nextInt(height);
x = rand.nextInt(width - length + 1);
}
points.add(new Coord(x, y));
if (firstCoord == null) {
firstCoord = new Coord(x, y);
}
validPosition = openSpot(ship, x, y, vertical);
}
if (vertical) {
for (int i = 0; i < length; i++) {
board[y + i][x] = symbol;
}
} else {
for (int i = 0; i < length; i++) {
board[y][x + i] = symbol;
}
}
Ship newShip = new Ship(ship, points, direction, points.get(points.size() - 1));
shipsPlace.add(newShip);
return points;
}
public Coord getFirstCoord() {
return firstCoord;
}
public List<Ship> getShipsPlace() {
return shipsPlace;
}
/**
* @param ship - the ship to be placed
* @param x - the x or x coordinate
* @param y - the column or y coordinate
* @param vertical - whether the ship is vertical or horizontal
* @return boolean
*/
public boolean openSpot(ShipType ship, int x, int y, boolean vertical) {
int length = ship.getLength();
// if (vertical) {
// for (int i = 0; i < length; i++) {
// if ((board[x + i][y] != '0')
// || (x >= height && y >= width && x < 0 && y < 0)) {
// return false;
// }
// }
// } else {
// for (int i = 0; i < length; i++) {
// if ((board[x][y + i] != '0')
// || (x >= height && y >= width && x < 0 && y < 0)) {
// return false;
// }
// }
// }
if(vertical){
if(y+length >= height ){
return false;
}
for (int i = 0; i < length; i++) {
if (board[y + i][x] != '0') {
return false;
}
}
}else{
if(x+length >= width ){
return false;
}
for (int i = 0; i < length; i++) {
if (board[y][x + i] != '0') {
return false;
}
}
}
return true;
}
}