-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBoard.java
More file actions
81 lines (72 loc) · 1.66 KB
/
Board.java
File metadata and controls
81 lines (72 loc) · 1.66 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
public class Board {
private String[][] squares;
public Board() {
squares = new String[10][10];
for (int i = 0; i < squares.length; i++) {
for (int j = 0; j < squares[0].length; j++) {
squares[i][j] = "-";
}
}
}
public String toString() {
String thing = "";
for (String[] r : squares) {
for (String c : r) {
thing += c + " ";
}
thing += "\n";
}
return thing;
}
public boolean addShip(int row, int col, int len, boolean horizontal) {
if (horizontal) {
for (int i = 0; i < len; i++) {
if (squares[row][col + i] == "b" || squares[row][col + i] == "x" || squares[row][col + i] == "m") {
return false;
}
}
for (int i = 0; i < len; i++) {
squares[row][col + i] = "b";
}
return true;
} else {
for (int i = 0; i < len; i++) {
if (squares[row + i][col] == "b" || squares[row + i][col] == "x" || squares[row + i][col] == "m") {
return false;
}
}
for (int i = 0; i < len; i++) {
squares[row + i][col] = "b";
}
return true;
}
}
public boolean foundShip(int len) {
return false;
}
public int shoot(int row, int col) {
switch (squares[row][col]) {
case "b":
squares[row][col] = "b";
return 1;
case "x":
case "m":
return 2;
case "-":
squares[row][col] = "m";
return 0;
default:
return -1;
}
}
public boolean gameOver() {
for (String[] r : squares) {
for (String c : r) {
if (c == "b") {
return false;
}
}
}
return true;
}
}