-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtic-tac-toe.c~
348 lines (335 loc) · 11 KB
/
tic-tac-toe.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
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
char tahta[3][3] = {
{'.', '.', '.'},
{'.', '.', '.'},
{'.', '.', '.'}
};
int bitis = 0; // Kontrol fonksiyonu 1 dondurdugunde oyun biter.
int khamle = 1;
int bhamle = 1; //Kullanici ve bilgisayarin o anki hamle sayisi
void yaz();
void giris();
void bilgisayar_hamlesi();
int kontrol(char x);
int main()
{
srand(time(NULL));
//printf("===============================================================================\n== XOX Oyunu ==\n== yapan: Ali Sentas ==\n===============================================================================\n\n");
printf("================================================================================\n");
printf("████████╗██╗ ██████╗ ████████╗ █████╗ ██████╗ ████████╗ ██████╗ ███████╗\n");
printf("╚══██╔══╝██║██╔════╝ ╚══██╔══╝██╔══██╗██╔════╝ ╚══██╔══╝██╔═══██╗██╔════╝\n");
printf(" ██║ ██║██║ ██║ ███████║██║ ██║ ██║ ██║█████╗ \n");
printf(" ██║ ██║██║ ██║ ██╔══██║██║ ██║ ██║ ██║██╔══╝ \n");
printf(" ██║ ██║╚██████╗ ██║ ██║ ██║╚██████╗ ██║ ╚██████╔╝███████╗\n");
printf(" ╚═╝ ╚═╝ ╚═════╝ ╚═╝ ╚═╝ ╚═╝ ╚═════╝ ╚═╝ ╚═════╝ ╚══════╝\n");
printf("================================================================================\n\n");
printf("Tic-Tac-Toe oyununa hosgeldiniz, siz X karakterini kullanacaksiniz. Giris yaparken koordinatlari arada bir bosluk birakip tek satirda girebilirsiniz. Ornegin: '2 1' gibi.\nBasarilar\n\n");
while(!bitis){
yaz();
giris();
if(!bitis) khamle++;
if(khamle == 6){
printf("\nOyun berabere...\n\n");
bitis = 1;
}
if(!bitis){
yaz();
bilgisayar_hamlesi();
bhamle++;
}
}
return 0;
}
/**
* yaz fonksiyonu tahtayi ve koordinat numaralarini ekrana yazdiriyor
*/
void yaz(){
int i, j;
printf("\n 1 2 3\n");
for(i = 0; i < 3; i++){
printf("%d", i + 1);
for(j = 0; j < 3; j++){
printf(" %c", tahta[i][j]);
}
printf("\n");
}
printf("\n");
}
/**
* giris fonksiyonu kullanicidan koordinat degerlerini alip tahtaya yaziyor
*/
void giris(){
int x, y;
printf("[x y] degerlerini giriniz > ");
scanf("%d %d", &x, &y);
if(tahta[y-1][x-1] == '.'){
tahta[y-1][x-1] = 'X';
}else{
printf("O alan dolu.\nYeni ");
giris();
}
// Kontrol fonksiyonu dondurdugunde oyunu bitir.
if(kontrol('X') == 1){
yaz();
printf("Tebrikler, oyunu kazandiniz.\n");
bitis = 1;
}
}
void bilgisayar_hamlesi(){
int x, y, xfound, x2found, dotfound, dot, ofound, o2found;
printf("Bilgisayar hamlesini yapiyor...\n");
if(bhamle == 1){
if(tahta[1][1] == '.') // Eger tahtanin ortasi bossa oraya yerlestir
tahta[1][1] = 'O';
else{
// Degilse rastgele bir yere
x = rand() % 3;
y = rand() % 3;
while(tahta[y][x] != '.'){
x = rand() % 3;
y = rand() % 3;
}
tahta[y][x] = 'O';
}
}else if(bhamle == 2){
if(tahta[1][1] == 'O'){
for(x = 0; x < 3; x++){
if(x == 1) continue;
xfound = 0;
x2found = 0;
dotfound = 0;
for(y = 0; y < 3; y++){
if(tahta[y][x] == 'X'){
if(!xfound) xfound++;
else x2found++;
}else if(tahta[y][x] == '.'){
dotfound++;
dot = y;
}
}
if(dotfound && xfound && x2found){
tahta[dot][x] = 'O';
goto control;
}
}
for(y = 0; y < 3; y++){
if(y == 1) continue;
xfound = 0;
x2found = 0;
dotfound = 0;
for(x = 0; x < 3; x++){
if(tahta[y][x] == 'X'){
if(!xfound) xfound++;
else x2found++;
}else if(tahta[y][x] == '.'){
dotfound++;
dot = x;
}
}
if(dotfound && xfound && x2found){
tahta[y][dot] = 'O';
goto control;
}
}
x = rand() % 3;
y = rand() % 3;
while(tahta[y][x] != '.'){
x = rand() % 3;
y = rand() % 3;
}
tahta[y][x] = 'O';
}else{
for(x = 0; x < 3; x++){
for(y = 0; y < 3; y++){
if(x == 1 && y == 1) continue;
if(tahta[y][x] == 'X'){
if(tahta[2 - y][2 - x] == '.'){
tahta[2 - y][2 - x] = 'O';
}else{
x = rand() % 3;
y = rand() % 3;
while(tahta[y][x] != '.'){
x = rand() % 3;
y = rand() % 3;
}
tahta[y][x] = 'O';
}
}
}
}
}
}else{
// First check our win
for(x = 0; x < 3; x++){
ofound = 0;
o2found = 0;
dotfound = 0;
for(y = 0; y < 3; y++){
if(tahta[y][x] == 'O'){
if(!ofound) ofound++;
else o2found++;
}else if(tahta[y][x] == '.'){
dotfound++;
dot = y;
}
}
if(dotfound && ofound && o2found){
tahta[dot][x] = 'O';
goto control;
}
}
for(y = 0; y < 3; y++){
ofound = 0;
o2found = 0;
dotfound = 0;
for(x = 0; x < 3; x++){
if(tahta[y][x] == 'O'){
if(!ofound) ofound++;
else o2found++;
}else if(tahta[y][x] == '.'){
dotfound++;
dot = x;
}
}
if(dotfound && ofound && o2found){
tahta[y][dot] = 'O';
goto control;
}
}
ofound = 0;
o2found = 0;
dotfound = 0;
for(x = 0; x < 3; x++){
if(tahta[x][x] == 'O'){
if(!ofound) ofound++;
else o2found++;
}else if(tahta[x][x] == '.'){
dotfound++;
dot = x;
}
}
if(dotfound && ofound && o2found){
tahta[dot][dot] = 'O';
goto control;
}
ofound = 0;
o2found = 0;
dotfound = 0;
for(x = 0; x < 3; x++){
if(tahta[x][2 - x] == 'O'){
if(!ofound) ofound++;
else o2found++;
}else if(tahta[x][2 - x] == '.'){
dotfound++;
dot = 2 - x;
}
}
if(dotfound && ofound && o2found){
tahta[2 - dot][dot] = 'O';
goto control;
}
// Check opponents win
for(x = 0; x < 3; x++){
xfound = 0;
x2found = 0;
dotfound = 0;
for(y = 0; y < 3; y++){
if(tahta[y][x] == 'X'){
if(!xfound) xfound++;
else x2found++;
}else if(tahta[y][x] == '.'){
dotfound++;
dot = y;
}
}
if(dotfound && xfound && x2found){
tahta[dot][x] = 'O';
goto control;
}
}
for(y = 0; y < 3; y++){
xfound = 0;
x2found = 0;
dotfound = 0;
for(x = 0; x < 3; x++){
if(tahta[y][x] == 'X'){
if(!xfound) xfound++;
else x2found++;
}else if(tahta[y][x] == '.'){
dotfound++;
dot = x;
}
}
if(dotfound && xfound && x2found){
tahta[y][dot] = 'O';
goto control;
}
}
xfound = 0;
x2found = 0;
dotfound = 0;
for(x = 0; x < 3; x++){
if(tahta[x][x] == 'X'){
if(!xfound) xfound++;
else x2found++;
}else if(tahta[x][x] == '.'){
dotfound++;
dot = x;
}
}
if(dotfound && xfound && x2found){
tahta[dot][dot] = 'O';
goto control;
}
xfound = 0;
x2found = 0;
dotfound = 0;
for(x = 0; x < 3; x++){
if(tahta[x][2 - x] == 'X'){
if(!xfound) xfound++;
else x2found++;
}else if(tahta[x][2 - x] == '.'){
dotfound++;
dot = 2 - x;
}
}
if(dotfound && xfound && x2found){
tahta[2 - dot][dot] = 'O';
goto control;
}
// ikisi de yoksa rastgele oyna
x = rand() % 3;
y = rand() % 3;
while(tahta[y][x] != '.'){
x = rand() % 3;
y = rand() % 3;
}
tahta[y][x] = 'O';
}
control:
if(kontrol('O')){
yaz();
printf("Bilgisayar kazandi. :(\n\n");
bitis = 1;
}
}
/**
* Kontrol fonksiyonu butun kazanma olasiliklarini kontrol ediyor.
* @param x : char : kontrol edilecek deger
* @return eger karakterden yanyana 3 tane bulursa 1, aksi halde 0
*/
int kontrol(char x){
int i;
// Yatay ve dikey kontrol
for(i = 0; i < 3; i++){
if(tahta[i][0] == x && tahta[i][1] == x && tahta[i][2] == x) return 1;
if(tahta[0][i] == x && tahta[1][i] == x && tahta[2][i] == x) return 1;
}
// Carpraz kontrol
// 2 olasilik var
if(tahta[0][0] == x && tahta[1][1] == x && tahta[2][2] == x) return 1;
if(tahta[0][2] == x && tahta[1][1] == x && tahta[2][0] == x) return 1;
return 0;
}