-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUnitTest.java
More file actions
158 lines (137 loc) · 5.45 KB
/
Copy pathUnitTest.java
File metadata and controls
158 lines (137 loc) · 5.45 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
157
158
package amazons;
import org.junit.Test;
import static amazons.Piece.*;
import static org.junit.Assert.*;
import ucb.junit.textui;
/** The suite of all JUnit tests for the amazons package.
* @author Khang
*/
public class UnitTest {
/** Run the JUnit tests in this package. Add xxxTest.class entries to
* the arguments of runClasses to run other JUnit tests. */
public static void main(String[] ignored) {
textui.runClasses(UnitTest.class);
}
/** Tests basic correctness of put and get on the initialized board. */
@Test
public void testBasicPutGet() {
Board b = new Board();
b.put(BLACK, Square.sq(3, 5));
assertEquals(b.get(3, 5), BLACK);
b.put(WHITE, Square.sq(9, 9));
assertEquals(b.get(9, 9), WHITE);
b.put(EMPTY, Square.sq(3, 5));
assertEquals(b.get(3, 5), EMPTY);
}
/** Tests proper identification of legal/illegal queen moves. */
@Test
public void testIsQueenMove() {
assertFalse(Square.sq(1, 5).isQueenMove(Square.sq(1, 5)));
assertFalse(Square.sq(1, 5).isQueenMove(Square.sq(2, 7)));
assertFalse(Square.sq(0, 0).isQueenMove(Square.sq(5, 1)));
assertTrue(Square.sq(1, 1).isQueenMove(Square.sq(9, 9)));
assertTrue(Square.sq(2, 7).isQueenMove(Square.sq(8, 7)));
assertTrue(Square.sq(3, 0).isQueenMove(Square.sq(3, 4)));
assertTrue(Square.sq(7, 9).isQueenMove(Square.sq(0, 2)));
}
public static void buildBoard(Board b, Piece[][] target) {
for (int col = 0; col < Board.SIZE; col++) {
for (int row = Board.SIZE - 1; row >= 0; row--) {
Piece piece = target[Board.SIZE - row - 1][col];
b.put(piece, Square.sq(col, row));
}
}
System.out.println(b);
}
@Test
public void testToString() {
Board b = new Board();
assertEquals(INIT_BOARD_STATE, b.toString());
makeSmile(b);
assertEquals(SMILE, b.toString());
}
@Test
public void testWin() {
Board b = new Board();
b.makeMove(Square.sq("c4"), Square.sq("b4"), Square.sq("a4"));
b.makeMove(Square.sq("a10"), Square.sq("b10"), Square.sq("a10"));
b.makeMove(Square.sq("d3"), Square.sq("c3"), Square.sq("a3"));
System.out.println(b.winner());
System.out.println(b);
}
private void makeSmile(Board b) {
b.put(EMPTY, Square.sq(0, 3));
b.put(EMPTY, Square.sq(0, 6));
b.put(EMPTY, Square.sq(9, 3));
b.put(EMPTY, Square.sq(9, 6));
b.put(EMPTY, Square.sq(3, 0));
b.put(EMPTY, Square.sq(3, 9));
b.put(EMPTY, Square.sq(6, 0));
b.put(EMPTY, Square.sq(6, 9));
for (int col = 1; col < 4; col += 1) {
for (int row = 6; row < 9; row += 1) {
b.put(SPEAR, Square.sq(col, row));
}
}
b.put(EMPTY, Square.sq(2, 7));
for (int col = 6; col < 9; col += 1) {
for (int row = 6; row < 9; row += 1) {
b.put(SPEAR, Square.sq(col, row));
}
}
b.put(EMPTY, Square.sq(7, 7));
for (int lip = 3; lip < 7; lip += 1) {
b.put(WHITE, Square.sq(lip, 2));
}
b.put(WHITE, Square.sq(2, 3));
b.put(WHITE, Square.sq(7, 3));
}
static final String INIT_BOARD_STATE =
" - - - B - - B - - -\n"
+ " - - - - - - - - - -\n"
+ " - - - - - - - - - -\n"
+ " B - - - - - - - - B\n"
+ " - - - - - - - - - -\n"
+ " - - - - - - - - - -\n"
+ " W - - - - - - - - W\n"
+ " - - - - - - - - - -\n"
+ " - - - - - - - - - -\n"
+ " - - - W - - W - - -\n";
static final String SMILE =
" - - - - - - - - - -\n"
+ " - S S S - - S S S -\n"
+ " - S - S - - S - S -\n"
+ " - S S S - - S S S -\n"
+ " - - - - - - - - - -\n"
+ " - - - - - - - - - -\n"
+ " - - W - - - - W - -\n"
+ " - - - W W W W - - -\n"
+ " - - - - - - - - - -\n"
+ " - - - - - - - - - -\n";
static final String SMILE1 =
" - - - - - - - - - -\n"
+ " S S S S - - S S S -\n"
+ " S - B S - - S B S -\n"
+ " S S S S - - S S S -\n"
+ " - - - - - - - - - -\n"
+ " - - - - - - - - - -\n"
+ " - - W - - - - W - -\n"
+ " - - - W W W W - - -\n"
+ " - - - - - - - - - -\n"
+ " - - - - - - - - - -\n";
static final Piece E = Piece.EMPTY;
static final Piece W = Piece.WHITE;
static final Piece B = Piece.BLACK;
static final Piece S = Piece.SPEAR;
static final String WIN =
"B - S B B S - - - -\n"
+ " S B S S S S - - - -\n"
+ " S S S - - - - - - -\n"
+ " - - - - - - - - - -\n"
+ " W - - - S - - - - -\n"
+ " - - - W - - - - - -\n"
+ " W - - - - - - - - W\n"
+ " - - - - - - - - - -\n"
+ " - - - - - - - - - -\n"
+ " - - - - - - - - - -\n";
}