-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathHFlow.c
690 lines (607 loc) · 14.1 KB
/
HFlow.c
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
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
#include "HFlow.h"
void HFlow_doGame(HGame *game, HCard const *CARD_SET, bool wasLoaded)//전체게임함수
{
// Initialize Player Turn
Renderer_help();
if(!wasLoaded)
{
/*
Generate Card Deck
*/
while(true)
{
/*
Four Same-Month-Card Protection. (It'll never end)
*/
HGame_reset(game, CARD_SET);
bool four_card_duplication = false;
int same_counter = 1;
HCard const *prev_card = HDeck_get(game->display_cards, 0)->data;
HCard const *this_card = NULL;
for(int c=1; c<game->display_cards->size; ++c)
{
this_card = HDeck_get(game->display_cards, c)->data;
if(this_card->month == prev_card->month)
{
++same_counter;
if(same_counter == 4)
{
four_card_duplication = true;
break;
}
}
prev_card = this_card;
}
if(!four_card_duplication)
{
break;
}
}
/*
ABOUT CHONG-TONG
Chong-tong should exist only one.
If there are more than 2 chong-tong, game will be nagari.
To examine whether chong-tong or not, this iterate every cards they have.
*/
// Chong-Tong Examination
int pres_who = -1;
int pres_flag = HGame_isPres(game, &pres_who);
if(pres_flag == 1)
{
// Chong-Tong
game->player[pres_who]->score += 10;
HNotice_President(game->player[pres_who]);
HFlow_stop(game, pres_who, false);
HGame_reset(game, CARD_SET);
}
else if(pres_flag == -1)
{
// Nagari '~'
game->was_nagari = true;
HNotice_Nagari();
HGame_reset(game, CARD_SET);
}
}
int card_pointer = 0;
Renderer_game(game, card_pointer);
if(!wasLoaded)
{
/*
Shake Shake
*/
HGame_autoshake(game);
}
/*
MAIN GAME
*/
bool continue_game = true;
while(continue_game)
{
/*
FIRST TURN LOGIC
When loaded, first time is preserved turn.
But after that, first time is 0 Player's turn.
*/
int p;
if(wasLoaded)
{
p = game->current_player_num;
wasLoaded = false; // For next loop, this should be reset.
}
else
{
p = 0;
}
/*
PROCEEDING TURNS
*/
for( ; p<3 && continue_game; ++p)
{
game->current_player_num = p;
HPlayer *player = game->player[p];
card_pointer = 0;
Renderer_game(game, card_pointer);
//인터페이스 : e(exit), b(잔고), h(키설명),save(저장).load
bool isSelecting = true;
while(isSelecting)
{
// Get Key Input
Renderer_game(game, card_pointer);
int temp = HGUI_getch();
char temp_string[32];
switch(temp)
{
default:
// Number Input
if(1 <= temp-'0' && temp-'0' <= 7 && temp-'0' <= player->myDeck->size)
{
card_pointer = temp-'0'-1;
}
break;
case 'a':
// Move Selector Left
if(card_pointer > 0)
{
--card_pointer;
}
break;
case 'd':
// Move Selector Right
if(card_pointer < player->myDeck->size - 1)
{
++card_pointer;
}
break;
case '9':
// Change 9-Five as PPI <-> ANIM
Renderer_apChange(player);
break;
case '\n':
// Enter
isSelecting = false;
break;
case 'b':
// See the Player's Balance
Renderer_showBalance(game);
break;
case 'h':
// Help
Renderer_help();
break;
case 'e':
// Exit
continue_game = !Renderer_exit();
if(!continue_game)
{
isSelecting = false;
}
break;
case 's':
// Save
if(Renderer_save()) // If you want to save,
{
if(HFileIO_saveGame(game, CARD_SET)) // Save
{
Renderer_notice("Save Done!", CARD_HEIGHT); // Success
}
else
{
Renderer_notice("Save Fail!", CARD_HEIGHT); // Fail
}
}
break;
}
}
// Exit Game
if(!continue_game)
{
break;
}
// eat함수
/*
Check whether this player eat gwan or not
This because to determine Gwan-Back
*/
bool hasGwan = HFlow_eat(game, card_pointer);
// Pan-SSuel-E
if(game->display_cards->size == 0 && player->myDeck->size > 0)
{
HFlow_stealcard(game);
HNotice_FClr(player);
}
// 점수산출score함수
HGame_calcScore(game);
// End Gmae Logic
bool loop_cut = false;
switch(HFlow_isEnd(game))
{
case 2:
// Game End with Nagari
HNotice_Nagari();
HGame_reset(game, CARD_SET);
loop_cut = true;
break;
case 1:
// Game End with Winner
HNotice_Win(player);
if(HFlow_stop(game, p, hasGwan)) // Chekc if someone bankrupt or not
{
// Bankrupt
continue_game = false;
}
else
{
// Normal
HGame_reset(game, CARD_SET);
}
loop_cut = true;
break;
}
HGUI_cReset();
if(loop_cut)
{
break;
}
}
}
HGUI_cReset();
}
/*
ABOUT isEnd
This will determine whether game should be end or not.
There are three return value.
2 : Game End with Nagari
1 : Game End with Winner
0 : Game Continue
*/
int HFlow_isEnd(HGame *game)
{
//났나? - 났을 때 고 혹은 스톱
HPlayer *player = HGame_nowPlayer(game);
if(player->score >= 3)
{
/*
Several Cas
i) Last Loop
ii) Other Loop
*/
if(player->myDeck->size == 0)
{
/*
Last Loop. There are Several Cas
i) Can Go -> 100% WIN
ii) Can't Go -> 100% LOSE
*/
if(player->score > player->score_lastgo || player->how_many_go == 0)
{
/*
Can Go ( AUTO STOP )
*/
game->was_nagari = false;
return 1;
}
else
{
return 0;
}
}
else
{
/*
Not Last Loop. If you have enough quality, you can go.
*/
if(player->score > player->score_lastgo || player->how_many_go == 0)
{
if(Renderer_willGo())
{
// Go
player->how_many_go += 1;
player->score_lastgo = player->score;
return 0;
}
else
{
// Stop (or Auto Stop)
game->was_nagari = false;
return 1;
}
}
else
{
return 0;
}
}
}
else
{
/*
In this case, most of turn is normal.
But when last loop & last turn, Nagari will occured.
*/
if(player->myDeck->size == 0 && game->current_player_num == 2)
{
// Nagari '~'
game->was_nagari = true;
return 2;
}
else
{
return 0;
}
}
}
/*
Eating Card : Return value is whether you eat the Gwang or not.
card_pointer is number that player selected
*/
bool HFlow_eat(HGame *game, int card_pointer) //자기턴진행함수
{
// Selector
//먹을거 있니(1~7)-뭐먹을래(1~2)
HCard const *myCard = HDeck_get(HGame_nowPlayer(game)->myDeck, card_pointer)->data;
HCard const *topCard = game->unknown_cards->first->prev->data;
HDeck_remove(HGame_nowPlayer(game)->myDeck, card_pointer);
HDeck_pop(game->unknown_cards);
HDeck *whatYouEat = new_HDeck();
//까기
bool hasGwan = false;
if(myCard->month == topCard->month)
{
// New card from the Main Deck is same with myCard
HDeck *target = (game->visible_cards)[myCard->month-1];
switch(target->size)
{
case 0:
// There is no matching card on the floor
// ZOK
HPlayer_eat(HGame_nowPlayer(game), myCard);
HPlayer_eat(HGame_nowPlayer(game), topCard);
HDeck_push(whatYouEat, myCard);
HDeck_push(whatYouEat, topCard);
if(myCard->type == H_GWAN || topCard->type == H_GWAN)
{
hasGwan = true;
}
Renderer_notice("쪽 !", CARD_HEIGHT);
HFlow_stealcard(game);
break;
case 1:
// POO
HDeck_push(target, myCard);
HDeck_push(target, topCard);
Renderer_notice("쌌다 !", CARD_HEIGHT);
break;
case 2:
// 4장 먹기
HPlayer_eat(HGame_nowPlayer(game), myCard);
HPlayer_eat(HGame_nowPlayer(game), topCard);
HPlayer_eat(HGame_nowPlayer(game), HDeck_get(target, 0)->data);
HPlayer_eat(HGame_nowPlayer(game), HDeck_get(target, 1)->data);
HDeck_push(whatYouEat, myCard);
HDeck_push(whatYouEat, topCard);
HDeck_push(whatYouEat, HDeck_get(target, 0)->data);
HDeck_push(whatYouEat, HDeck_get(target, 1)->data);
// Gwang Processing
if(myCard->type == H_GWAN || topCard->type == H_GWAN ||
HDeck_get(target, 0)->data->type == H_GWAN ||
HDeck_get(target, 1)->data->type == H_GWAN)
{
hasGwan = true;
}
HDeck_clear(target);
Renderer_notice("따닥 !", CARD_HEIGHT);
HFlow_stealcard(game);
break;
}
}
else
{
for(int t=0; t<2; ++t)
{
// If it's not, you should take two possibilities.
HCard const *nowCard;
HDeck *target;
// Set Target
// 1. Your Card, 2. Top Card from Deck
if(t == 0)
{
nowCard = myCard;
target = (game->visible_cards)[myCard->month-1]; // Bcz of Horrible Length....
}
else
{
nowCard = topCard;
target = (game->visible_cards)[topCard->month-1];
}
int sel_num;
switch(target->size)
{
case 0:
// You Throw the Card
HDeck_push(target, nowCard);
break;
case 1:
// Match : You Can Eat This!
HPlayer_eat(HGame_nowPlayer(game), nowCard);
HPlayer_eat(HGame_nowPlayer(game), HDeck_get(target, 0)->data);
HDeck_push(whatYouEat, nowCard);
HDeck_push(whatYouEat, HDeck_get(target, 0)->data);
// Gwang Processing
if(nowCard->type == H_GWAN || HDeck_get(target, 0)->data->type == H_GWAN)
{
hasGwan = true;
}
HDeck_clear(target);
break;
case 2:
// You have to select the card
if(HCard_comp(HDeck_get(target, 0)->data, HDeck_get(target, 1)->data) == 0)
{
sel_num = 0;
}
else
{
sel_num = Renderer_eatw(target);
}
HPlayer_eat(HGame_nowPlayer(game), nowCard);
HPlayer_eat(HGame_nowPlayer(game), HDeck_get(target, sel_num)->data);
HDeck_push(whatYouEat, nowCard);
HDeck_push(whatYouEat, HDeck_get(target, sel_num)->data);
// Gwang Processing
if(nowCard->type == H_GWAN || HDeck_get(target, sel_num)->data->type == H_GWAN)
{
hasGwan = true;
}
HDeck_remove(target, sel_num);
break;
case 3:
// Eat the Poo!!!
for(int i=0; i<3; ++i)
{
// Eat Every Cards in the Deck
HPlayer_eat(HGame_nowPlayer(game), HDeck_get(target, i)->data);
HDeck_push(whatYouEat, HDeck_get(target, i)->data);
}
HPlayer_eat(HGame_nowPlayer(game), nowCard);
HDeck_push(whatYouEat, nowCard);
// Gwang Processing
if(myCard->type == H_GWAN || topCard->type == H_GWAN ||
HDeck_get(target, 0)->data->type == H_GWAN ||
HDeck_get(target, 1)->data->type == H_GWAN ||
HDeck_get(target, 2)->data->type == H_GWAN)
{
hasGwan = true;
}
HDeck_clear(target);
Renderer_notice("올 ㅋ", CARD_HEIGHT);
HFlow_stealcard(game);
break;
}
}
}
// Renderer
if(whatYouEat->size > 0)
{
HDeck_sort(whatYouEat);
Renderer_noticeCards(whatYouEat);
}
HGame_refresh(game);
delete_HDeck(whatYouEat);
return hasGwan;
}
/*
ABOUT STEAL CARD
When special events occured, you can grab someone's PPI.
This will search for "Single PPI" first.
If there is no Single PPI, it will try to grab "Double PPI".
*/
void HFlow_stealcard(HGame *game)//상대피 뺏어오기 함수
{
if(game->current_player_num < 0 || game->current_player_num >= 3)
{
#ifdef DEBUG
printError("HMain", "Error", "stealcard(HGame *, int)", "Wrong Player Number");
#endif
}
else
{
HPlayer *me = HGame_nowPlayer(game);
HPlayer *you = NULL;
for(int i=0; i<3; ++i)
{
// Player Shadow Set
you = (game->player)[i];
if(you != me)
{
//상대피에 피가 있나?
if(you->normDeck->size > 0)
{
//상대피에 쌍피만 있는가?
// Search for Double PPI
int target = 0;
for(int j=0; j<you->normDeck->size; ++j)
{
// Check All Norm Deck Card
if(!HDeck_get(you->normDeck, j)->data->isDouble)
{
// This part will search Non-Double Card.
// If there is no Non-Double, just pick one.
target = j;
break;
}
}
//쌍피만 있다면 쌍피를 가져온다
//쌍피만 있는게 아니라면 그냥 피를 가져온다
HDeck_drawFrom(me->normDeck, you->normDeck, target);
}
HDeck_sort(me->normDeck);
HDeck_sort(you->normDeck);
}
}
}
}
/*
ABOUT STOP
stop() will transform "Game Point" to "Money".
*/
bool HFlow_stop(HGame *game, int winner, bool hasGwan)
{
// Original Logic Idea by Jeong Min Gi
HPlayer *win_player = game->player[winner];
int money_base = WON_PER_POINT*win_player->score; // Default Prize
bool gameover = false;
/*
WINNER PART
*/
//player1이 났을 시 쓰리고, 포고, 흔들기를 고려해 점수를 곱하여 보낸다
// Processing Go
money_base += WON_PER_POINT*win_player->how_many_go;
if(win_player->how_many_go >= 3)
{
money_base *= (2 << (win_player->how_many_go-3));
}
// Processing Nagari;
if(game->was_nagari)
{
money_base *= 2;
}
// Processing Meong-Bak
if(win_player->animDeck->size >= 7)
{
money_base *= 2;
HNotice_MBak(win_player);
}
// Processing Player's Shakability
if(win_player->hasShake)
{
// If he shaked specific month, multiple 2
money_base *= 2;
}
/*
LOSER PART
Show me the money
*/
for(int p=0; p<3; ++p)
{
// Processing Private Transfer Rate
HPlayer *player = game->player[p];
int xRate = 1;
if(p != winner)
{
// Ppi-Bak
if(0 < player->normDeck->size && player->normDeck->size <= 5)
{
xRate *= 2;
HNotice_PBak(player);
}
// Gwang-Bak
if(hasGwan && player->gwanDeck->size == 0)
{
xRate *= 2;
HNotice_GBak(player);
}
/*
ABOUT MONEY TRANSFER
i) They have enough money -> Show me the money
ii) They don't have enough money -> Kick Out
*/
if(player->money > xRate*money_base)
{
// Can Afford to Pay
win_player->money += xRate*money_base;
player->money -= xRate*money_base;
HNotice_Lose(player, xRate*money_base);
}
else
{
// Cannot Afford to Pay
win_player->money += player->money; // Their all possesion
player->money = 0;
gameover = true;
HNotice_Bankrupt(player);
}
}
}
HGame_setTurn(game, winner);
return gameover; // Whether Bankrupt or not
}