Código
#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() { time_t 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= '_'; for(a=0;a<N;a++) { for(b=0;b<N;b++) { } aux1=1; } //Indica movimiento realizado. if (mov==1) if (mov==2) if (mov==3) if (mov==4) for (i=0; i<N; i++){ for (j=0; j<N; j++){ puntaje= tablero[i][j]+c; c=puntaje; if (tablero[i][j]==2048){ juego=1;} } } } void random_m() { int h,g; do { b1=0; 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() { } void movimiento() { int i,h,g,a; int band=0; char t1; time_t t; do { char aux[1]; 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: { break; } } imp_tablero(); } while(aux1==1); verificar_mov(); } void opciones() { char decision; if (decision=='s'||decision=='S') juego=0; else } void verificar_mov() { if (no_mov1==1&&no_mov2==1){ aux2=0; aux3=0; aux4=0; no_mov1=0; no_mov2=0; b1=0; } if (no_mov3==1&&no_mov4==1){ aux2=0; aux3=0; aux4=0; no_mov3=0; no_mov4=0; b1=0; } if (no_mov5==1&&no_mov6==1){ aux2=0; aux3=0; no_mov5=0; no_mov6=0; b1=0; } if (no_mov7==1&&no_mov8==1){ aux2=0; aux3=0; no_mov7=0; no_mov8=0; b1=0; } }
[MOD] Usar las etiquetas GeShi, para publicar código.