-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcore.c
More file actions
184 lines (163 loc) · 4.7 KB
/
core.c
File metadata and controls
184 lines (163 loc) · 4.7 KB
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
#include "game.h"
#include "ai.h"
#include "settings.h"
extern settings gameSet;
extern stat gameStat;
extern stat currentStat;
/* Initializes the board to the initial state by setting the entire board to empty*/
void initializeBoard(void){
int i;
for ( i = 0; i<BOARD; i++ )
gameBoard.row[i] = ' ';
}
/* Checks if the game has ended or not */
int hasEnded( void ){
int i;
/*Checking if the game has ended in any of the row*/
for ( i = 0; i<7 ; i+= 3 ){
/*If one of cell of the row is empty , the game has not ended in that row*/
if ( gameBoard.row[i+1] == ' ')
continue;
/* If all the values in the row are equal , The game has ended */
if ( gameBoard.row[i] == gameBoard.row[i+1] && gameBoard.row[i+1] == gameBoard.row[i+2] ){
return (gameBoard.row[i+1] == player1.sign)?PLAYER1:PLAYER2;
}
}
/*Checking if the game has ended in any of the column*/
for ( i = 0; i<3 ; i++ ){
if ( gameBoard.row[i+3] == ' ')
continue;
if ( gameBoard.row[i] == gameBoard.row[i+3] && gameBoard.row[i+3] == gameBoard.row[i+6] )
return (gameBoard.row[i+3] == player1.sign)?PLAYER1:PLAYER2;
}
/*Checking if the game has ended in any of the diagonals*/
if ( gameBoard.row[4] != ' '){ // If the center square is empty then game has not ended in any of diagonals
if ( gameBoard.row[0] == gameBoard.row[4] && gameBoard.row[4] == gameBoard.row[8] )
return (gameBoard.row[4] == player1.sign)?PLAYER1:PLAYER2;
if ( gameBoard.row[2] == gameBoard.row[4] && gameBoard.row[4] == gameBoard.row[6] )
return (gameBoard.row[4] == player1.sign)?PLAYER1:PLAYER2;
}
/* If the game has not ended in any of the rows columns or diagonals and there is still a empty box, It has not ended*/
for ( i =0 ; i<9; i++ ){
if ( gameBoard.row[i] == ' ')
return FALSE;
}
//If there is no empty cell
return TIE;
}
/* "Performs" the move of the player 1 or player2 by changing the values in the game board
x is the cell in which the symbol is to be placed
player is the player whose symbol is to be places ( Player 1 or Player 2)*/
void performMove( int x , int player ){
gameBoard.row[x] = ( player == PLAYER1 )?player1.sign:player2.sign;
}
/* Checks if the move the user is trying to do is legal or not */
int isLegal( int x ){
/* If the character is not valid or the cell the user is trying to fill is not Empty*/
if ( x == -1 || gameBoard.row[x] != ' ')
return FALSE;
else
return TRUE;
}
void playGame(int mode){
int winner;
initializeBoard();
printBoard();
if ( mode == SINGLE ){
player1 = (gameStat.gcSingle%2)?gameSet.ai:gameSet.p1;
player2 = (gameStat.gcSingle%2)?gameSet.p1:gameSet.ai;
player1.position = PLAYER1;
player2.position = PLAYER2;
winner = playSingle();
if ( winner == PLAYER1 ){
if ( player1.type == AI ){
gameStat.ai.win++;
currentStat.ai.win++;
gameStat.p1.lose++;
currentStat.p1.lose++;
}
else{
currentStat.p1.win++;
gameStat.p1.win++;
currentStat.ai.lose++;
gameStat.ai.lose++;
}
} else if ( winner == PLAYER2 ){
if ( player2.type == AI ){
currentStat.ai.win++;
gameStat.ai.win++;
currentStat.p1.lose++;
gameStat.p1.lose++;
}
else{
currentStat.ai.lose++;
gameStat.ai.lose++;
currentStat.p1.win++;
gameStat.p1.win++;
}
}
} else {
player1 = (gameStat.gcDouble%2)?gameSet.p2:gameSet.p1;
player2 = (gameStat.gcDouble%2)?gameSet.p1:gameSet.p2;
player1.position = PLAYER1;
player2.position = PLAYER2;
winner = playDouble();
if ( winner == PLAYER1 ){
currentStat.p1.win++;
gameStat.p1.win++;
currentStat.p2.lose++;
gameStat.p2.lose++;
}
else {
currentStat.p2.win++;
gameStat.p2.win++;
currentStat.p1.lose++;
gameStat.p1.lose++;
}
}
if ( winner == PLAYER1 ){
printf("%s wins!!!\n",player1.name);
} else if (winner == PLAYER2) {
printf("%s wins!!!\n",player2.name);
} else {
printf("The game has been drawn\n");
}
writeSettings();
}
int playSingle( void ){
player current;
current = player1;
ai = (player1.type == AI )?player1:player2;
human = (player1.type == HUMAN )?player1:player2;
int cell,rv;
setDepth();
while ( !(rv=hasEnded()) ){
if ( current.type == AI )
cell = aiMove();
else {
printf("%s's turn: ", current.name);
cell = getMove();
}
performMove(cell, current.position);
current = ( current.position == PLAYER1 )?player2:player1;
printBoard();
}
gameStat.gcSingle++;
currentStat.gcSingle++;
return rv;
}
int playDouble( void ){
player current;
int cell,rv;
current = player1;
while ( !(rv=hasEnded()) ){
printf("%s's turn: ",current.name);
cell = getMove();
performMove(cell,current.position);
current = ( current.position == PLAYER1 )?player2:player1;
printBoard();
}
gameStat.gcDouble++;
currentStat.gcDouble++;
return rv;
}