-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRuleTest.cpp
341 lines (233 loc) · 7.96 KB
/
RuleTest.cpp
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
// file RuleTest.cpp
#include <iostream>
#include <cppunit/config/SourcePrefix.h>
#include "RuleTest.h"
#include "RuleFactory.h"
CPPUNIT_TEST_SUITE_REGISTRATION( RuleTest );
//---------------------------------------------------------------
void RuleTest::setUp()
{
RuleFactory the_factory;
rule_m_p = the_factory.getRules();
}
//---------------------------------------------------------------
void RuleTest::tearDown()
{
if ( NULL != rule_m_p )
{
delete rule_m_p;
rule_m_p = NULL;
}
}
//---------------------------------------------------------------
// This rule says if there is one square left, pick it.
void RuleTest::testRule1()
{
for ( int i = 0; i < 3; i++ )
game_m.addMove(i, &human_m);
for ( int i = 4; i < 9; i++ )
game_m.addMove(i, &computer_m);
int move = rule_m_p->getMove(&human_m, &computer_m, &game_m);
CPPUNIT_ASSERT( 3 == move );
}
//---------------------------------------------------------------
// This rule says that if the center square is not picked, pick it
void RuleTest::testRule2()
{
int move = rule_m_p->getMove(&human_m, &computer_m, &game_m);
CPPUNIT_ASSERT( 8 == move );
}
//---------------------------------------------------------------
// This rule says that if the center square is the only one taken
// by the human, pick an even square.
void RuleTest::testRule3()
{
game_m.addMove(8, &human_m);
int move = rule_m_p->getMove(&human_m, &computer_m, &game_m);
CPPUNIT_ASSERT( 0 == (move % 2) );
}
//---------------------------------------------------------------
// This rule says if the if computer has the center square and human
// has one other square, move in square ((human + 3) mod 8).
// This test has the human making a move to an even square
void RuleTest::testRule4Odd()
{
game_m.addMove(8, &computer_m);
game_m.addMove(6, &human_m);
int move = rule_m_p->getMove(&human_m, &computer_m, &game_m);
CPPUNIT_ASSERT( 1 == move );
}
//---------------------------------------------------------------
// This test is as above but has the human making a move to an odd square
void RuleTest::testRule4Even()
{
game_m.addMove(8, &computer_m);
game_m.addMove(5, &human_m);
int move = rule_m_p->getMove(&human_m, &computer_m, &game_m);
CPPUNIT_ASSERT( 0 == move );
}
//---------------------------------------------------------------
// This test is for when the computer has picked the center square, and
// the human has picked two even squares, but not created a blocking
// situation. This first case is when the human has squares 0 and 4
void RuleTest::testRule5a()
{
game_m.addMove(8, &computer_m);
game_m.addMove(0, &human_m);
game_m.addMove(4, &human_m);
int move = rule_m_p->getMove(&human_m, &computer_m, &game_m);
CPPUNIT_ASSERT( 1 == (move % 2) );
}
//---------------------------------------------------------------
// This next case is as above, but the human has 2 and 6
void RuleTest::testRule5b()
{
game_m.addMove(8, &computer_m);
game_m.addMove(2, &human_m);
game_m.addMove(6, &human_m);
int move = rule_m_p->getMove(&human_m, &computer_m, &game_m);
CPPUNIT_ASSERT( 1 == (move % 2) );
}
//---------------------------------------------------------------
// This test is where the computer has the center, and the human
// has an odd and an even. The computer has to pick the right odd square
void RuleTest::testRule6a()
{
game_m.addMove(8, &computer_m);
game_m.addMove(2, &human_m);
game_m.addMove(5, &human_m);
int move = rule_m_p->getMove(&human_m, &computer_m, &game_m);
CPPUNIT_ASSERT( 7 == move );
}
//---------------------------------------------------------------
// Same as above but rotated around the grid
void RuleTest::testRule6b()
{
game_m.addMove(8, &computer_m);
game_m.addMove(6, &human_m);
game_m.addMove(1, &human_m);
int move = rule_m_p->getMove(&human_m, &computer_m, &game_m);
CPPUNIT_ASSERT( 3 == move );
}
//---------------------------------------------------------------
// These next two are mirror images of the first two
void RuleTest::testRule6c()
{
game_m.addMove(8, &computer_m);
game_m.addMove(2, &human_m);
game_m.addMove(7, &human_m);
int move = rule_m_p->getMove(&human_m, &computer_m, &game_m);
CPPUNIT_ASSERT( 5 == move );
}
//---------------------------------------------------------------
void RuleTest::testRule6d()
{
game_m.addMove(8, &computer_m);
game_m.addMove(6, &human_m);
game_m.addMove(3, &human_m);
int move = rule_m_p->getMove(&human_m, &computer_m, &game_m);
CPPUNIT_ASSERT( 1 == move );
}
//---------------------------------------------------------------
void RuleTest::testRule6e()
{
game_m.addMove(8, &computer_m);
game_m.addMove(0, &human_m);
game_m.addMove(5, &human_m);
int move = rule_m_p->getMove(&human_m, &computer_m, &game_m);
CPPUNIT_ASSERT( 3 == move );
}
//---------------------------------------------------------------
// Human has 9 and a corner. Computer has the opposite corner.
// Computer should pick two more than the computer's move
void RuleTest::testRule7a()
{
game_m.addMove(8, &human_m);
game_m.addMove(0, &human_m);
game_m.addMove(4, &computer_m);
int move = rule_m_p->getMove(&human_m, &computer_m, &game_m);
CPPUNIT_ASSERT( 6 == move );
}
//---------------------------------------------------------------
void RuleTest::testRule7b()
{
game_m.addMove(8, &human_m);
game_m.addMove(2, &human_m);
game_m.addMove(6, &computer_m);
int move = rule_m_p->getMove(&human_m, &computer_m, &game_m);
CPPUNIT_ASSERT( 0 == move );
}
//---------------------------------------------------------------
void RuleTest::testRule7c()
{
game_m.addMove(8, &human_m);
game_m.addMove(4, &human_m);
game_m.addMove(0, &computer_m);
int move = rule_m_p->getMove(&human_m, &computer_m, &game_m);
CPPUNIT_ASSERT( 2 == move );
}
//---------------------------------------------------------------
void RuleTest::testRule7d()
{
game_m.addMove(8, &human_m);
game_m.addMove(6, &human_m);
game_m.addMove(2, &computer_m);
int move = rule_m_p->getMove(&human_m, &computer_m, &game_m);
CPPUNIT_ASSERT( 4 == move );
}
//---------------------------------------------------------------
// If the computer has 8, and the human has two odd squares,
// the computer needs to pick the right square, otherwise the
// human can set up a win.
void RuleTest::testRule8a()
{
game_m.addMove(8, &computer_m);
game_m.addMove(1, &human_m);
game_m.addMove(3, &human_m);
int move = rule_m_p->getMove(&human_m, &computer_m, &game_m);
CPPUNIT_ASSERT( 4 == move );
}
//---------------------------------------------------------------
void RuleTest::testRule8b()
{
game_m.addMove(8, &computer_m);
game_m.addMove(1, &human_m);
game_m.addMove(7, &human_m);
int move = rule_m_p->getMove(&human_m, &computer_m, &game_m);
CPPUNIT_ASSERT( 0 == move );
}
//---------------------------------------------------------------
// In this and the next case, the human moves are on the opposite
// sides of the center
void RuleTest::testRule8c()
{
game_m.addMove(8, &computer_m);
game_m.addMove(1, &human_m);
game_m.addMove(5, &human_m);
int move = rule_m_p->getMove(&human_m, &computer_m, &game_m);
CPPUNIT_ASSERT( 6 == move );
}
//---------------------------------------------------------------
void RuleTest::testRule8d()
{
game_m.addMove(8, &computer_m);
game_m.addMove(3, &human_m);
game_m.addMove(7, &human_m);
int move = rule_m_p->getMove(&human_m, &computer_m, &game_m);
CPPUNIT_ASSERT( 0 == move );
}
//---------------------------------------------------------------
// This rule is the last in the chain. It just says pick any open
// square.
void RuleTest::testLastRule()
{
int a_moves[] = {3, 4, 5, 6, 7};
IntSet some_moves(a_moves, a_moves+5);
game_m.addMove(0, &computer_m);
game_m.addMove(1, &human_m);
game_m.addMove(2, &computer_m);
game_m.addMove(8, &human_m);
int move = rule_m_p->getMove(&human_m, &computer_m, &game_m);
CPPUNIT_ASSERT( some_moves.count(move) );
}
//---------------------------------------------------------------