-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
378 lines (337 loc) · 8.73 KB
/
main.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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
#include <iostream>
using namespace std;
#include "Tetris.h"
#include "TetrisLine.h"
#include "TetrisSquare.h"
#include "KeyboardMessage.h"
#include "MoveTetrisHandler.h"
#include "PlayingState.h"
#include "KeyDownHandler.h"
#include "GameFram.h"
#include "CommonMessage.h"
#include "TetrisFactory.h"
#include "GameMap.h"
#include "ENU_KEY.h"
void test_rotate()
{
cout << "Begin test_rotate():\n";
Tetris *ter = new Tetris(TYP_TETRIS_1);
cout << "type = " << ter->getType() << endl;
cout << "position = " << ter->getPosition() << endl;
cout << "Begin rotate\n";
cout << *(TetrisBase*)(ter) << endl;
ter->rotate(90);
cout << "after rotate 90\n";
cout << *(TetrisBase*)(ter) << endl;
ter->rotate(180);
cout << "after rotate 180\n";
cout << *(TetrisBase*)(ter) << endl;
cout << "after rotate 270\n";
ter->rotate(270);
cout << *(TetrisBase*)(ter) << endl;
delete ter;
cout << "end test_rotate():\n\n";
}
void test_otherTetris()
{
cout << "Begin test_otherTetris():\n";
for (int i = 0; i < 5; ++i)
{
Tetris *ter = new Tetris((PDCTYPE)i);
cout << *(TetrisBase*)(ter) << endl;
delete ter;
}
cout << "end test_otherTetris():\n\n";
}
void test_CreateTetrisLine()
{
cout << "Begin test_CreateTetrisLine():\n";
TetrisLine *ter = new TetrisLine();
cout << *ter << endl;
cout << "after rotate 90\n";
ter->rotate(90);
cout << *(TetrisBase*)(ter) << endl;
delete ter;
cout << "end test_CreateTetrisLine():\n\n";
}
void test_CreateTetrisSquare()
{
cout << "Begin test_CreateTetrisSquare():\n";
TetrisSquare *ter = new TetrisSquare();
cout << *ter << endl;
cout << "after rotate 90\n";
ter->rotate(90);
cout << *(TetrisBase*)(ter) << endl;
delete ter;
cout << "end test_CreateTetrisSquare():\n\n";
}
void test_memry_leak()
{
cout << "Begin test_memry_leak():\n";
for (int i = 0; i < 10000000; ++i)
{
TetrisSquare *ter = new TetrisSquare();
delete ter;
}
cout << "end test_memry_leak():\n\n";
}
void test_message()
{
cout << "Begin test_message():\n";
MessageBase *msg = new KeyboardMessage(MSG_KEY_DOWN, KEY_UP);
cout << "Msg type = " << msg->getMsg() << endl;
cout << "Handleding ……\n";
msg->setHandled();
cout << "Now state = " << msg->ifHandled() << endl;
delete msg;
cout << "end test_message():\n\n";
}
void test_handler()
{
cout << "Begin test_handler():\n";
HandlerBase *handler = new MoveTetrisHandler();
MessageBase *msg = new KeyboardMessage(MSG_KEY_DOWN, KEY_UP);
handler->BeginHandle(msg);
delete msg;
delete handler;
cout << "end test_handler():\n\n";
}
void test_state()
{
cout << "Begin test_state():\n";
HandlerBase * handler = new MoveTetrisHandler(); //移动方块的handler
HandlerBase * handler1 = new KeyDownHandler(); //键盘按下的消息处理handler
handler->setNextHandler(handler1); //创建责任链
handler1->setNextHandler(handler);
StateBase * state = new PlayingState(handler);
MessageBase *msg = new KeyboardMessage(MSG_KEY_DOWN, KEY_UP); //KeyboardMessage,按下键盘10
state->stateHandle(msg); //此状态下开始处理消息
delete msg;
delete state;
delete handler;
delete handler1;
cout << "end test_state():\n\n";
}
void test_GameFram()
{
cout << "Begin test_GameFram():\n";
GameFram * game = GameFram::getInstance();
cout << "end test_GameFram():\n\n";
}
void test_tetris_factory()
{
cout << "Begin test_tetris_factory():\n";
TetrisFactory * factory = TetrisFactory::getInstance();
TetrisBase * tetris = factory->CreateTetris(TYP_TETRIS_1);
cout << "My create tetris is \n";
cout << *tetris << endl;
cout << "Now destroy tetris \n";
factory->DestroyTetris(tetris);
cout << "end test_tetris_factory():\n\n";
}
void test_send_msg_to_game_fram()
{
cout << "Begin test_send_msg_to_game_fram():\n";
GameFram * game = GameFram::getInstance();
MessageBase* msg = new CommonMessage(MSG_CREATE_TETRIS);
game->PushMsg(msg);
delete msg;
msg = new CommonMessage(MSG_DEBUG_TRACE);
game->PushMsg(msg);
delete msg;
cout << "end test_send_msg_to_game_fram():\n\n";
}
void test_PinToMap()
{
cout << "Begin test_PinToMap():\n";
for (int i = 0; i < 5; ++i)
{
Tetris *ter = new Tetris((PDCTYPE)i);
cout << *(TetrisBase*)(ter) << endl;
GameMap *map = new GameMap(10,10);
ter->move();ter->move();ter->move();//ter->move();ter->move();
cout << ter->getPosition()<< endl;
ter->PinToMap(map);
cout << *(AggregateBase*)map << endl;
delete map;
delete ter;
}
cout << "end test_PinToMap():\n\n";
}
void test_move_in_map()
{
cout << "Begin test_move_in_map():\n";
GameFram * game = GameFram::getInstance();
MessageBase* msg_trace = new CommonMessage(MSG_DEBUG_TRACE);
MessageBase* msg = new CommonMessage(MSG_CREATE_TETRIS);
game->PushMsg(msg);
delete msg;
msg = new CommonMessage(MSG_UPDATE_GAME);
game->PushMsg(msg);
game->PushMsg(msg);
game->PushMsg(msg);
game->PushMsg(msg);
game->PushMsg(msg);
game->PushMsg(msg_trace);
MessageBase *msg1 = new KeyboardMessage(MSG_KEY_DOWN, KEY_RIGHT);
game->PushMsg(msg1);
delete msg1;
game->PushMsg(msg);
delete msg;
game->PushMsg(msg_trace);
delete msg_trace;
cout << "end test_move_in_map():\n\n";
}
void test_ill_move()
{
cout << "Begin test_ill_move():\n";
GameFram * game = GameFram::getInstance();
MessageBase* msg_trace = new CommonMessage(MSG_DEBUG_TRACE);
MessageBase* msg = new CommonMessage(MSG_CREATE_TETRIS);
game->PushMsg(msg);
delete msg;
msg = new CommonMessage(MSG_UPDATE_GAME);
game->PushMsg(msg);
game->PushMsg(msg);
game->PushMsg(msg);
game->PushMsg(msg);
game->PushMsg(msg);
game->PushMsg(msg_trace);
MessageBase *msg1 = new KeyboardMessage(MSG_KEY_DOWN, KEY_RIGHT);
game->PushMsg(msg1);
game->PushMsg(msg1);
game->PushMsg(msg1);
game->PushMsg(msg1);
game->PushMsg(msg1);
game->PushMsg(msg1);
game->PushMsg(msg1);
game->PushMsg(msg1);
game->PushMsg(msg1);
game->PushMsg(msg1); //一直往右边移动
delete msg1;
msg1 = new KeyboardMessage(MSG_KEY_DOWN, KEY_LEFT);
game->PushMsg(msg1);
game->PushMsg(msg1);
game->PushMsg(msg1);
game->PushMsg(msg1);
game->PushMsg(msg1);
game->PushMsg(msg1);
game->PushMsg(msg1);
game->PushMsg(msg1);
game->PushMsg(msg1);
game->PushMsg(msg1); //一直往左边移动
game->PushMsg(msg1);
game->PushMsg(msg1);
game->PushMsg(msg1);
delete msg1;
game->PushMsg(msg);
delete msg;
game->PushMsg(msg_trace);
delete msg_trace;
cout << "end test_ill_move():\n\n";
}
void test_down()
{
cout << "Begin test_down():\n";
GameFram * game = GameFram::getInstance();
MessageBase* msg_trace = new CommonMessage(MSG_DEBUG_TRACE);
MessageBase* msg = new CommonMessage(MSG_CREATE_TETRIS);
game->PushMsg(msg);
delete msg;
msg = new CommonMessage(MSG_UPDATE_GAME);
game->PushMsg(msg);
game->PushMsg(msg);
game->PushMsg(msg);
game->PushMsg(msg);
game->PushMsg(msg);
game->PushMsg(msg_trace);
MessageBase *msg1 = new KeyboardMessage(MSG_KEY_DOWN, KEY_RIGHT);
game->PushMsg(msg1);
game->PushMsg(msg1);
game->PushMsg(msg1);
game->PushMsg(msg1);
game->PushMsg(msg1);
game->PushMsg(msg1);
game->PushMsg(msg1);
game->PushMsg(msg1);
game->PushMsg(msg1);
game->PushMsg(msg1); //一直往右边移动
delete msg1;
msg1 = new KeyboardMessage(MSG_KEY_DOWN, KEY_LEFT);
game->PushMsg(msg1);
game->PushMsg(msg1);
game->PushMsg(msg1);
game->PushMsg(msg1);
game->PushMsg(msg1);
game->PushMsg(msg1);
game->PushMsg(msg1);
game->PushMsg(msg1);
game->PushMsg(msg1);
game->PushMsg(msg1); //一直往左边移动
game->PushMsg(msg1);
game->PushMsg(msg1);
game->PushMsg(msg1);
delete msg1;
for (int i = 0; i < 2500; ++i)
{
game->PushMsg(msg);
}
delete msg;
game->PushMsg(msg_trace);
delete msg_trace;
cout << "end test_down():\n\n";
}
void test_random_game()
{
cout << "Begin test_random_game():\n";
GameFram * game = GameFram::getInstance();
MessageBase* updateMsg = new CommonMessage(MSG_UPDATE_GAME);
MessageBase* traceMsg = new CommonMessage(MSG_DEBUG_TRACE);
game->PushMsg(updateMsg);
for (int i = 0; i < 10485; ++i)
{
int randNum = rand() % 10;
if (randNum >= 8)
{
game->PushMsg(updateMsg);
}else if (randNum <= 3)
{
MessageBase* keyMsg = new KeyboardMessage(MSG_KEY_DOWN, KEY_LEFT);
game->PushMsg(keyMsg);
delete keyMsg;
}else if (randNum > 7){
MessageBase* keyMsg = new KeyboardMessage(MSG_KEY_DOWN, KEY_UP);
game->PushMsg(keyMsg);
delete keyMsg;
}else
{
MessageBase* keyMsg = new KeyboardMessage(MSG_KEY_DOWN, KEY_RIGHT);
game->PushMsg(keyMsg);
delete keyMsg;
}
}
delete updateMsg;
game->PushMsg(traceMsg);
delete traceMsg;
cout << "end test_random_game():\n\n";
}
int main()
{
test_rotate();
test_otherTetris();
test_CreateTetrisLine();
test_CreateTetrisSquare();
//test_memry_leak();
test_message();
test_handler();
test_state();
test_GameFram();
test_tetris_factory();
test_send_msg_to_game_fram();
test_PinToMap();
//test_move_in_map();
//test_ill_move();
//test_down();
test_random_game();
return 0;
}