-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconnect4ios.c
1130 lines (937 loc) · 22.7 KB
/
connect4ios.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
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <time.h>
/* Domain of Coordinates
*
*Those two constants specify the
*maximum value that can be applied
*to this game.
*
*The domain is:
*Any point at (0,0) to (6,5).
*
*The Coordinate System is shown below.
*/
#define MaxX 6
#define MaxY 5
//Player Flag
typedef enum
{
PLAYER_A = 1, // For User
PLAYER_B = 2 // For Computer
}PLAYER;
/* RoundState -- Basic Construction of the Game
*
*Data Members:
* Scene - The whole board. It will be constantly updated
* when the game is undergoing.
*
* NextMove - Because of the rules, positions that are available
* for the next move are restricted: consider the board
* as a top-open box, the only thing you can do is grabbing
* a ball and let it free drop to the bottom.
*
* CurrentPlayer - Specify which player is currently playing.
*
* Moves - Count the moves that have been made.
*/
/* Thorough Explanation of Scene
*
*A (MaxY+1) * (MaxX+1) two-dimensional board.
*For each block: -1 means forbidden (in next move)
* 0 means available and no chess in it
* 1 means occupied by player A (User)
* 2 means occupied by player B (Computer)
*
* 0 1 2 3 4 5 6
* +--+--+--+--+--+--+--+
*0| | | | | | | |
* +--+--+--+--+--+--+--+
*1| | | | | | | |
* +--+--+--+--+--+--+--+
*2| | | | | | | |
* +--+--+--+--+--+--+--+
*3| | | | | | | |
* +--+--+--+--+--+--+--+
*4| | | | | | | |
* +--+--+--+--+--+--+--+
*5| | | | | | | |
* +--+--+--+--+--+--+--+
*
*Initially, the bottom line should be set by 0, the other should be set by -1.
*/
typedef struct
{
int Scene[MaxY+1][MaxX+1];
int NextMove[MaxX+1][2];
PLAYER CurrentPlayer;
int Moves;
}RoundState;
/*Rating System -- Primary Evaluation
*
*Minimax Algorithm will rate moves as the
*reference of the primary evaluation. More
*meaningful values are WinPosition and
*LosePosition.
*
*Because the algorithm evaluates on the view
*of computer, WinPosition would be the best;
*since NeutralPosition is relatively
*meaningless, so it is set to the lowest.
*/
#define WinPosition 1000
#define LosePosition 0
#define NeutralPosition -1000
/* MaxDepth
*/
#define MaxDepth 8
/* Rating System -- Secondary Evaluation
*
*It will be used only when the Primary Evaluation
*NeutralPosition. The thorough explanation of
*it is stated at DetermineBestMove().
*/
int VictoryProbability[MaxX+1] = {0};
/* CalculateCoordinateY()
*
*This function is a helper function of DetermineBestMove().
*
*DetermineBestMove() only returns the x-coordinate of next
*move. So CalculateCoordinateY() can scan NextMove Matrix
*to find the y-coordinate. Because in each turn, the
*x-coordinate of next move is unique.
*/
int CalculateCoordinateY(RoundState state, int x)
{
int i;
for(i=0;i<=MaxX;i++)
{
if(state.NextMove[i][0] == x)
{
return state.NextMove[i][1];
}
}
return -1;
}
/* DisplayScene()
*
*Display the board on the screen.
*/
void DisplayScene(RoundState state)
{
int i,j;
int k,code;
for(k=0;k<=MaxX;k++)
{
printf(" %d",k);
}
printf("\n");
for(i=0;i<=MaxY;i++)
{
printf(" ");
for(k=0;k<=MaxX;k++)
{
printf("+---");
}
printf("+\n");
printf("%d",i);
for(j=0;j<=MaxX;j++)
{
code = state.Scene[i][j];
if(code == -1)
{
printf("| ");
}
else
{
printf("| %d ", state.Scene[i][j]);
}
}
printf("|\n");
}
printf(" ");
for(k=0;k<=MaxX;k++)
{
printf("+---");
}
printf("+\n");
for(k=0;k<=MaxX;k++)
{
printf(" %d",k);
}
printf("\n\n");
}
/* Direction Matrix
*
*Used by FindWinner().
*
*Each coordinate represents a direction vector. The whole matrix
*states 8 directions relative to a center point.
*/
const int Direction[8][2] = {{0,-1},{1,-1},{1,0},{1,1},{0,1},{-1,1},{-1,0},{-1,-1}};
/* GameInit()
*
*Preparations before the game starts.
*/
void GameInit(RoundState *state, PLAYER player)
{
int i,j;
// Initialize Scene Matrix
for(i=0;i<=MaxY;i++)
{
for(j=0;j<=MaxX;j++)
{
state->Scene[i][j] = -1;
}
}
// Modify the bottom line
for(j=0;j<=MaxX;j++)
{
state->Scene[MaxY][j] = 0;
}
// Initialize NextMove Matrix
for(i=0;i<=MaxX;i++)
{
state->NextMove[i][0] = i;
state->NextMove[i][1] = MaxY;
}
state->CurrentPlayer = player;
// Reset the global counter
state->Moves = 0;
// Initialize VictoryProbability Matrix
for(i=0;i<=MaxX;i++)
{
VictoryProbability[i] = 0;
}
// Initialize the seed
srand((unsigned int)time(NULL));
}
/* Opponent()
*
*UPDATE: Find the opponent of the player who is currently playing.
*/
PLAYER Opponent(PLAYER CurrentPlayer)
{
switch(CurrentPlayer)
{
case PLAYER_A:
return PLAYER_B;
case PLAYER_B:
return PLAYER_A;
}
}
/* CheckNextStep()
*
*UPDATE: For the consideration of efficiency, this function now
*only works for users in the main loop.
*/
bool CheckNextStep(RoundState state, int x, int y)
{
int i = 0;
if(y == -1)
return false;
for(i=0;i<=MaxX;i++)
{
if(state.NextMove[i][0] == -1)
{
continue;
}
else if(state.NextMove[i][0] == x && state.NextMove[i][1] == y)
{
return true;
}
}
return false;
}
/* MakeMove()
*Old SetNewChess()
*
*UPDATE: In order to consider the effeciency, MakeMove()
*will no longer check if the coordinate is in the correct range.
*/
void MakeMove(RoundState *state, int x, int y)
{
// Mark that position
state->Scene[y][x] = state->CurrentPlayer;
// Update the NextMove Matrix
if(y > 0)
{
state->Scene[y-1][x] = 0;
state->NextMove[x][0] = x;
state->NextMove[x][1] = y-1;
}
// If we reach the top, disable this column
else if(y == 0)
{
state->NextMove[x][0] = -1;
state->NextMove[x][1] = -1;
}
// Reverse the player
state->CurrentPlayer = Opponent(state->CurrentPlayer);
// Finish this turn
state->Moves++;
}
/* RetractMove()
*
*UPDATE: Retract one move, actually does the same job as MakeMove().
*
*UPDATE: In order to consider the effeciency, RetractMove()
*will also not check if the coordinate is in the correct range.
*So if it will be used by users, adding a function CheckRetraction()
*is necessary to make sure if this position is the top number(1 or 2)
*of this column.
*/
void RetractMove(RoundState *state, int x, int y)
{
// Reset this position
state->Scene[y][x] = 0;
// If there is any meaningless zero, reset it
if(y > 0)
{
state->Scene[y-1][x] = -1;
}
// Update the NextMove Matrix
// We do not need to check the y-coordinate now,
//because you cannot make a move outside anyway.
state->NextMove[x][0] = x;
state->NextMove[x][1] = y;
// Reverse the player
state->CurrentPlayer = Opponent(state->CurrentPlayer);
// Roll back this turn
state->Moves--;
}
/* FindWinner()
*
*UPDATE: new update of CheckWinner(), it is faster.
*/
int FindWinner(RoundState state)
{
int x,y,m,n;
int NextX,NextY;
int id;
bool pass;
if(state.Moves < 7)
{
return -1;
}
for(x=0;x<=MaxX;x++)
{
for(y=MaxY;y>=0;y--)
{
id = state.Scene[y][x];
if(id == 0 || id == -1)
{
//Instead of 'continue', we search the Scene vertically,
//based on the rule, it would be meaningless if we linear
//search a winner strand based on a reference of 0 or -1.
//Surprisingly, the values above 0 would always be -1.
break;
}
for(m=0;m<8;m++) // Direction
{
pass = true;
NextX = x;
NextY = y;
for(n=0;n<3;n++) // Number of points scanned forward
{
NextX += Direction[m][0];
NextY += Direction[m][1];
if((NextX<0 || NextX>MaxX) || (NextY<0 || NextY>MaxY))
{
//This strand is a dead strand, so it is not
//necessary to step forward along this direction.
pass = false;
break;
}
if(state.Scene[NextY][NextX] != id)
{
//This strand is a dead strand, so it is not
//necessary to step forward along this direction.
pass = false;
break;
}
}
if(pass) // All three points are passed
{
// Winner strand is detected, no need to continue on.
return id;
}
}
}
}
return -1;
}
/* Minimax Algorithm
*
*DetermineBestMove() and EvaluatePosition()
*
*The core is a recursion involved two functions. These 2 functions
*call each other to evaluate 'current' Scene.
*
*DetermineBestMove() will simulate what the Scene would look like
*next step as many as possible. How many scenario it can evaluate
*depends on the 'depth' value. Bsed on the experimental data, when
*depth = 10, it is goning to take more than a minute to think.
*
*Since it is a game, the winning and losing scenes might occur at
*any round, instead after all blocks are filled. So the number of
*all possiblities would be extremely hard to determine accurately.
*/
int EvaluatePosition(RoundState, int);
/* EvaluateBestMove()
*
*Entry point of the evaluation.
*
*Find a best move among those simulated scenes. how many final-round
*situations can be simulated depends on depth value.
*
*The concept is easy: EvaluateBestMove(), assisted with EvaluatePosition(),
*continuously plays(simulates) this game. When a result occurs(win/lose/draw),
*EvaluateBestMove() will catch a rating of this simulation, if the rating is
*benefit for computer/not benefit for user, and better than previous one, this
*simulation(move) will be reserved.
*/
int EvaluateBestMove(RoundState state, int *MoveRating, int depth)
{
int i;
int Move[2], BestMoveX;
int MaxRating = NeutralPosition - 1; // in order to be replaced at the first time
int Rating;
for(i=0; i<=MaxX; i++)
{
if(state.NextMove[i][0] == -1)
{
continue;
}
Move[0] = state.NextMove[i][0];
Move[1] = state.NextMove[i][1];
// Tactical Prediction Stage
// Virtually make a move
MakeMove(&state, Move[0], Move[1]);
// Evaluate this move
Rating = EvaluatePosition(state, depth + 1);
// Secondary Rating Mechanism
if(Rating == WinPosition)
{
VictoryProbability[i]++;
}
// Primary Rating Mechanism
if (Rating > MaxRating)
{
BestMoveX = Move[0];
MaxRating = Rating;
}
// Retract this move, that is why we call it 'virtual'
RetractMove(&state, Move[0], Move[1]);
}
// Rating of the CURRENT Move
//This is tricky: Each simulated step is actually made by different
//players. Since the rating is used for computer (PLAYER_B), so a
//good move for PLAYER_B is a bad move for PLAYER_A. Therefore when
//the recursion is rolling back, we need to reverse the evaluation
//result if the current player is switched to PLAYER_A.
//Which means that this algorithm is actually evaluating for both sides,
//but only the best move for computer (PLAYER_B) should be preserved and
//the rest should be eliminated properly.
*MoveRating = (state.CurrentPlayer == PLAYER_B)?(MaxRating):(-MaxRating);
/*
if(*MoveRating == NeutralPosition)
{
BestMoveX = FindMaxVP();
}
*/
//For this game only, we do not need to return a coordinate.
//Because at each turn, the x-coordinate is unique in NextMove,
//y-coordinate, however, is not.
return BestMoveX;
}
/* EvaluatePosition()
*
*This function is easier to understand: check if the simulation is
*over(hence the game is over). If so, grade this simulation; if not,
*come back to DetermineBestMove() to proceed the current simulation.
*/
int EvaluatePosition(RoundState state, int depth)
{
int rate;
int code = FindWinner(state);
if(code != -1 || depth >= MaxDepth)
{
//PossibleScenes++;
if(code == PLAYER_B)
{
return WinPosition;
}
else if(code == PLAYER_A)
{
return LosePosition;
}
else
{
return NeutralPosition;
}
}
// Game is undergoing, proceed the simulation
EvaluateBestMove(state, &rate, depth);
return rate;
}
/* DetermineBestMove()
*
*It is an external packer function to make a final
*decision through considering ratings from primary
*and secondary rating system.
*
*The concept is: because of the limitation of depth
*value, many scenes might not reveal a result. This
*time the primary evaluation will always return
*NeutralPosition. Then we need the secondary evaluation.
*
*VictoryProbability stores the total number of scenes of
*WinPosition for each point where you can make the move.
*A higher number stands for a higher chance you are going
*to win finally.
*/
int DetermineBestMove(RoundState state, int *MoveRating)
{
int BestX;
int i, max = -1;
BestX = EvaluateBestMove(state, MoveRating, 0);
if(*MoveRating == NeutralPosition)
{
// Find the maximum probability
for(i=0;i<=MaxX;i++)
{
if(VictoryProbability[i] > max)
{
max = VictoryProbability[i];
BestX = i;
}
}
}
// Clean up VictoryProbability for the next round.
for(i=0;i<=MaxX;i++)
{
VictoryProbability[i] = 0;
}
return BestX;
}
/* RandCreate()
*
*Create a random number within the domain [low, high).
*/
int RandCreate(int low, int high)
{
int val;
val = ((int)(rand()%(high - low)) + low);
return(val);
}
/* DummyPlayer()
*
*A dummy procedure to play against players.
*
*Dummy player uses a randomly picked coordinate to make a move.
*/
int DummyPlayer(RoundState *state)
{
int choice;
while(1)
{
// Why MaxX+1? See comments of RandCreate() and RoundState
choice = RandCreate(0, MaxX+1);
if(state->NextMove[choice][0] != -1)
{
break;
}
}
return choice;
//MakeMove(state, state->NextMove[choice][0], state->NextMove[choice][1]);
}
const char* Instruction1 =
"\nWelcome to Connect 4\n\n"
"The following instructions will teach you how to play this game.\n\n"
"First, There is a %d * %d board.\n";
const char* Instruction2 =
"You can see numbers along edges, when you make a move, you only "
"need to choose one number from those shown on the top or the bottom.\n"
"In this case, your input should be 0~%d.\n"
"Because where you make the next move is restricted. Those blocks that "
"contains '0' are open for the next move.\n"
"Give it a try, make a move.\n\n";
const char* Instruction3 =
"Nicely done! Now you can play, but before you get started. You have to "
"know how to win in this game.\n"
"Just like what the name tells you, you need to connect at least 4 chess "
"of yours along one of eight directions to win.\n"
"Now, try to connect 4 chess.\n\n";
const char* Instruction4 =
"Congratulations, you win!\n"
"This is just a demo procedure, of course. When you start a new game, you and "
"another player or computer take turns to make moves.\n"
"For the record, When you play with computer, '1' stands for you, '2' stands for "
"the computer.\n\n"
"Good luck and enjoy this game :)\n\n";
/* WaitForSpace and WaitForYesNo
*
*Get a restricted input from user for pauses.
*/
void WaitForSpace(char* hint)
{
char ch;
printf(hint);
while(1)
{
ch = ' ';//getch();
if(ch == ' ')
{
system("cls");
break;
}
}
}
bool WaitForYesNo(char* hint)
{
char ch;
bool bRet;
printf(hint);
while(1)
{
ch = 'n';//getch();
if(ch == 'y' || ch == 'Y')
{
bRet = true;
break;
}
if(ch == 'n' || ch == 'N')
{
bRet = false;
break;
}
}
return bRet;
}
/* DemoHelper
*
*A special move-making procedure, for simulating
*a real game scenario.
*
*In order to use this function properly, the member
*Moves in RoundState must be adjusted to 7 (or a value
*that is slightly higher that 7) to ensure FindWinner
*runs properly (for more references, see EnterInstruction).
*
*The reason is that in the demo procedure, '2 players' is
*not meaningful. So for each turn, the player will be
*locked at the same one (PLAYER_A or PLAYER_B). For more
*references, see FindWinner and MakeMove.
*/
void DemoHelper(RoundState *game)
{
int x,y;
while(1)
{
printf("Your move: ");
scanf("%d",&x);
y = CalculateCoordinateY(*game, x);
if(CheckNextStep(*game, x, y))
{
break;
}
printf("Illegal Input, try again.\n");
}
MakeMove(game, x, y);
// Lock the player, it is just a demo.
game->CurrentPlayer = PLAYER_A;
}
/* EnterInstruction()
*
*Guidence of instructions.
*/
void EnterInstruction(RoundState *game)
{
system("cls");
printf(Instruction1, MaxY+1, MaxX+1);
DisplayScene(*game);
WaitForSpace("\n\nPress SPACE to proceed.");
printf(Instruction2, MaxX);
DisplayScene(*game);
// Hack the move counter, because FindWinner() works only after the 7th move.
game->Moves = 7;
DemoHelper(game);
printf(Instruction3);
DisplayScene(*game);
while(FindWinner(*game) == -1)
{
DemoHelper(game);
DisplayScene(*game);
}
printf(Instruction4);
WaitForSpace("\n\nPress SPACE to finish.");
}
/* ModeHelper()
*
*Guidence of game modes.
*
*Easy Mode
* Computer randomly makes a move.
*
*Hard Mode
* Computer carefully makes a move.
*
*Hell Mode
* Computer carefully makes a move, meanwhile,
*users will not see any board update on the screen,
*but coordinates.
*
*2-player mode
* 2 users play the game.
*/
int ModeHelper()
{
char ch;
printf("\n\nChoose a mode to play:\n"
"1 -> Easy mode\n"
"2 -> Hard mode\n"
"3 -> Hell mode\n"
"4 -> 2-player mode\n"
"Enter your choice(1|2|3|4):");
while(1)
{
ch = '2';//getch();
switch(ch)
{
case '1':
return 1;
case '2':
return 2;
case '3':
return 3;
case '4':
return 4;
default:
break;
}
}
}
/* Guidance()
*
*A run-once function that satrts when this program just starts.
*/
void Guidance()
{
RoundState game;
bool bRules;
system("cls");
printf("Welcome to Connect 4\n\n");
bRules = WaitForYesNo("Would you like to know more about the rules?(y/n)");
if(bRules)
{
GameInit(&game,PLAYER_A);
EnterInstruction(&game);
}
}
/* GameMain_EasyMode()
*
*The game loop for Easy Mode.
*/
void GameMain_EasyMode(RoundState *state)
{
int x,y,rating;
DisplayScene(*state);
while((FindWinner(*state) == -1) && (state->Moves <= (MaxX+1)*(MaxY+1)))
{
switch(state->CurrentPlayer)
{
case PLAYER_A:
{
while(1)
{
printf("Your move: ");
//scanf("%d %d",&x,&y);
scanf("%d",&x);
y = CalculateCoordinateY(*state, x);
if(CheckNextStep(*state, x, y))
{
break;
}
printf("Illegal Input, try again.\n");
}
system("cls");
break;
}
case PLAYER_B:
{
x = DummyPlayer(state);
y = CalculateCoordinateY(*state, x);
system("cls");
printf("Computer makes a move (%d,%d).\n",x,y);
break;
}
}
MakeMove(state, x, y);
DisplayScene(*state);
}
return;
}
/* GameMain_HardMode()
*
*The game loop for Hard Mode.
*/
void GameMain_HardMode(RoundState *state)
{
int x,y,rating;
DisplayScene(*state);
while((FindWinner(*state) == -1) && (state->Moves <= (MaxX+1)*(MaxY+1)))
{
switch(state->CurrentPlayer)
{
case PLAYER_A:
{
while(1)
{
printf("Your move: ");
//scanf("%d %d",&x,&y);
scanf("%d",&x);
y = CalculateCoordinateY(*state, x);
if(CheckNextStep(*state, x, y))
{
break;
}
printf("Illegal Input, try again.\n");
}
system("cls");
break;
}
case PLAYER_B:
printf("Computer is thinking...");
x = DetermineBestMove(*state, &rating);
y = CalculateCoordinateY(*state, x);
system("cls");
printf("\nIt makes the move (%d, %d) (%d)\n",x,y,rating);
break;
}
MakeMove(state, x, y);
DisplayScene(*state);
}
return;
}
/* GameMain_HellMode()
*
*The game loop for Hell Mode.
*/
void GameMain_HellMode(RoundState *state)
{
int x,y,rating;
//DisplayScene(*state);
while((FindWinner(*state) == -1) && (state->Moves <= (MaxX+1)*(MaxY+1)))
{
switch(state->CurrentPlayer)
{
case PLAYER_A:
{
while(1)
{
printf("Your move: ");