C sharp - Programar en c JUEGO 2048

 
Vista:

Programar en c JUEGO 2048

Publicado por mikaela (1 intervención) el 06/06/2016 21:08:10
tengo el codigo completo en C, del juego 2048, no logro hacer que pierda, es decir, que termine cuando no queden mas movimientos ni celdas vacias!! Necesito ayuda, alguna sugerencia, algo! He estado horas con esto y no logro que me quede, gracias!!!

Código

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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define N 4
 
 
void imp_tablero ();
void random_m ();
//Funciones Movimientos
void MOV_ARRIBA1 ();
void MOV_ABAJO1 ();
void MOV_IZQ1 ();
void MOV_DER1 ();
void MOV_ARRIBA2 ();
void MOV_ABAJO2();
void MOV_DER2();
void MOV_IZQ2();
void lugares_ARRIBA();
void inicio_juego(); // Imprime tablero, muestra puntaje e indica movimiento.
void movimiento(); // Generar movimiento
void opciones(); //Pregunta al usuario si quiere continuar jugando.
void verificar_mov();
void perder();
int tablero[N][N] = {0};
int aux, aux1=1,aux2=0,aux3=0,aux4=0,aux5=0;
int a,b,ab,mov,no_mov1=0, no_mov2=0,no_mov3=0,no_mov4=0,no_mov5=0,no_mov6=0,no_mov7=0,no_mov8=0;
int b1=0, juego=0;
	int cambio_num=0;
int M=N-2, puntaje;
char decision;
		int main()
		{
printf ("--------------------------------------------------\n");;
printf ("**2222222222**0000000000**444****444**8888888888**\n");;
printf ("**2222222222**0000000000**444****444**8888888888**\n");;
printf ("*********222**000****000**444****444**888****888**\n");;
printf ("**2222222222**000****000**4444444444**8888888888**\n");;
printf ("**2222222222**000****000**4444444444**8888888888**\n");;
printf ("**222*********000****000*********444**888****888**\n");;
printf ("**2222222222**0000000000*********444**8888888888**\n");;
printf ("**2222222222**0000000000*********444**8888888888**\n");;
printf ("--------------------------------------------------\n\n\n");;
 
 
 
			time_t t;
			srand((unsigned) time(&t));
			for(a=0;a<3;a++)
			{
				random_m();
			}
			while (juego==0){
			inicio_juego();
			imp_tablero();
			movimiento();
			}
			opciones ();
 
			return 0;
 
		}
void imp_tablero()
{
	int a,b,c=0,i,j;
	char borde = '|', piso= '_';
	printf("\n");
	for(a=0;a<N;a++)
	{
		for(b=0;b<N;b++)
		{
 
			printf("%c\t%d\t%c",borde, tablero[a][b], borde);
		}
		printf ("\n");
		aux1=1;
	}
 
	//Indica movimiento realizado.
	if (mov==1)
	printf("\nMovimiento hacia arriba\n");
	if (mov==2)
	printf ("\nMovimiento hacia abajo\n");
	if (mov==3)
	printf ("\nMovimiento hacia la izquierda\n");
	if (mov==4)
	printf ("\nMovimiento hacia la derecha\n");
 
 
	for (i=0; i<N; i++){
		for (j=0; j<N; j++){
				puntaje= tablero[i][j]+c;
				c=puntaje;
				if (tablero[i][j]==2048){
					printf ("\nFELICITACIONES, HAS GANADO EL JUEGO!\n");
					juego=1;}
 
		}
	}
 
	printf("\nPUNTAJE: %d\n", puntaje);
}
void random_m()
{
	int h,g;
do  {
	b1=0;
	h= rand()% N;
	g= rand()% N;
	if(tablero[g][h]==0){    // Alternar para que aparezca '2' y '4'
	if (cambio_num==0)
		{
			tablero[g][h] = 2;
			cambio_num=1;
			b1 = 1;
		}
		else {
			tablero[g][h] = 4;
			cambio_num=0;
			b1 = 1;
		}
		}
	} while (b1==0);
}
void MOV_ARRIBA1()
{
	do{
		ab=0;
		for(a=1;a<N;a++)
			{
			for(b=0;b<N;b++)
			{
				if(tablero[a-1][b]==0&&tablero[a][b]!=0)
				{
					tablero[a-1][b]=tablero[a][b];
					tablero[a][b]=0;
					ab=1;
					aux2=0;
					aux4=0;
 
				}
				else {aux2++;
					if (aux2==12){
						no_mov3=1;
						b1=1;
 
					}
					else aux2=0;
 
 
				}
			}
		}
 
	}while (ab==1);
	mov=1;
 
}
void MOV_ABAJO1()
{
	do{
		ab=0;
		for(a=M;a>=0;a--)
		{
			for(b=0;b<N;b++)
			{
				if(tablero[a+1][b]==0&&tablero[a][b]!=0)
				{
					tablero[a+1][b]=tablero[a][b];
					tablero[a][b]=0;
					ab=1;
					aux2=0;
				}
 
				else {
					aux2++;
					if (aux2==2){
						no_mov5=1;
						b1=1;
					}
					else aux2=0;
				}
				}
			}
		}while(ab==1);
		mov=2;
}
void MOV_IZQ1()
{
	do
	{
		ab=0;
		for (a=0;a<N;a++)
		{
			for (b=1;b<N;b++)
			{
				if(tablero[a][b-1]==0&&tablero[a][b]!=0)
				{
					tablero[a][b-1]=tablero[a][b];
					tablero[a][b]=0;
					ab=1;
					aux2=0;
				}
				else {aux2++;
					if (aux2==2){
						no_mov7=1;
						b1=1;
					}
					else aux2=0;
				}
			}
		}
	}while (ab==1);
	mov=3;
}
void MOV_DER1()
{
	do{
		ab=0;
		for (a=0;a<N;a++)
		{
			for (b=M;b>=0;b--)
			{
 
				if(tablero[a][b+1]==0&&tablero[a][b]!=0)
				{
					tablero[a][b+1]=tablero[a][b];
					tablero[a][b]=0;
					ab=1;
					aux2=0;
				}
				else {aux2++;
					if (aux2==2){
						no_mov1=1;
						b1=1;
					}
					else aux2=0;
				}
			}
		}
	}while (ab==1);
	mov=4;
}
void MOV_ARRIBA2()
{
	for (a=1;a<N;a++)
	{
		for (b=0;b<N;b++)
		{
			if (tablero[a-1][b]==tablero[a][b])
			{
				tablero[a-1][b]+=tablero[a][b];
				tablero[a][b]=0;
				no_mov4=0;
				aux3=0;
			}
			else {
				aux3++;
				if (aux3==12)
				no_mov4=1;
			}
		}
	}
}
void MOV_ABAJO2()
{
 
	for (a=M;a>=0;a--)
	{
		for (b=0;b<N;b++)
		{
			if (tablero[a+1][b]==tablero[a][b])
			{
				tablero[a+1][b]+=tablero[a][b];
				tablero[a][b]=0;
				aux3=0;
			}
			else {
				aux3=1;
				no_mov6=1;
			}
		}
	}
}
void MOV_DER2 ()
{
 
	for (a=0;a<N;a++)
	{
		for (b=M;b>=0;b--)
		{
			if(tablero[a][b+1]==tablero[a][b])
			{
				tablero[a][b+1]+=tablero[a][b];
				tablero[a][b]=0;
				aux3=0;
			}
			else {
				aux3=1;
				no_mov2=1;
			}
 
		}
	}
}
void MOV_IZQ2()
{
 
	for(a=0;a<N;a++)
	{
		for(b=1;b<N;b++)
		{
			if(tablero[a][b-1]==tablero[a][b])
			{
				tablero[a][b-1]+=tablero[a][b];
				tablero[a][b]=0;
				aux3=0;
				}
				else {
				aux3=1;
				no_mov8=1;
			}
		}
	}
}
 
