-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
310 lines (276 loc) · 7.83 KB
/
main.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
#include <stdlib.h>
#include <stdio.h>
#include <SDL/SDL.h>
#include <SDL/SDL_image.h>
#include <SDL/SDL_ttf.h>
#include "include/constantes.h"
#include "include/jeu.h"
#include "include/editeur.h"
#include "include/fichiers.h"
int main(int argc, char *argv[])
{
SDL_Surface *ecran = NULL, *menu = NULL;
SDL_Rect positionMenu;
SDL_Event event;
int continuer = 1;
SDL_Init(SDL_INIT_VIDEO);
TTF_Init();
// Chargement de l'icone du jeu
SDL_WM_SetIcon(IMG_Load("res/images/pac_ghost.gif"), NULL);
ecran = SDL_SetVideoMode(LARGEUR_FENETRE, HAUTEUR_FENETRE, 32, SDL_HWSURFACE | SDL_DOUBLEBUF);
// Titre de la fenetre du jeu
SDL_WM_SetCaption("<|°_°|> SOKO-PACMAN <|°_°|>", NULL);
menu = IMG_Load("res/images/menu.bmp");
positionMenu.x = 0;
positionMenu.y = 0;
while (continuer)
{#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "global.h"
#include "fusion.h"
/* The header file */
/*2 Dimensional array a[TAILLE][TAILLE] For all 0*/
void Start() /* Generate start Interface */
{
printf("Welcome to 2048\n");
printf(" 1). A new game \n");
printf(" 2). help \n");
}
int Score(int **a) /* The scoring function */
{
int i, j;
int max = a[0][0];
int sum = 0;
for (i = 0; i < TAILLE; i++)
for (j = 0; j < TAILLE; j++)
if (a[i][j] > max)
max = a[i][j];
sum += max;
return sum; /* Take the sum of the largest number as the score */
}
void Interface(int **a)
{
int i, j;
int b[2] = {2, TAILLE}, c[TAILLE] = {0, 1, 2, 3}, sum, n, m;
do
{
srand((int)time(NULL)); /* Take time as a random number seed */
m = c[rand() % TAILLE];
n = c[rand() % TAILLE];
/* Randomly generated coordinates */
if (a[m][n] == 0) /* Determines whether it is a space */
{
a[m][n] = b[rand() % 2]; /* Randomly generated 2 or TAILLE*/
break; /* Jump out of the loop */
}
} while (1);
printf("%d %d %d\n", m, n, a[m][n]);
for (i = 0; i < TAILLE; i++)
{
printf("-------------------------\n");
for (j = 0; j < TAILLE; j++)
{
if ((m == i) && (n == j))
printf("| %d ", a[m][n]); /* The output is randomly generated 2 or TAILLE*/
else
{
if (a[i][j] > 0)
printf("| %d ", a[i][j]); /* if a[i][j] If there are Numbers, output them */
else
printf("| "); /* if a[i][j] No Numbers, output Spaces */
}
}
printf("|\n");
}
printf("-------------------------\n");
printf(" score :%d\n", sum = Score(a));
}
void Mouvement(char b, int **a)
{
int x, m, i, j;
switch (b)
{
case 'h':
for (j = 0; j < TAILLE; j++)
for (i = 1; i <= 3; i++)
for (x = i, m = i; x > 0; x--, m--) /*x Is the number of cycles, m alternative i Keep the cycle going */
{
if (a[m - 1][j] > 0)
break; /* If the 1 A non 0 And jump down 1 position */
else
{
a[m - 1][j] = a[m][j];
a[m][j] = 0;
}
}
Fusion_up(a);
break;
case 'b':
for (j = 0; j < TAILLE; j++)
for (i = 2; i >= 0; i--)
for (x = 3 - i, m = i; x > 0; x--, m++)
{
if (a[m + 1][j] > 0)
break;
else
{
a[m + 1][j] = a[m][j];
a[m][j] = 0;
}
}
Fusion_down(a);
break;
case 'g':
for (i = 0; i < TAILLE; i++)
for (j = 1; j <= 3; j++)
for (x = j, m = j; x > 0; x--, m--)
{
if (a[i][m - 1] > 0)
break;
else
{
a[i][m - 1] = a[i][m];
a[i][m] = 0;
}
}
Fusion_left(a);
break;
case 'd':
for (i = 0; i < TAILLE; i++)
for (j = 2; j >= 0; j--)
for (x = 3 - j, m = j; x > 0; x--, m++)
{
if (a[i][m + 1] > 0)
break;
else
{
a[i][m + 1] = a[i][m];
a[i][m] = 0;
}
}
Fusion_right(a);
break;
}
}
int ** allocation ()
{
int **Tab;
Tab = malloc(TAILLE * sizeof(int*));
int i ;
if ( Tab == NULL )
{
exit( EXIT_FAILURE ) ;
}
for ( i = 0 ; i < TAILLE ; i++ )
{
Tab[i] = malloc(TAILLE * sizeof(int));
}
return Tab;
}
int main(int argc, char *argv[])
{
system("clear");
int i, j;
int **plateau;
plateau = allocation();
for (i = 0; i < TAILLE; i++) {
for (j= 0; j < TAILLE; j++) {
plateau[i][j] = 0;
}
}
for (i = 0; i < TAILLE; i++) {
for (j= 0; j < TAILLE; j++) {
printf("%d\t",plateau[i][j]);
}
printf("\n");
}
printf("\n\n\n");
int flag = 1;
char c, k;
system("stty -icanon");
outloop:;
Start();
k = getchar();
printf("\n");
if (k == '1')
{
while (flag == 1)
{
Interface(plateau);
c = getchar();
printf("\n");
system("clear");
Mouvement(c, plateau);
for (i = 0; i < TAILLE; i++)
for (j = 0; j < TAILLE; j++)
{
if (plateau[i][j] == 0)
{
flag = 1;
goto end; /* If you have a checkerboard 1 Jump out of the judgment and continue to generate random Numbers */
}
else
flag = 0;
}
end:;
}
}
if (k == '2')
{
do
{
printf(" Rules of the game: by clicking <w>,<s>,<a>,<d> Key to move the Numbers up, down, left and right. Add the same Numbers. The sum of the Numbers in each cell will be scored. \n");
printf(" 3). return \n");
k = getchar();
} while (k != 3);
goto outloop; /* Jump to the start screen */
}
printf(" Game over \n");
return 0;
}
SDL_WaitEvent(&event);
switch (event.type)
{
case SDL_QUIT:
continuer = 0;
break;
case SDL_KEYDOWN:
switch (event.key.keysym.sym)
{
case SDLK_ESCAPE: // Demande l'arret du jeu
continuer = 0;
break;
case SDLK_F1: // Touche F1
regles(ecran);
break;
case SDLK_KP1: // Demande à jouer, niveau 1
jouer(ecran, 1);
break;
case SDLK_KP2: // Demande à jouer, niveau 2
jouer(ecran, 2);
break;
case SDLK_KP3: // Demande à jouer, niveau 3
jouer(ecran, 3);
break;
case SDLK_KP4: // Demande l'éditeur de niveaux
editeur(ecran);
break;
default:
break;
}
break;
default:
break;
}
// Effacement de l'écran
SDL_FillRect(ecran, NULL, SDL_MapRGB(ecran -> format, 0, 0, 0));
SDL_BlitSurface(menu, NULL, ecran, &positionMenu);
SDL_Flip(ecran);
}
//printf("Nombre de déplacements : %d\n", deplacements);
SDL_FreeSurface(menu);
SDL_Quit();
TTF_Quit();
return EXIT_SUCCESS;
}