/
main.cpp
executable file
·225 lines (204 loc) · 5.21 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
/*
* SDL Tic Tac Toe AI (to be implemented)
* Author: Tudor Marcu
* Copyright (C) Tudor Marcu 2013. All rights reserved.
*/
#include "SDL/SDL.h"
#include "SDL/SDL_image.h"
bool player = true; /* True = X False = O Player */
int handle_button(int *, int *, SDL_Rect *, short int *);
int check_win(short int *);
int main(int argc, char *argv[])
{
SDL_Surface *surface;
SDL_Event event;
SDL_Rect squares[9] = {{0, 0, 130, 130}, {140, 0, 130, 130}, {280, 0, 130, 130},
{0, 140, 130, 130}, {140, 140, 130, 130}, {280, 140, 130, 130},
{0, 280, 130, 130}, {140, 280, 130, 130}, {280, 280, 130, 130}};
SDL_Rect imagepos = {0, 0, 0, 0};
SDL_Surface *imagex;
SDL_Surface *imageo;
short int states[9] = {0, 0, 0, 0, 0, 0, 0, 0, 0};
int valid = 0;
int i;
int ret;
int mx, my;
bool done = false;
SDL_Init(SDL_INIT_VIDEO);
imagex = SDL_LoadBMP("x.bmp");
imageo = SDL_LoadBMP("o.bmp");
if (imagex == NULL || imageo == NULL) {
fprintf(stderr, "Could not load image: %s\n", SDL_GetError());
return -1;
}
/* Control window position */
putenv(strdup("SDL_VIDEO_CENTERED=1"));
surface = SDL_SetVideoMode(410, 410, 16, SDL_SWSURFACE);
if (!surface) {
fprintf(stderr, "Video mode set failed: %s\n", SDL_GetError());
return -1;
}
SDL_WM_SetCaption("TICTACTOE", "Can't Beat Me");
/* Fill screen and then put white squares on top */
SDL_FillRect(surface, &surface->clip_rect, SDL_MapRGB(surface->format, 0x0, 0x0, 0x0));
for (i = 0; i < 9; i++)
SDL_FillRect(surface, &squares[i], 0xFFFFFF);
/* Update screen buffer */
SDL_Flip(surface);
SDL_Surface *image = NULL;
while(!done) {
while(SDL_PollEvent(&event)) {
switch (event.type) {
case SDL_QUIT:
done = true;
break;
case SDL_MOUSEBUTTONDOWN:
valid = handle_button(&mx, &my, &imagepos, states);
if (valid == 0)
player = !player;
break;
}
}
if (imagepos.x != 0 && valid == 0) {
image = player ? imageo : imagex;
if (SDL_BlitSurface(image, NULL, surface, &imagepos) != 0) {
fprintf(stderr, "Failed to blit image: %s\n", SDL_GetError());
return -1;
}
SDL_Flip(surface);
}
ret = check_win(states);
if (ret == 1) {
printf("Player 1 won\n");
done = true;
} else if (ret == 2) {
printf("Player 2 won\n");
done = true;
} else if (ret == 0){
printf("Draw!\n");
done = true;
} else {
continue;
}
}
SDL_Quit();
return 0;
}
int check_win(short int *state)
{
int i;
/* Player one won -> 1 + 1 + 1 */
if ((state[0] + state[1] + state[2] == 3) ||
(state[3] + state[4] + state[5] == 3) ||
(state[6] + state[7] + state[8] == 3) ||
(state[0] + state[3] + state[6] == 3) ||
(state[1] + state[4] + state[7] == 3) ||
(state[2] + state[5] + state[8] == 3) ||
(state[0] + state[4] + state[8] == 3) ||
(state[2] + state[4] + state[6] == 3)) {
return 1;
}
if ((state[0] + state[1] + state[2] == 30) ||
(state[3] + state[4] + state[5] == 30) ||
(state[6] + state[7] + state[8] == 30) ||
(state[0] + state[3] + state[6] == 30) ||
(state[1] + state[4] + state[7] == 30) ||
(state[2] + state[5] + state[8] == 30) ||
(state[0] + state[4] + state[8] == 30) ||
(state[2] + state[4] + state[6] == 30)) {
return 2;
}
for (i = 0; i < 9; i++) {
if (state[i] == 0)
return -1;
}
return 0;
}
/* Set the x/y coordinate for the X and O images to be places based on mouse */
int handle_button(int *x, int *y, SDL_Rect *pos, short int *state)
{
SDL_GetMouseState(x, y);
if (*x < 130 && *y < 130) { /* First row */
pos->x = 50;
pos->y = 50;
pos->h = 0;
pos->w = 0;
if (state[0] == 0) {
state[0] = player ? 1 : 10;
return 0;
} else return -1;
} else if (*x > 140 && *x < 270 && *y < 130) {
pos->x = 190;
pos->y = 50;
pos->h = 0;
pos->w = 0;
if (state[1] == 0) {
state[1] = player ? 1 : 10;
return 0;
} else return -1;
} else if (*x > 280 && *x < 410 && *y < 130) {
pos->x = 330;
pos->y = 50;
pos->h = 0;
pos->w = 0;
if (state[2] == 0) {
state[2] = player ? 1 : 10;
return 0;
} else return -1;
} else if (*x < 130 && *y > 130 && *y < 270) { /* Second Row */
pos->x = 50;
pos->y = 190;
pos->h = 0;
pos->w = 0;
if (state[3] == 0) {
state[3] = player ? 1 : 10;
return 0;
} else return -1;
} else if (*x > 140 && *x < 270 && *y > 130 && *y < 270) {
pos->x = 190;
pos->y = 190;
pos->h = 0;
pos->w = 0;
if (state[4] == 0) {
state[4] = player ? 1 : 10;
return 0;
} else return -1;
} else if (*x > 280 && *x < 410 && *y > 130 && *y < 270) {
pos->x = 330;
pos->y = 190;
pos->h = 0;
pos->w = 0;
if (state[5] == 0) {
state[5] = player ? 1 : 10;
return 0;
} else return -1;
} else if (*x < 130 && *y > 280 && *y < 410) { /* Third Row */
pos->x = 50;
pos->y = 330;
pos->h = 0;
pos->w = 0;
if (state[6] == 0) {
state[6] = player ? 1 : 10;
return 0;
} else return -1;
} else if (*x > 140 && *x < 270 && *y > 280 && *y < 410) {
pos->x = 190;
pos->y = 330;
pos->h = 0;
pos->w = 0;
if (state[7] == 0) {
state[7] = player ? 1 : 10;
return 0;
} else return -1;
} else if (*x > 280 && *x < 410 && *y > 280 && *y < 410) {
pos->x = 330;
pos->y = 330;
pos->h = 0;
pos->w = 0;
if (state[8] == 0) {
state[8] = player ? 1 : 10;
return 0;
} else return -1;
}
return -1;
}