-
Notifications
You must be signed in to change notification settings - Fork 0
/
StepTest.cpp
205 lines (136 loc) · 4.59 KB
/
StepTest.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
// file StepTest.cpp
#include <iostream>
#include <cppunit/config/SourcePrefix.h>
#include "StepTest.h"
#include "StepFactory.h"
CPPUNIT_TEST_SUITE_REGISTRATION( StepTest );
//---------------------------------------------------------------
void StepTest::setUp()
{
StepFactory the_factory;
step_m_p = the_factory.getSteps();
}
//---------------------------------------------------------------
void StepTest::tearDown()
{
if ( NULL != step_m_p )
{
delete step_m_p;
step_m_p = NULL;
}
}
//---------------------------------------------------------------
// This step checks for a valid user move
void StepTest::testStep1()
{
game_m.addMove(1, &human_m);
string message("");
bool keep_playing =
step_m_p->makeMove(1, message, &human_m, &computer_m, &game_m);
CPPUNIT_ASSERT( message ==
"Square 2 not available. Please choose another.\n" );
CPPUNIT_ASSERT( keep_playing );
}
//---------------------------------------------------------------
// This step says that if adding the human move means the
// human wins, announce the win and end the game.
void StepTest::testStep2()
{
game_m.addMove(2, &human_m);
game_m.addMove(3, &human_m);
string message("");
bool keep_playing =
step_m_p->makeMove(4, message, &human_m, &computer_m, &game_m);
CPPUNIT_ASSERT( message == "The Human has won. Congratulations!\n");
CPPUNIT_ASSERT( !keep_playing );
}
//---------------------------------------------------------------
// This step says that if there is no move left after the human
// moves, declare a draw and end the game.
void StepTest::testStep3()
{
for ( int i = 1; i < 9; i++ )
game_m.addMove(i, &computer_m);
string message("");
bool keep_playing =
step_m_p->makeMove(0, message, &human_m, &computer_m, &game_m);
CPPUNIT_ASSERT( message == "Draw Game.\n");
CPPUNIT_ASSERT( !keep_playing );
}
//---------------------------------------------------------------
// This step checks whether the computer can make a winning move.
// If so, make the move and announce the win.
void StepTest::testStep4()
{
game_m.addMove(3, &computer_m);
game_m.addMove(8, &computer_m);
string message("");
bool keep_playing =
step_m_p->makeMove(1, message, &human_m, &computer_m, &game_m);
CPPUNIT_ASSERT( message == "Computer Wins!\n" );
CPPUNIT_ASSERT( !keep_playing );
}
//---------------------------------------------------------------
// This step is for the computer making a blocking move. This first
// version is when the blocking move is the last available, creating
// a draw
void StepTest::testStep5a()
{
game_m.addMove(0, &computer_m);
game_m.addMove(1, &computer_m);
game_m.addMove(3, &computer_m);
game_m.addMove(8, &computer_m);
game_m.addMove(2, &human_m);
game_m.addMove(4, &human_m);
game_m.addMove(7, &human_m);
string message("");
bool keep_playing =
step_m_p->makeMove(5, message, &human_m, &computer_m, &game_m);
CPPUNIT_ASSERT( message == "Draw Game.\n" );
CPPUNIT_ASSERT( !keep_playing );
}
//---------------------------------------------------------------
// This next case is as above, but the game can continue
void StepTest::testStep5b()
{
game_m.addMove(3, &computer_m);
game_m.addMove(8, &computer_m);
game_m.addMove(2, &human_m);
game_m.addMove(7, &human_m);
string message("");
bool keep_playing =
step_m_p->makeMove(6, message, &human_m, &computer_m, &game_m);
CPPUNIT_ASSERT( message == "" );
CPPUNIT_ASSERT( keep_playing );
}
//---------------------------------------------------------------
// This step is the last in the chain. It has the computer pick
// a square based on its rules. The first case creates a draw.
void StepTest::testLastStepA()
{
game_m.addMove(1, &computer_m);
game_m.addMove(4, &computer_m);
game_m.addMove(6, &computer_m);
game_m.addMove(8, &computer_m);
game_m.addMove(2, &human_m);
game_m.addMove(3, &human_m);
game_m.addMove(5, &human_m);
string message("");
bool keep_playing =
step_m_p->makeMove(0, message, &human_m, &computer_m, &game_m);
CPPUNIT_ASSERT( message == "Draw Game.\n" );
CPPUNIT_ASSERT( !keep_playing );
}
//---------------------------------------------------------------
// This case does not create a draw
void StepTest::testLastStepB()
{
game_m.addMove(1, &computer_m);
game_m.addMove(3, &human_m);
string message("");
bool keep_playing =
step_m_p->makeMove(6, message, &human_m, &computer_m, &game_m);
CPPUNIT_ASSERT( message == "" );
CPPUNIT_ASSERT( keep_playing );
}
//---------------------------------------------------------------