-
Notifications
You must be signed in to change notification settings - Fork 174
/
Copy pathBoard.java
83 lines (61 loc) · 2.11 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
public class Board {
public static char EMPTY = '.';
private int N, M;
private char[][] data;
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){
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];
}
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;
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(){
}
}