-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBoard.java
More file actions
178 lines (145 loc) · 4.81 KB
/
Board.java
File metadata and controls
178 lines (145 loc) · 4.81 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
166
167
168
169
170
171
172
173
174
175
176
177
178
package battleship;
import java.util.ArrayList;
import java.util.List;
import javafx.event.EventHandler;
import javafx.geometry.Point2D;
import javafx.scene.Parent;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
/**
*
* @author amberprice
*/
public class Board extends Parent {
private VBox rows = new VBox();
private boolean enemy = false;
public int ships = 5;
public Board(boolean enemy, EventHandler<? super MouseEvent> handler) {
this.enemy = enemy;
for (int y = 0; y < 10; y++) {
HBox row = new HBox();
for (int x = 0; x < 10; x++) {
Cell c = new Cell(x, y, this);
c.setOnMouseClicked(handler);
row.getChildren().add(c);
}
rows.getChildren().add(row);
}
getChildren().add(rows);
}
public boolean placeShip(Ship ship, int x, int y) {
if (canPlaceShip(ship, x, y)) {
int length = ship.type;
if (ship.vertical) {
for (int i = y; i < y + length; i++) {
Cell cell = getCell(x, i);
cell.ship = ship;
if (!enemy) {
cell.setFill(Color.WHITE);
cell.setStroke(Color.GREEN);
}
}
}
else {
for (int i = x; i < x + length; i++) {
Cell cell = getCell(i, y);
cell.ship = ship;
if (!enemy) {
cell.setFill(Color.WHITE);
cell.setStroke(Color.GREEN);
}
}
}
return true;
}
return false;
}
public Cell getCell(int x, int y) {
return (Cell)((HBox)rows.getChildren().get(y)).getChildren().get(x);
}
private Cell[] getNeighbors(int x, int y) {
Point2D[] points = new Point2D[] {
new Point2D(x - 1, y),
new Point2D(x + 1, y),
new Point2D(x, y - 1),
new Point2D(x, y + 1)
};
List<Cell> neighbors = new ArrayList<Cell>();
for (Point2D p : points) {
if (isValidPoint(p)) {
neighbors.add(getCell((int)p.getX(), (int)p.getY()));
}
}
return neighbors.toArray(new Cell[0]);
}
private boolean canPlaceShip(Ship ship, int x, int y) {
int length = ship.type;
if (ship.vertical) {
for (int i = y; i < y + length; i++) {
if (!isValidPoint(x, i))
return false;
Cell cell = getCell(x, i);
if (cell.ship != null)
return false;
for (Cell neighbor : getNeighbors(x, i)) {
if (!isValidPoint(x, i))
return false;
if (neighbor.ship != null)
return false;
}
}
}
else {
for (int i = x; i < x + length; i++) {
if (!isValidPoint(i, y))
return false;
Cell cell = getCell(i, y);
if (cell.ship != null)
return false;
for (Cell neighbor : getNeighbors(i, y)) {
if (!isValidPoint(i, y))
return false;
if (neighbor.ship != null)
return false;
}
}
}
return true;
}
private boolean isValidPoint(Point2D point) {
return isValidPoint(point.getX(), point.getY());
}
private boolean isValidPoint(double x, double y) {
return x >= 0 && x < 10 && y >= 0 && y < 10;
}
public class Cell extends Rectangle {
public int x, y;
public Ship ship = null;
public boolean wasShot = false;
private Board board;
public Cell(int x, int y, Board board) {
super(30, 30);
this.x = x;
this.y = y;
this.board = board;
setFill(Color.LIGHTGRAY);
setStroke(Color.BLACK);
}
public boolean shoot() {
wasShot = true;
setFill(Color.BLACK);
if (ship != null) {
ship.hit();
setFill(Color.RED);
if (!ship.isAlive()) {
board.ships--;
}
return true;
}
return false;
}
}
}