void inicio_juego()
{
	printf("Utilice:\nW para arriba\nS para abajo\nD para derecha \nA para izquierda \n\n\n\n");
	printf ("TABLERO DE JUEGO\n\n\n");;
}
void movimiento()
{
 
	int i,h,g,a;
	int band=0;
	char t1;
	time_t t;
 
	do
		{
 
		printf ("\n			    ^\n");
		printf ("			    W\n");
		printf ("			<A     D>\n");
		printf ("			    S    \n");
		printf ("\n			    v\n\n");
 
 
		printf ("----> ");
 
 
		char aux[1];
			scanf ("%s", &aux);
			printf ("\n\n");
			t1=aux[0];
 
 
 
			switch(t1)
			{
			case 'w': case 'W':
 
				MOV_ARRIBA1 ();
				MOV_ARRIBA2();
				MOV_ARRIBA1 ();
				random_m();
 
 
			break;
 
			case 's': case 'S':
				MOV_ABAJO1();
				MOV_ABAJO2();
				MOV_ABAJO1();
				random_m();
 
				break;
 
			case 'a': case 'A':
				MOV_IZQ1();
				MOV_IZQ2();
				MOV_IZQ1();
				random_m();
 
				break;
 
			case 'd': case 'D':
				MOV_DER1;
				MOV_DER2();
				MOV_DER1();
				random_m();
 
 
				break;
 
			default:
				{
					printf("\n\n\n\tTECLA ERRONEA\n\n");
 
					break;
				}
			}
 
		imp_tablero();
 
	}
 
	while(aux1==1);
 
		verificar_mov();
	printf("TERMINO EL JUEGO");
 
}
void opciones()
{
	char decision;
 
	printf("Desea seguir jugando?\nS = Si\nN = No\n-------> ");
	scanf ("%c", &decision);
	if (decision=='s'||decision=='S')
		juego=0;
	else
		printf ("GRACIAS POR JUGAR! HASTA LA PRÓXIMA..");
}
 
 
void verificar_mov()
{
	if (no_mov1==1&&no_mov2==1){
		printf ("\nNO PUEDE MOVERSE A LA DERECHA\n");
		aux2=0;
		aux3=0;
		aux4=0;
		no_mov1=0;
		no_mov2=0;
		b1=0;
	}
 
	if (no_mov3==1&&no_mov4==1){
		printf ("\nNO PUEDE MOVERSE HACIA ARRIBA\n");
		aux2=0;
		aux3=0;
		aux4=0;
		no_mov3=0;
		no_mov4=0;
		b1=0;
	}
		if (no_mov5==1&&no_mov6==1){
		printf ("\nNO PUEDE MOVERSE HACIA ABAJO\n");
		aux2=0;
		aux3=0;
		no_mov5=0;
		no_mov6=0;
		b1=0;
	}
		if (no_mov7==1&&no_mov8==1){
		printf ("\nNO PUEDE MOVERSE A LA IZQUIERDA\n");
		aux2=0;
		aux3=0;
		no_mov7=0;
		no_mov8=0;
		b1=0;
	}
}
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder