-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBoard.java
More file actions
159 lines (136 loc) · 3.18 KB
/
Copy pathBoard.java
File metadata and controls
159 lines (136 loc) · 3.18 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
/*======================================
class Board
Instantiates a player's board of Tiles
======================================*/
public class Board {
//========== Instance Variables ==========//
private static final int DEFAULT_SIZE = 11;
private Tile[][] _board;
//========== Default Constructor ==========//
public Board() {
_board = new Tile[DEFAULT_SIZE][DEFAULT_SIZE];
for( int r = 0; r < _board.length; r++ ) {
for( int c = 0; c < _board[r].length; c++ ) {
if( r == 0 )
set( r, c, new Tile("axis", 1, " " + c + " " ));
else if( c == 0 )
set( r, c, new Tile("axis", 1, " " + r + " " ));
else {
set( r, c, new Tile() );
}
}
}
}
//========== METHODS ==========//
public String toString() {
String output = "";
for( Tile[] r: _board ) {
for( Tile c: r )
output += c + " ";
output += "\n";
}
return output;
}
public boolean set( int r, int c, Tile newVal ) {
_board[r][c] = newVal;
return true;
}
public boolean free (int r, int c) {
return get(r,c)._type.equals("water");
}
//adds bonus tile if meets criteria
public void sprinkle () {
for (int r = 1; r < _board.length; r++) {
for (int c = 1; c < _board[r].length; c ++ ) {
if ( free(r,c) && (int) 3 * Math.random() < .3) {
set(r,c, new Tile ("bonus", 1, "-o-"));
}
}
}
}
//so many repeats for the nxt 2, not sure how to simplify
public boolean rangeFree (String dir, int r, int c, Tile t) {
boolean open = true;
if (dir.equals("north")) {
for (int l = r; l < r - t._length; l ++) {
open = free(l,c);
}
}
else if (dir.equals("south")) {
for (int l = r; l > r + t._length; l--) {
open = free(l,c);
}
}
else if (dir.equals("east")) {
for (int l = c; l > r - t._length; l--) {
open = free(r,l);
}
}
else {
for (int l = c; l< r + t._length -1; l++) {
open = free(r,l);
}
}
return open;
}
public boolean place (int r, int c, String dir, Tile t) {
boolean boo = false;
if (rangeFree(dir, r, c, t)) {
if (dir.equals("north")) {
for (int l = r; l <= t._length; l ++) {
boo = set (l, c, t);
}
}
else if (dir.equals("south")) {
for (int l = r; l >= t._length; l--) {
boo = set (l, c, t);
}
}
else if (dir.equals("east")) {
for (int l = c; l >= t._length; l--) {
boo = set (r, l, t);
}
}
else {
for (int l = c; l<= t._length; l++) {
boo = set (r, l, t);
}
}
}
return boo;
}
public Tile get( int r, int c ) {
return _board[r][c];
}
public void flip (int r, int c) {
_board[r][c].flip();
}
//Flips all tiles of the board that are facing up
public void faceDown() {
for( Tile[] r: _board )
for( Tile c: r )
if( c.isFaceUp() )
c.flip();
}
public int numShips () {
int total = 0;
for (Tile[] r : _board) {
for (Tile t : r) {
if (t.getType().equals("ship")) {
total ++;
}
}
}
return total;
}
public String reveal () {
String ret = "";
for (Tile[] r: _board) {
for (Tile t : r) {
ret += t._face;
}
ret += "\n";
}
return ret;
}
}