-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBoard.java
113 lines (92 loc) · 3.4 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
import java.util.*;
public class Board {
private Piece[][] pieces = new Piece[8][8]; // initially null
private static Board b_instance = null;
private List<BoardListener> listenerList = new LinkedList<>(); // store all listeners
private Board() {
}
public static Board theBoard() {
if (b_instance == null)
b_instance = new Board();
return b_instance; // implement this
}
public String isOccupied(String loc){
/* if loc is occupied by any Piece, return the color char, else return '0'
* */
int[] locArray = Helper.getLocArray(loc);
if (pieces[locArray[0]][locArray[1]] == null)
return null;
return pieces[locArray[0]][locArray[1]].color().getColorStr();
}
// Returns piece at given loc or null if no such piece
// exists
public Piece getPiece(String loc) {
int[] locArray = Helper.getLocArray(loc);
int i=locArray[0], j=locArray[1];
// if invalid loc
if(i >=8 || j >=8)
throw new ArrayIndexOutOfBoundsException();
return pieces[i][j];
// throw new UnsupportedOperationException();
}
public void addPiece(Piece p, String loc) {
int[] locArray = Helper.getLocArray(loc);
int i=locArray[0], j=locArray[1];
// if invalid loc
if(i >=8 || j >=8 || pieces[i][j]!=null)
throw new ArrayIndexOutOfBoundsException();
pieces[i][j] = p;
}
public void movePiece(String from, String to) {
int[] fromArray = Helper.getLocArray(from);
int[] toArray = Helper.getLocArray(to);
// if invalid loc
if(fromArray[0] >=8 || fromArray[1] >=8 || toArray[0] >=8 || toArray[1] >=8)
throw new ArrayIndexOutOfBoundsException();
// if invalid move
Piece p = pieces[fromArray[0]][fromArray[1]];
if(!p.moves(b_instance, from).contains(to)){
System.out.println("invalid move: "+p.toString()+" from " + from + " to " + to);
throw new UnsupportedOperationException();
}
// make a move
// call the observers
for(BoardListener listener : listenerList){
listener.onMove(from, to, p);
// if capture sth
if(pieces[toArray[0]][toArray[1]]!=null){
listener.onCapture(p,pieces[toArray[0]][toArray[1]]);
}
}
// modify the board pieces
pieces[toArray[0]][toArray[1]] = p;
pieces[fromArray[0]][fromArray[1]] = null;
}
public void clear() {
pieces = new Piece[8][8];
// throw new UnsupportedOperationException();
}
public void registerListener(BoardListener bl) {
listenerList.add(bl);
// throw new UnsupportedOperationException();
}
public void removeListener(BoardListener bl) {
if(listenerList.contains(bl))
listenerList.remove(bl);
else{
throw new UnsupportedOperationException();}
}
public void removeAllListeners() {
listenerList = new LinkedList<>();
// throw new UnsupportedOperationException();
}
public void iterate(BoardInternalIterator bi) {
for(int i=0;i<8;i++){
for(int j=0;j<8;j++){
if(pieces[i][j]!=null)
bi.visit(Helper.getLocStr(j,i), pieces[j][i]);
}
}
// throw new UnsupportedOperationException();
}
}