-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.cpp
599 lines (567 loc) · 22 KB
/
game.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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
#include <iostream>
#include <iomanip>
#include <sstream>
#include <cmath>
#include <memory>
#include <vector>
#include <algorithm>
#include <random>
#include <chrono>
#include "common.h"
#include "cross_socket.h"
#include "cross_library.h"
#include "WELLRNG512Gen.h"
#include "player_information.h"
#include "puyo.h"
#include "board.h"
#include "game.h"
void Game::DrawFrame(int offset_x, int offset_y) {
std::cout << Console::green;
std::cout << Console::GotoXY(X(0 + offset_x), Y(0 + offset_y));
std::cout << "┌── ───┐┌───┐";
for (int i = 1; i < 13; i++) {
std::cout << Console::GotoXY(X( 0 + offset_x), Y(i + offset_y)) << "│";
std::cout << Console::GotoXY(X( 7 + offset_x), Y(i + offset_y)) << "││";
std::cout << Console::GotoXY(X(12 + offset_x), Y(i + offset_y)) << "│";
}
std::cout << Console::GotoXY(X(0 + offset_x), Y(13 + offset_y));
std::cout << "└──────┘└───┘";
std::cout << Console::GotoXY(X( 9 + offset_x), Y( 1 + offset_y)) << " NEXT ";
std::cout << Console::GotoXY(X( 9 + offset_x), Y( 2 + offset_y)) << " " << Console::GotoXY(X(11 + offset_x), Y( 2 + offset_y)) << " ";
std::cout << Console::GotoXY(X( 9 + offset_x), Y( 3 + offset_y)) << " " << Console::GotoXY(X(11 + offset_x), Y( 3 + offset_y)) << " ";
std::cout << Console::GotoXY(X( 9 + offset_x), Y( 4 + offset_y)) << " Chain";
std::cout << Console::GotoXY(X( 9 + offset_x), Y( 6 + offset_y)) << "MaxChn";
std::cout << Console::GotoXY(X( 9 + offset_x), Y( 8 + offset_y)) << " Score";
std::cout << Console::GotoXY(X( 9 + offset_x), Y(10 + offset_y)) << " BGM ";
std::cout << Console::GotoXY(X( 9 + offset_x), Y(11 + offset_y)) << "ObsNum";
/*
0 1
0 1 2 3 4 5 6 7 8 9 0 1 2
00 ┌── ───┐┌───┐
1 │ ││ NEXT │
2 │ ││ │
3 │ ││ │
4 │ ││ Chain│
5 │ ││ │
6 │ ││MaxChn│
7 │ ││ │
8 │ ││ Score│
9 │ ││ │
10 │ ││BGM │
1 │ ││ObsNum│
2 │ ││ │
3 └──────┘└───┘
*/
}
void Game::DrawBoard(const std::shared_ptr<Board> &board, int offset_x, int offset_y) {
board->Draw(1 + offset_x, 1 + offset_y);
}
void Game::DrawNextBiPuyo(const std::shared_ptr<BiPuyo> &bipuyo, int offset_x, int offset_y) {
bipuyo->Draw(10 + offset_x, 3 + offset_y);
}
void Game::DrawPlayerInformation(const PlayerInformation &info, int offset_x, int offset_y) {
std::cout << Console::white;
std::cout << Console::GotoXY(X(9 + offset_x), Y(5 + offset_y)) << std::right << std::setw(6) << info.chain_number;
std::cout << Console::GotoXY(X(9 + offset_x), Y(7 + offset_y)) << std::right << std::setw(6) << info.max_chain_number;
std::cout << Console::GotoXY(X(9 + offset_x), Y(9 + offset_y)) << std::right << std::setw(6) << info.score;
std::cout << Console::GotoXY(X(9 + offset_x), Y(12 + offset_y)) << std::right << std::setw(6) << info.obstacle_number_taken;
}
void Game::Draw(const std::shared_ptr<Board> &board, const std::shared_ptr<BiPuyo> &bipuyo, const PlayerInformation &info, int offset_x, int offset_y) {
Game::DrawFrame(offset_x, offset_y);
Game::DrawBoard(board, offset_x, offset_y);
Game::DrawNextBiPuyo(bipuyo, offset_x, offset_y);
Game::DrawPlayerInformation(info, offset_x, offset_y);
}
bool SingleGame::GameInit() {
Console::ScreenClear();
std::cout << Console::GotoXY(X(0), Y(0));
// prepare game
my_bipuyo_generator = std::make_shared<BiPuyoGenerator>(); // single game doesn't need sync seed
my_board = std::make_shared<Board>();
my_board->SetNextBiPuyo(my_bipuyo_generator->GenerateBipuyo());
my_next_bipuyo = my_bipuyo_generator->GenerateBipuyo();
std::cout << "Loading..." << std::endl;
Console::Sleep(500);
return true;
}
void SingleGame::GameLoop() {
while (!gameover) {
if (!my_board->IsBusy()) {
if (kbhit()) {
int input = getch();
bool special = false;
if (input == 224) {
input = getch();
special = true;
}
if (special) {
if (input == 75) {
my_board->MoveLeft();
}
if (input == 77) {
my_board->MoveRight();
}
if (input == 80) {
my_board->MoveDown();
}
} else {
if (input == 'j' || input == 'h') {
my_board->MoveLeft();
}
if (input == 'l') {
my_board->MoveRight();
}
if (input == 'k') {
my_board->MoveDown();
}
if (input == '-') {
break;
}
if (input == 'z') { // rotate
my_board->RotateCCW();
}
}
}
}
else {
if (kbhit()) getch();
}
// input
my_board->Update();
my_board->UpdatePlayerInformation(my_info);
if (my_board->IsNeedNext()) {
// need next bipuyo
my_board->SetNextBiPuyo(my_next_bipuyo);
// set next bipuyo
my_next_bipuyo = my_bipuyo_generator->GenerateBipuyo();
}
gameover = my_board->IsGameOver();
// process
Draw();
// draw
Console::Sleep(16); // ?
}
}
void SingleGame::GameResult() {
while (kbhit()) getch();
Console::ScreenClear();
std::cout << Console::white << Console::GotoXY(0, 0)
<< "게임 끝" << std::endl
<< "점수: " << my_info.score << std::endl
<< "최대 연쇄: " << my_info.max_chain_number << std::endl
<< "메인으로 돌아가려면 아무키나 누르세요" << std::endl;
getch();
}
void SingleGame::Draw() {
int offset_x = 0;
int offset_y = 0;
/*
0 1
0 1 2 3 4 5 6 7 8 9 0 1 2
00 ┌── ───┐┌───┐
1 │ ││ NEXT │
2 │ ││ │
3 │ ││ │
4 │ ││ Chain│
5 │ ││ │
6 │ ││MaxChn│
7 │ ││ │
8 │ ││ Score│
9 │ ││ │
10 │ ││BGM │
1 │ ││ObsNum│
2 │ ││ │
3 └──────┘└───┘
*/
Game::Draw(my_board, my_next_bipuyo, my_info, offset_x, offset_y);
}
bool VSGame::GameInit() {
Console::ScreenClear();
std::cout << Console::GotoXY(X(0), Y(0));
// prepare game
int seed = time(NULL);
my_bipuyo_generator = std::make_shared<BiPuyoGenerator>(seed);
my_board = std::make_shared<Board>();
my_board->SetNextBiPuyo(my_bipuyo_generator->GenerateBipuyo());
my_next_bipuyo = my_bipuyo_generator->GenerateBipuyo();
other_bipuyo_generator = std::make_shared<BiPuyoGenerator>(seed);
other_board = std::make_shared<Board>();
other_board->SetNextBiPuyo(other_bipuyo_generator->GenerateBipuyo());
other_next_bipuyo = other_bipuyo_generator->GenerateBipuyo();
std::cout << "Loading..." << std::endl;
Console::Sleep(500);
return true;
}
void VSGame::GameLoop() {
while (!gameover) {
if (kbhit()) {
int input = getch();
bool special = false;
if (input == 224) {
input = getch();
special = true;
}
if (!my_board->IsBusy()) {
if (special) { }
else {
if (input == 'd') {
my_board->MoveLeft();
}
if (input == 'g') {
my_board->MoveRight();
}
if (input == 'f') {
my_board->MoveDown();
}
if (input == 'z') { // rotate
my_board->RotateCCW();
}
}
}
if (!other_board->IsBusy()) {
if (special) { }
else {
if (input == 'l') {
other_board->MoveLeft();
}
if (input == '\'') {
other_board->MoveRight();
}
if (input == ';') {
other_board->MoveDown();
}
if (input == 'm') {
other_board->RotateCCW();
}
}
}
}
// input
my_board->Update();
my_board->UpdatePlayerInformation(my_info);
if (my_board->IsNeedNext()) {
my_board->SetNextBiPuyo(my_next_bipuyo);
my_next_bipuyo = my_bipuyo_generator->GenerateBipuyo();
}
other_board->TakeObstacles(my_board->SendObstacles());
// effect please
other_board->Update();
other_board->UpdatePlayerInformation(other_info);
if (other_board->IsNeedNext()) {
other_board->SetNextBiPuyo(other_next_bipuyo);
other_next_bipuyo = other_bipuyo_generator->GenerateBipuyo();
}
my_board->TakeObstacles(other_board->SendObstacles());
gameover = my_board->IsGameOver() || other_board->IsGameOver();
// process
Draw();
Console::Sleep(16);
}
}
void VSGame::GameResult() {
while (kbhit()) getch();
Console::ScreenClear();
std::cout << Console::white << Console::GotoXY(0, 0);
if (my_board->IsGameOver()) {
std::cout << "1p가 이겼습니다." << std::endl;
}
else {
std::cout << "2p가 이겼습니다." << std::endl;
}
std::cout
<< "1p 점수: " << my_info.score << std::endl
<< "1p 최대 연쇄: " << my_info.max_chain_number << std::endl
<< "2p 점수: " << other_info.score << std::endl
<< "2p 최대 연쇄: " << other_info.max_chain_number << std::endl
<< "메인으로 돌아가려면 아무키나 누르세요" << std::endl;
getch();
}
void VSGame::Draw() {
/*
0 1 2
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6
00 ┌── ───┐┌───┐ ┌── ───┐┌───┐
1 │ ││ NEXT │ │ ││ NEXT │
2 │ ││ │ │ ││ │
3 │ ││ │ │ ││ │
4 │ ││ │ │ ││ │
5 │ ││ Chain│ │ ││ Chain│
6 │ ││ │ │ ││ │
7 │ ││MaxChn│ │ ││MaxChn│
8 │ ││ │ │ ││ │
9 │ ││ Score│ │ ││ Score│
10 │ ││ │ │ ││ │
1 │ ││BGM │ │ ││BGM │
2 │ ││ObsNum│ │ ││ObsNum│
3 │ ││ │ │ ││ │
4 └──────┘└───┘ └──────┘└───┘
*/
Game::Draw(my_board, my_next_bipuyo, my_info);
for (int i = 0; i <= 14; i++) {
std::cout << Console::GotoXY(X(13), Y(i)) << " ";
}
Game::Draw(other_board, other_next_bipuyo, other_info, 14);
}
bool VSRemoteGame::GameInit() {
int input;
Console::ScreenClear();
std::cout << Console::GotoXY(X(0), X(0));
std::cout << "하실 것을 골라주세요" << std::endl;
std::cout << "0. 돌아가기" << std::endl;
std::cout << "1. 서버" << std::endl;
std::cout << "2. 클라" << std::endl;
input = getch();
switch (input) {
case '0': return false;
case '1': break;
case '2': break;
default: return false;
}
Console::ScreenClear();
// 서버 인지 클라 인지
int seed;
if (input == '1') {
socket = std::make_shared<ServerSocket>();
auto server_socket = std::dynamic_pointer_cast<ServerSocket>(socket);
// 서버 일 경우 연결을 만들고 자기 아이피 주소를 보여줌
std::cout << Console::white
<< "안녕하세요. 당신은 서버 역할입니다." << std::endl
<< "클라이언트 역할의 분께 당신의 IP를 알려드려야 합니다." << std::endl
<< "혹시 공유기를 사용하시고 계시다면 포트 포워딩이 필요합니다." << std::endl
<< "ip는 http://www.myipaddress.com/show-my-ip-address/ 나" << std::endl
<< "shell 에서 (linux, MaxOS) ifconfig 명령어, 혹은 (Windows) ipconfig 명령어 를 사용하시면 됩니다." << std::endl
<< "포트는 9595를 사용하므로 열려있으면 닫아주세요" << std::endl;
if (server_socket->Bind("0.0.0.0", 9595)) {
std::cout << Console::red
<< "9595 포트에 Bind 실패했습니다. 이미 사용 중인지 확인해주세요." << std::endl
<< "3초 뒤 화면으로 돌아갑니다." << std::endl;
Console::Sleep(3000);
return false;
}
if (server_socket->Listen(5)) {
std::cout << Console::red
<< "Listen 실패했습니다." << std::endl
<< "3초 뒤 화면으로 돌아갑니다." << std::endl;
Console::Sleep(3000);
return false;
}
if (server_socket->Accept()) {
std::cout << Console::red
<< "Accept 실패했습니다." << std::endl
<< "3초 뒤 화면으로 돌아갑니다." << std::endl;
Console::Sleep(3000);
return false;
}
int generated_seed = rand();
server_socket->Send(std::to_string(generated_seed));
seed = generated_seed;
}
else {
socket = std::make_shared<ClientSocket>();
auto client_socket = std::dynamic_pointer_cast<ClientSocket>(socket);
// 클라일 경우 IP 주소 입력받게 함
std::string ip_address;
std::cout << Console::white
<< "안녕하세요. 당신은 클라이언트 역할입니다." << std::endl
<< "서버 역할의 분께서 당신에게 IP를 알려드릴 겁니다." << std::endl
<< "그것을 입력해주시기 바랍니다." << std::endl
<< "예시) 127.0.0.1" << std::endl;
std::cin >> ip_address;
if (client_socket->Connect(ip_address, 9595)) {
std::cout << Console::red
<< "Connect 실패했습니다. 올바른 IP인지 확인해주세요." << std::endl
<< "3초 뒤 화면으로 돌아갑니다." << std::endl;
Console::Sleep(3000);
return false;
}
seed = std::stoi(client_socket->Recv());
}
my_bipuyo_generator = std::make_shared<BiPuyoGenerator>(seed);
my_board = std::make_shared<Board>();
my_board->SetNextBiPuyo(my_bipuyo_generator->GenerateBipuyo());
my_next_bipuyo = my_bipuyo_generator->GenerateBipuyo();
other_board = std::make_shared<Board>();
other_board->SetNextBiPuyo(BiPuyoGenerator::GenerateEmptyBipuyo());
other_next_bipuyo = BiPuyoGenerator::GenerateEmptyBipuyo();
Console::ScreenClear();
std::cout << Console::GotoXY(X(0), X(0));
std::cout << "Loading..." << std::endl;
Console::Sleep(500);
return true;
}
void VSRemoteGame::GameLoop() {
while (!gameover) {
auto start = std::chrono::system_clock::now();
if (socket->Send(this->Serialize()) < 0) {
std::cout << Console::red << Console::GotoXY(0, 0) << "통신 에러";
Console::Sleep(1000);
break;
}
try {
auto response = socket->Recv();
if (response.compare("end")) {
this->Deserialize(response);
}
else {
break;
}
}
catch (int error) {
std::cout << Console::red << Console::GotoXY(0, 0) << "통신 에러";
Console::Sleep(1000);
break;
}
if (!my_board->IsBusy()) {
if (kbhit()) {
int input = getch();
bool special = false;
if (input == 224) {
input = getch();
special = true;
}
if (special) {
if (input == 75) {
my_board->MoveLeft();
}
if (input == 77) {
my_board->MoveRight();
}
if (input == 80) {
my_board->MoveDown();
}
} else {
if (input == 'j' || input == 'h') {
my_board->MoveLeft();
}
if (input == 'l') {
my_board->MoveRight();
}
if (input == 'k') {
my_board->MoveDown();
}
if (input == '-') {
break;
}
if (input == 'z') { // rotate
my_board->RotateCCW();
}
}
}
}
else {
if (kbhit()) getch();
}
// input
my_board->TakeObstacles(obstacle_to_take);
my_board->Update();
my_board->UpdatePlayerInformation(my_info);
if (my_board->IsNeedNext()) {
my_board->SetNextBiPuyo(my_next_bipuyo);
my_next_bipuyo = my_bipuyo_generator->GenerateBipuyo();
}
obstacle_to_send = my_board->SendObstacles();
gameover = my_board->IsGameOver() || other_board->IsGameOver();
// process
Draw();
auto end = std::chrono::system_clock::now();
auto delta = (std::chrono::duration_cast<std::chrono::milliseconds>(end - start)).count();
int tick = delta > 16 ? 0 : 16 - delta;
std::cout << Console::GotoXY(0, 19) << Console::red << " ";
std::cout << Console::GotoXY(0, 19) << Console::red << "FPS: " << 1000 / (delta > 16 ? delta : 16);
Console::Sleep(tick);
}
socket->Send("end");
Console::Sleep(50); // 왜 필요할까
socket->Close();
}
void VSRemoteGame::GameResult() {
while (kbhit()) getch();
Console::ScreenClear();
std::cout << Console::white << Console::GotoXY(0, 0);
if (my_board->IsGameOver()) {
std::cout << "졌습니다..." << std::endl;
}
else {
std::cout << "이겼습니다!" << std::endl;
}
std::cout
<< "내 점수: " << my_info.score << std::endl
<< "내 최대 연쇄: " << my_info.max_chain_number << std::endl
<< "상대 점수: " << other_info.score << std::endl
<< "상대 최대 연쇄: " << other_info.max_chain_number << std::endl
<< "메인으로 돌아가려면 아무키나 누르세요" << std::endl;
getch();
}
void VSRemoteGame::Draw() {
VSGame::Draw();
}
std::string VSRemoteGame::Serialize() {
std::ostringstream data;
data
<< "G{"
<< "P{" << my_info.Serialize() << "}"
<< "M{" << my_board->Serialize() << "}"
<< "B{" << my_next_bipuyo->Serialize() << "}"
<< "O{" << obstacle_to_send << "}"
<< "}";
return data.str();
}
void VSRemoteGame::Deserialize(std::string data) {
int state = 0;
std::ostringstream buffer;
for (int i = 0; i < data.length(); i++) {
char token = data.at(i);
switch (state) {
case 0: {
if (token == 'G') continue;
if (token == '{') state = 1;
} break;
case 1: {
if (token == 'P') continue;
if (token == '{') continue;
if (token == '}') {
other_info.Deserialize(buffer.str());
buffer.str("");
state = 2;
continue;
}
buffer << token;
} break;
case 2: {
if (token == 'M') continue;
if (token == '{') continue;
if (token == '}') {
other_board->Deserialize(buffer.str());
buffer.str("");
state = 3;
continue;
}
buffer << token;
} break;
case 3: {
if (token == 'B') continue;
if (token == '{') continue;
if (token == '}') {
other_next_bipuyo->Deserialize(buffer.str());
buffer.str("");
state = 4;
continue;
}
buffer << token;
} break;
case 4: {
if (token == 'O') continue;
if (token == '{') continue;
if (token == '}') {
obstacle_to_take = std::stoi(buffer.str());
buffer.str("");
state = 5;
}
buffer << token;
} break;
case 5: {
continue;
} break;
}
}
}