-
Notifications
You must be signed in to change notification settings - Fork 174
/
Copy pathBoard.java
154 lines (116 loc) · 4.03 KB
/
Board.java
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
public class Board {
public static char EMPTY = '.';
private int N, M;
private char[][] data;
private Board preBoard = null;
private String swapString = "";
public Board(String[] lines){
if(lines == null)
throw new IllegalArgumentException("lines cannot be null in Board constructor.");
N = lines.length;
if(N == 0)
throw new IllegalArgumentException("lines cannot be empty in Board constructor.");
M = lines[0].length();
data = new char[N][M];
for(int i = 0 ; i < N ; i ++){
if(lines[i].length() != M)
throw new IllegalArgumentException("All lines' length must be same in Board constructor.");
for(int j = 0 ; j < M ; j ++)
data[i][j] = lines[i].charAt(j);
}
}
public Board(Board board, Board preBoard, String swapString){
if(board == null)
throw new IllegalArgumentException("board can not be null in Board constructor!");
this.N = board.N;
this.M = board.M;
this.data = new char[N][M];
for(int i = 0 ; i < N ; i ++)
for(int j = 0 ; j < M ; j ++)
this.data[i][j] = board.data[i][j];
this.preBoard = preBoard;
this.swapString = swapString;
}
public Board(Board board){
this(board, null, "");
}
public int N(){ return N; }
public int M(){ return M; }
public char getData(int x, int y){
if(!inArea(x, y))
throw new IllegalArgumentException("x, y are out of index in getData!");
return data[x][y];
}
public boolean inArea(int x, int y){
return x >= 0 && x < N && y >= 0 && y < M;
}
public void print(){
for(int i = 0 ; i < N ; i ++)
System.out.println(String.valueOf(data[i]));
}
public boolean isWin(){
for(int i = 0 ; i < N ; i ++)
for(int j = 0 ; j < M ; j ++)
if(data[i][j] != EMPTY)
return false;
printSwapInfo();
return true;
}
public void swap(int x1, int y1, int x2, int y2){
if(!inArea(x1, y1) || !inArea(x2, y2))
throw new IllegalArgumentException("x, y are out of index in swap!");
char t = data[x1][y1];
data[x1][y1] = data[x2][y2];
data[x2][y2] = t;
return;
}
public void run(){
do{
drop();
}while(match());
return;
}
private static int d[][] = {{0, 1}, {1, 0}};
private boolean match(){
boolean isMatched = false;
boolean tag[][] = new boolean[N][M];
for(int x = 0 ; x < N ; x ++)
for(int y = 0 ; y < M ; y ++)
if(data[x][y] != EMPTY)
for(int i = 0 ; i < 2 ; i ++){
int newX1 = x + d[i][0];
int newY1 = y + d[i][1];
int newX2 = newX1 + d[i][0];
int newY2 = newY1 + d[i][1];
if(inArea(newX1, newY1) && inArea(newX2, newY2)
&& data[x][y] == data[newX1][newY1] && data[x][y] == data[newX2][newY2]){
tag[x][y] = true;
tag[newX1][newY1] = true;
tag[newX2][newY2] = true;
isMatched = true;
}
}
for(int x = 0 ; x < N ; x ++)
for(int y = 0 ; y < M ; y ++)
if(tag[x][y])
data[x][y] = EMPTY;
return isMatched;
}
private void drop(){
for(int j = 0 ; j < M ; j ++){
int cur = N-1;
for(int i = N-1 ; i >= 0 ; i --)
if(data[i][j] != EMPTY){
swap(cur, j, i, j);
cur--;
}
}
return;
}
public void printSwapInfo(){
if(preBoard != null)
preBoard.printSwapInfo();
System.out.println(swapString);
return;
}
}