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
|
/* IA pour morpion récursif - Alexis
* main.c
* Contient les fonctions principales */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "main.h"
#define INPUT_LENGTH 50
int main()
{
int i,j,k,l;
char input[INPUT_LENGTH];
float timeout;
Dots grid[9][9];
Boxes subgrid, move, firstsubgrid;
int firstMove=1;
for(i=0;i<9;i++)
for(j=0;j<9;j++)
grid[i][j] = FREE;
subgrid = Z;
firstsubgrid=V;
// Dis bonjour à Alex ...
fgets(input, sizeof(input), stdin);
for(i=0;i<INPUT_LENGTH;i++){if(input[i]=='\n'){input[i]='\0';}}
if(strcmp(input, "Hello morpion_rec") == 0)
{
printf("Hello morpion_rec\n");
}
else
{
printf("Who are you, crazy bastard?\n");
return EXIT_FAILURE;
}
printGrid(grid);
// Est-ce à moi de jouer ?
while(1)
{
while(1)
{
fgets(input, sizeof(input), stdin);
// Attendre pour jouer
if(strstr(input,"Your turn")==input)
{
sscanf(input+10,"%f",&timeout);
break;
}
//Attendre le coup de l'adversaire
if(strstr(input,"Play")==input)
{
sscanf(input+5,"%d %d %d %d",&i,&j,&k,&l);
subgrid=(i-1)+(j-1)*3;
move = (k-1)+(l-1)*3;
fprintf(stderr,"%d %d\n", subgrid, move);
// L'adversaire joue les ronds
if(firstMove)
{
firstsubgrid=subgrid;
firstMove=0;
}
play(grid, subgrid, CIRCLE, move);
printGrid(grid);
subgrid=move;
}
// Attendre le résultat de la partie
if(strstr(input,"You win")==input)
{
printf("Yeah!");
return EXIT_SUCCESS;
}
if(strstr(input,"You lose")==input)
{
printf("I am so sad...");
return EXIT_SUCCESS;
}
if(strstr(input,"Tie")==input)
{
printf("It is a good result anyways.");
return EXIT_SUCCESS;
}
if(strstr(input,"Cheater")==input)
{
printf("You are so nasty :(");
return EXIT_FAILURE;
}
}
// On joue toujours les croix
if(firstMove)
{
firstsubgrid=V;
subgrid=firstsubgrid;
firstMove=0;
}
if(chooseMoveAndPlay(grid, subgrid, CROSS, &move, firstsubgrid) != 0){return EXIT_FAILURE;}
printf("Play %d %d %d %d\n", subgrid%3+1, subgrid/3+1, move%3+1, move/3+1);
subgrid=move;
printGrid(grid);
}
return EXIT_SUCCESS;
}
int play(Dots grid[9][9], Boxes subgrid, Dots player, Boxes move)
{
if(subgrid == Z || move == Z || player == FREE || grid[subgrid][move] != FREE)
return 1;
grid[subgrid][move] = player;
}
int chooseMoveAndPlay(Dots grid[9][9], Boxes subgrid, Dots player, Boxes *move, Boxes firstSubgrid)
{
int i,j;
Boxes sg = (subgrid!=Z) ? subgrid : V; // Inutile maintenant
for(i=0;i<9;i++)
{
if(grid[sg][i] == FREE)
{
*move = i;
grid[sg][i] = player;
return 0;
}
}
if(firstSubgrid=subgrid)
{
for(i=0;i<9;i++)
{
for(j=0;j<9;j++)
{
if(grid[j][i] == FREE)
{
*move = i;
grid[j][i] = player;
return 0;
}
}
}
}
return 1;
}
void checkSolutions(Boxes a, Solutions sols)
{
switch(a)
{
case I:
sols[0][0] = II; sols[0][1] = III;
sols[1][0] = IV; sols[1][1] = VII;
sols[2][0] = Z; sols[2][1] = Z;
sols[3][0] = Z; sols[3][1] = Z;
break;
case II:
sols[0][0] = I; sols[0][1] = III;
sols[1][0] = V; sols[1][1] = VIII;
sols[2][0] = Z; sols[2][1] = Z;
sols[3][0] = Z; sols[3][1] = Z;
break;
case III:
sols[0][0] = I; sols[0][1] = II;
sols[1][0] = VI; sols[1][1] = IX;
sols[2][0] = Z; sols[2][1] = Z;
sols[3][0] = Z; sols[3][1] = Z;
break;
case IV:
sols[0][0] = I; sols[0][1] = VII;
sols[1][0] = V; sols[1][1] = VI;
sols[2][0] = Z; sols[2][1] = Z;
sols[3][0] = Z; sols[3][1] = Z;
break;
case V:
sols[0][0] = II; sols[0][1] = VIII;
sols[1][0] = I; sols[1][1] = IX;
sols[2][0] = IV; sols[2][1] = VI;
sols[3][0] = VII; sols[3][1] = III;
break;
case VI:
sols[0][0] = IV; sols[0][1] = V;
sols[1][0] = III; sols[1][1] = IX;
sols[2][0] = Z; sols[2][1] = Z;
sols[3][0] = Z; sols[3][1] = Z;
break;
case VII:
sols[0][0] = I; sols[0][1] = IV;
sols[1][0] = VII; sols[1][1] = IX;
sols[2][0] = Z; sols[2][1] = Z;
sols[3][0] = Z; sols[3][1] = Z;
break;
case VIII:
sols[0][0] = II; sols[0][1] = V;
sols[1][0] = VI; sols[1][1] = IX;
sols[2][0] = Z; sols[2][1] = Z;
sols[3][0] = Z; sols[3][1] = Z;
break;
case IX:
sols[0][0] = III; sols[0][1] = VI;
sols[1][0] = VII; sols[2][1] = IX;
sols[2][0] = Z; sols[2][1] = Z;
sols[3][0] = Z; sols[3][1] = Z;
break;
default:
sols[0][0] = Z; sols[0][1] = Z;
sols[1][0] = Z; sols[1][1] = Z;
sols[2][0] = Z; sols[2][1] = Z;
sols[3][0] = Z; sols[3][1] = Z;
break;
}
}
// Les affichages se font dans stderr
void printGrid(Dots grid[9][9])
{
int x,y, i,j;
fprintf(stderr,"\n*********\n");
for(x=0;x<9;x++)
{
for(y=0;y<9;y++)
{
i=(y/3)*3+(x/3);
j=(y-(y/3)*3)*3+(x-(x/3)*3);
fprintf(stderr,"%d", grid[i][j]);
}
fprintf(stderr,"\n");
}
fprintf(stderr,"\n*********\n");
}
|