-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBoard.java
More file actions
156 lines (135 loc) · 4.49 KB
/
Board.java
File metadata and controls
156 lines (135 loc) · 4.49 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
package game2048;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Formatter;
import java.util.Iterator;
import java.util.List;
import java.util.Random;
/**
* @author hug
*/
public class Board implements Iterable<Tile> {
/** Current contents of the board. */
private Tile[][] values;
/** Side that the board currently views as north. */
private Side viewPerspective;
public Board(int size) {
values = new Tile[size][size];
viewPerspective = Side.NORTH;
}
/** Shifts the view of the board such that the board behaves as if side S is north. */
public void setViewingPerspective(Side s) {
viewPerspective = s;
}
/** Create a board where RAWVALUES hold the values of the tiles on the board
* (0 is null) with a current score of SCORE and the viewing perspective set to north. */
public Board(int[][] rawValues, int score) {
int size = rawValues.length;
values = new Tile[size][size];
viewPerspective = Side.NORTH;
for (int col = 0; col < size; col += 1) {
for (int row = 0; row < size; row += 1) {
int value = rawValues[size - 1 - row][col];
Tile tile;
if (value == 0) {
tile = null;
} else {
tile = Tile.create(value, col, row);
}
values[col][row] = tile;
}
}
}
/** Returns the size of the board. */
public int size() {
return values.length;
}
/** Shifts the view of the Board. */
public void startViewingFrom(Side s) {
viewPerspective = s;
}
/** Return the current Tile at (COL, ROW), when sitting with the board
* oriented so that SIDE is at the top (farthest) from you. */
private Tile vtile(int col, int row, Side side) {
return values[side.col(col, row, size())][side.row(col, row, size())];
}
/** Return the current Tile at (COL, ROW), where 0 <= ROW < size(),
* 0 <= COL < size(). Returns null if there is no tile there. */
public Tile tile(int col, int row) {
return vtile(col, row, viewPerspective);
}
/** Clear the board to empty and reset the score. */
public void clear() {
for (Tile[] column : values) {
Arrays.fill(column, null);
}
}
/** Adds the tile T to the board */
public void addTile(Tile t) {
values[t.col()][t.row()] = t;
}
/** Places the Tile TILE at column COL, row ROW where COL and ROW are
* treated as coordinates with respect to the current viewPerspective.
*
* Returns whether or not this move is a merge.
* */
public boolean move(int col, int row, Tile tile) {
int pcol = viewPerspective.col(col, row, size()),
prow = viewPerspective.row(col, row, size());
if (tile.col() == pcol && tile.row() == prow) {
return false;
}
Tile tile1 = vtile(col, row, viewPerspective);
values[tile.col()][tile.row()] = null;
if (tile1 == null) {
values[pcol][prow] = tile.move(pcol, prow);
return false;
} else {
values[pcol][prow] = tile.merge(pcol, prow, tile1);
return true;
}
}
@Override
/** Returns the board as a string, used for debugging. */
public String toString() {
Formatter out = new Formatter();
out.format("%n[%n");
for (int row = size() - 1; row >= 0; row -= 1) {
for (int col = 0; col < size(); col += 1) {
if (tile(col, row) == null) {
out.format("| ");
} else {
out.format("|%4d", tile(col, row).value());
}
}
out.format("|%n");
}
return out.toString();
}
/** Iterates through teach tile in the board. */
private class AllTileIterator implements Iterator<Tile>, Iterable<Tile> {
int r, c;
AllTileIterator() {
r = 0;
c = 0;
}
public boolean hasNext() {
return r < size();
}
public Tile next() {
Tile t = tile(c, r);
c = c + 1;
if (c == size()) {
c = 0;
r = r + 1;
}
return t;
}
public Iterator<Tile> iterator() {
return this;
}
}
public Iterator<Tile> iterator() {
return new AllTileIterator();
}
}