1
+ import java .util .LinkedList ;
2
+ import java .util .List ;
3
+ import java .util .NoSuchElementException ;
4
+
5
+ public class Board {
6
+ private static Board instance = new Board ();
7
+
8
+ //col:a-h == 0-7 row:1-8 == 0-7
9
+ private Piece [][] pieces = new Piece [8 ][8 ];
10
+ private List <BoardListener > listeners ;
11
+
12
+ private Board () { }
13
+
14
+ public static Board theBoard () {
15
+ return instance ;
16
+ }
17
+
18
+ // Returns piece at given loc or null if no such piece
19
+ // exists
20
+ public Piece getPiece (String loc ) {
21
+ int col = getCol (loc );
22
+ int row = getRow (loc );
23
+ return pieces [col ][row ];
24
+ }
25
+
26
+ public void addPiece (Piece p , String loc ) {
27
+ int col = getCol (loc );
28
+ int row = getRow (loc );
29
+ if (pieces [col ][row ] == null ){
30
+ pieces [col ][row ] = p ;
31
+ }else throw new NoSuchElementException ("Board addPiece exception: position is already occupied!" );
32
+ }
33
+
34
+ public void movePiece (String from , String to ) {
35
+ int fcol = getCol (from );
36
+ int frow = getRow (from );
37
+ int tcol = getCol (to );
38
+ int trow = getRow (to );
39
+ //check if there is a piece in from position
40
+ if (pieces [fcol ][frow ] != null ){
41
+ //check if move valid
42
+ List <String > toList = pieces [fcol ][frow ].moves (instance , from );
43
+ if (toList .contains (to )){
44
+ for (BoardListener l : listeners ){
45
+ //broadcast onMove
46
+ l .onMove (from , to , pieces [fcol ][frow ]);
47
+ }
48
+ //to position already has a piece, capture
49
+ if (pieces [tcol ][trow ] != null ){
50
+ //broadcast onCapture
51
+ for (BoardListener l : listeners ){
52
+ l .onCapture (pieces [fcol ][frow ], pieces [tcol ][trow ]);
53
+ }
54
+ }
55
+ //from capture to
56
+ pieces [tcol ][trow ] = pieces [fcol ][frow ];
57
+ //from is vacant
58
+ pieces [fcol ][frow ] = null ;
59
+ }else {
60
+ throw new NoSuchElementException ("board movePiece exception: invalid move: " + from + " to " + to );
61
+ }
62
+ }else {
63
+ throw new NoSuchElementException ("board movePiece exception: There is no piece at: " + from );
64
+ }
65
+ }
66
+
67
+ public void clear () {
68
+ //reassign, garbage collection
69
+ pieces = new Piece [8 ][8 ];
70
+ }
71
+
72
+ public void registerListener (BoardListener bl ) {
73
+ listeners .add (bl );
74
+ }
75
+
76
+ public void removeListener (BoardListener bl ) {
77
+ listeners .remove (bl );
78
+ }
79
+
80
+ public void removeAllListeners () {
81
+ listeners = new LinkedList <BoardListener >();
82
+ }
83
+
84
+ public void iterate (BoardExternalIterator bi ) {
85
+ for (int i = 0 ; i < 8 ; i ++){
86
+ for (int j = 0 ; j < 8 ; j ++){
87
+ bi .visit (toStringLoc (i , j ), pieces [i ][j ]);
88
+ }
89
+ }
90
+ }
91
+
92
+ private int getRow (String loc ){
93
+ int r = Character .getNumericValue (loc .charAt (1 )) - 1 ;
94
+ if ((r >= 0 ) && (r <= 7 )) {
95
+ return Character .getNumericValue (loc .charAt (1 )) - 1 ;
96
+ }else {
97
+ throw new NoSuchElementException ("Board exception: row wrong" + loc );
98
+ }
99
+ }
100
+
101
+ private int getCol (String loc ) {
102
+ char c = loc .charAt (0 );
103
+ if ((c >= 'a' ) && (c <= 'h' )) {
104
+ return c - 'a' ;
105
+ } else {
106
+ throw new NoSuchElementException ("Board exception: loc col wrong" + loc );
107
+ }
108
+ }
109
+ private String toStringLoc (int col , int row ){
110
+ char c = (char ) (col + 'a' );
111
+ StringBuffer sb = new StringBuffer ();
112
+ sb .append (c );
113
+ sb .append (row + 1 );
114
+ return sb .toString ();
115
+ }
116
+ }
0 commit comments