-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
370 lines (326 loc) · 9.62 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
#include <SDL/SDL.h>
#include <SDL/SDL_ttf.h>
#include <SDL/SDL_image.h>
#include <string>
#include <stdlib.h>
#include <sstream>
//Screen attributes
const int SCREEN_WIDTH = 300;
const int SCREEN_HEIGHT = 300;
const int SCREEN_BPP = 32;
//The surfaces
SDL_Surface *gameBoard = NULL;
SDL_Surface *screen = NULL;
SDL_Surface *cross = NULL;
SDL_Surface *circle = NULL;
//The event structure
SDL_Event event;
//The font in use
TTF_Font *font;
void apply_surface( int x, int y, SDL_Surface* source, SDL_Surface* destination, SDL_Rect* clip = NULL )
{
//Holds offsets
SDL_Rect offset;
//Get offsets
offset.x = x;
offset.y = y;
//Blit
SDL_BlitSurface( source, clip, destination, &offset );
}
bool init()
{
//Initialize all SDL subsystems
if( SDL_Init( SDL_INIT_EVERYTHING ) == -1 )
{
return false;
}
//Set up the screen
screen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE );
//If there was an error in setting up the screen
if( screen == NULL )
{
return false;
}
//Initialize SDL_ttf
if(TTF_Init() == -1)
{
return false;
}
//Set the window caption
SDL_WM_SetCaption( "Tic Tac Toe", NULL );
//If everything initialized fine
return true;
}
SDL_Surface *load_image( std::string filename )
{
//The image that's loaded
SDL_Surface* loadedImage = NULL;
//The optimized surface that will be used
SDL_Surface* optimizedImage = NULL;
//Load the image
loadedImage = IMG_Load( filename.c_str() );
//If the image loaded
if( loadedImage != NULL )
{
//Create an optimized surface from png file with alpha blending
optimizedImage = SDL_DisplayFormatAlpha( loadedImage );
//Free the old surface
SDL_FreeSurface( loadedImage );
//If the surface was optimized
if( optimizedImage != NULL )
{
//Color key surface
SDL_SetColorKey( optimizedImage, SDL_SRCCOLORKEY, SDL_MapRGB( optimizedImage->format, 255, 255, 255 ) );
}
}
//Return the optimized surface
return optimizedImage;
}
bool load_files()
{
gameBoard = load_image( "assets/gameboard.png" );
cross = load_image("assets/x.png");
circle = load_image("assets/circle.png");
if(gameBoard == NULL)
{
return false;
}
if(cross == NULL)
{
return false;
}
if(circle == NULL)
{
return false;
}
//If everything loaded fine
return true;
}
void clean_up()
{
//Free the surface
SDL_FreeSurface(screen);
SDL_FreeSurface(gameBoard);
SDL_FreeSurface(cross);
//Quit SDL
SDL_Quit();
}
int main( int argc, char* args[] )
{
int turn = 1;
int x = 0;
int y = 0;
int i = 0;
int sum = 0;
int turnCount = 0;
int gameBoardMatrix[9]= {0,0,0,0,0,0,0,0,0};
bool appplyOrNot = false;
bool quit = false;
bool gameWon = false;
if( init() == false)
{
return 1;
}
if(load_files() == false)
{
return 1;
}
apply_surface(0, 0, gameBoard, screen );
while(quit == false )
{
while(SDL_PollEvent(&event))
{
if(event.type == SDL_MOUSEBUTTONDOWN)
{
if(event.button.button == SDL_BUTTON_LEFT)
{
x = event.button.x;
y = event.button.y;
turnCount++;
//Check if square clicked is vacant then mark it accordingly X or O
if(x/100 == 0 && y/100 == 0 && gameBoardMatrix[0]== 0)
{
appplyOrNot = true;
if(turn == 1)
{
gameBoardMatrix[0] = 1;
}
else
{
gameBoardMatrix[0] = -1;
}
}
if(x/100 == 1 && y/100 == 0 && gameBoardMatrix[1] == 0)
{
appplyOrNot = true;
if(turn == 1)
{
gameBoardMatrix[1] = 1;
}
else
{
gameBoardMatrix[1] = -1;
}
}
if(x/100 == 2 && y/100 == 0 && gameBoardMatrix[2] == 0)
{
appplyOrNot = true;
if(turn == 1)
{
gameBoardMatrix[2] = 1;
}
else
{
gameBoardMatrix[2] = -1;
}
}
if(x/100 == 0 && y/100 == 1 && gameBoardMatrix[3] == 0)
{
appplyOrNot = true;
if(turn == 1)
{
gameBoardMatrix[3] = 1;
}
else
{
gameBoardMatrix[3] = -1;
}
}
if(x/100 == 1 && y/100 == 1 && gameBoardMatrix[4] == 0)
{
appplyOrNot = true;
if(turn == 1)
{
gameBoardMatrix[4] = 1;
}
else
{
gameBoardMatrix[4] = -1;
}
}
if(x/100 == 2 && y/100 == 1 && gameBoardMatrix[5] == 0)
{
appplyOrNot = true;
if(turn == 1)
{
gameBoardMatrix[5] = 1;
}
else
{
gameBoardMatrix[5] = -1;
}
}
if(x/100 == 0 && y/100 == 2 && gameBoardMatrix[6] == 0)
{
appplyOrNot = true;
if(turn == 1)
{
gameBoardMatrix[6] = 1;
}
else
{
gameBoardMatrix[6] = -1;
}
}
if(x/100 == 1 && y/100 == 2 && gameBoardMatrix[7] == 0)
{
appplyOrNot = true;
if(turn == 1)
{
gameBoardMatrix[7] = 1;
}
else
{
gameBoardMatrix[7] = -1;
}
}
if(x/100 == 2 && y/100 == 2 && gameBoardMatrix[8] == 0)
{
appplyOrNot = true;
if(turn == 1)
{
gameBoardMatrix[8] = 1;
}
else
{
gameBoardMatrix[8] = -1;
}
}
if(appplyOrNot == true)
{
if(turn == 1)
{
apply_surface(x/100 * 100, y/100 * 100,circle,screen);
turn = 2;
}
else
{
apply_surface(x/100 * 100, y/100 * 100,cross,screen);
turn = 1;
}
appplyOrNot = false;
}
}
}
}
//Check for end game conditions, win or draw
//Check rows
for(i=0;i<=6; i+=3)
{
sum = gameBoardMatrix[i] + gameBoardMatrix[i+1] + gameBoardMatrix[i+2];
if(sum == 3)
{
gameWon = true;
}
else if(sum == -3)
{
gameWon = true;
}
}
//Check columns
for(i=0;i<=2;i++)
{
sum = gameBoardMatrix[i] + gameBoardMatrix[i+3] + gameBoardMatrix[i+6];
if(sum == 3)
{
gameWon = true;
}
else if(sum == -3)
{
gameWon = true;
}
}
//Check diagonals
if(gameBoardMatrix[0] + gameBoardMatrix[4] + gameBoardMatrix[8] == 3 || gameBoardMatrix[2] + gameBoardMatrix[4] + gameBoardMatrix[6] == 3)
{
gameWon = true;
}
else if(gameBoardMatrix[0] + gameBoardMatrix[4] + gameBoardMatrix[8] == -3 || gameBoardMatrix[2] + gameBoardMatrix[4] + gameBoardMatrix[6] == -3)
{
gameWon = true;
}
//Checking if end game conditions are met
if(gameWon == true || turnCount == 9)
{
apply_surface(0, 0, gameBoard, screen );
for(i=0;i<=8;i++)
{
gameBoardMatrix[i]=0;
}
gameWon = false;
turnCount = 0;
}
//Incase of user exits
if(event.type == SDL_QUIT)
{
quit = true;
}
//Update the screen
if( SDL_Flip( screen ) == -1 )
{
return 1;
}
}
clean_up();
return 0;
}