Título: El juego de la vida
Publicado por: Kenji-chan en 20 Noviembre 2016, 18:35 pm
Hola disculpen alguin me podria ayudar con mi implementacion del juego de la vida de john conway, concretamente en la funcion comprobacion(), se agradece de ante mano. ;D //El juego de la vida #include <stdio.h> #include <windows.h> #include <time.h> int columnas; void gotoxy(int x,int y); void dibujarrejilla(int fil, int col); void dibujarCelula(int rejilla[][columnas], int filas, int col); void llenarRejilla(int rejilla[][columnas], int filas, int col); void comprobacion(int rejilla[][columnas], int filas, int col); int main(){ int filas, col; int i, j; printf("Coloque el numero de filas\n"); printf("Coloque el numero de columnas\n"); columnas = col; int rejilla[filas][col]; llenarRejilla(rejilla, filas, col); dibujarrejilla(filas, col); do{ dibujarCelula(rejilla, filas, col); comprobacion(rejilla, filas, col); Sleep(1000); //getch(); }while(1); return 0; } void gotoxy(int x,int y){ HANDLE hcon; hcon = GetStdHandle(STD_OUTPUT_HANDLE); COORD dwPos; dwPos.X = x; dwPos.Y= y; SetConsoleCursorPosition(hcon,dwPos); } void llenarRejilla(int rejilla[][columnas], int filas, int col){ int i, j; for(i=0; i<filas; i++){ for(j=0; j<col; j++){ } } } void dibujarrejilla(int fil, int col){ int i, j; for(i=0; i<fil; i++){ for(j=0; j<col; j++){ if(i==0){ if(j==0){ gotoxy(i*2,j*2); }else{ if((col-1) == j){ gotoxy(i*2,j*2); gotoxy(i*2,(j*2-1)); }else{ gotoxy(i*2,j*2); gotoxy(i*2,(j*2-1)); } } }else{ if(j==0){ gotoxy(i*2,j*2); }else{ if((col-1) == j){ gotoxy(i*2,j*2); gotoxy(i*2,(j*2-1)); }else{ gotoxy(i*2,j*2); gotoxy(i*2,(j*2-1)); } } if((fil-1) == i){ if(j==0){ gotoxy(i*2,j*2); }else if((col-1) == j){ gotoxy(i*2,j*2); }else{ gotoxy(i*2,j*2); } } } } } } void dibujarCelula(int pos[][columnas], int filas, int col){ int j, i; for(i=0; i<filas-1; i++){ for(j=0; j<col-1; j++){ if(pos[i][j] == 1){ gotoxy((i*2)+1,(j*2)+1); }else{ gotoxy((i*2)+1,(j*2)+1); } } } } void comprobacion(int rejilla[][columnas], int filas, int col){ int i, j, x, y, reja[filas][col], vivas; for(i=0; i<filas; i++){ for(j=0; j<col; j++){ if(rejilla[i][j] == 0){ vivas = 0; for(x=0; x<3; x++){ for(y=0; y<3; y++){ if((i+1)-x == i && (j+1)-y == j){ reja[i][j] = rejilla[i][j]; }else if(rejilla[(i+1)-x][(j+1)-y] == 1){ vivas++; } } } if( vivas == 3){ reja[i][j] = 1; } }else if(rejilla[i][j] == 1){ vivas = 0; for(x=0; x<3; x++){ for(y=0; y<3; y++){ if((i+1)-x == i && (j+1)-y == j){ reja[i][j] = rejilla[i][j]; }else if(rejilla[(i+1)-x][(j+1)-y] == 1){ vivas++; } } } if(vivas == 2 || vivas == 3){ reja[i][j] = 1; }else{ reja[i][j] = 0; } } } } for(i=0; i<filas; i++){ for(j=0; j<col; j++){ rejilla[i][j] = reja[i][j]; } } }
Título: Re: El juego de la vida
Publicado por: engel lex en 20 Noviembre 2016, 18:41 pm
que problema tienes en esa función?
Título: Re: El juego de la vida
Publicado por: Kenji-chan en 20 Noviembre 2016, 18:47 pm
no ebalua correcta mente algunas celdas como x ejemplo devez en cuando me crea una celula donde no hay nada de vida o (http://imageshack.com/a/img921/5756/Rjctm7.png) en las celulas que estan solas ai ariba no mueren por soledad
MOD: Imagen adaptada a lo permitido.
Título: Re: El juego de la vida
Publicado por: Kenji-chan en 20 Noviembre 2016, 18:58 pm
no se si sedeva a que mi funcion no evalua si la comparacion esta fuera de la matriz, solo evalua si es 0 o 1
Título: Re: El juego de la vida
Publicado por: engel lex en 20 Noviembre 2016, 19:07 pm
me parece tu codigo excesivamente anidado y complicado para algo simple... void comprobacion(int rejilla[][columnas], int filas, int col){ int i, j, x, y, reja[filas][col], vivas; for(i=0; i<filas; i++){ for(j=0; j<col; j++){ if(rejilla[i][j] == 0){ vivas = 0; for(x=0; x<3; x++){ for(y=0; y<3; y++){ if((i+1)-x == i && (j+1)-y == j){ reja[i][j] = rejilla[i][j]; }else if(rejilla[(i+1)-x][(j+1)-y] == 1){ vivas++; } } } if( vivas == 3){ reja[i][j] = 1; } }else if(rejilla[i][j] == 1){ vivas = 0; for(x=0; x<3; x++){ for(y=0; y<3; y++){ if((i+1)-x == i && (j+1)-y == j){ reja[i][j] = rejilla[i][j]; }else if(rejilla[(i+1)-x][(j+1)-y] == 1){ vivas++; } } } if(vivas == 2 || vivas == 3){ reja[i][j] = 1; }else{ reja[i][j] = 0; } } } } for(i=0; i<filas; i++){ for(j=0; j<col; j++){ rejilla[i][j] = reja[i][j]; } } }
por cosas de legibilidad lo cambiaría a por otro lado no estoy seguro si compruebas los si está en el borde void comprobacion(int rejilla[][columnas], int filas, int col){ int i, j, reja[filas][col], vivas; for(i=0; i<filas; i++){ for(j=0; j<col; j++){ vivas = rejilla[i+0][j+0]; if(i > 0){ if(j > 0) vivas += rejilla[i-1][j-1]; vivas += rejilla[i-1][j+0]; if(j < col -1) vivas += rejilla[i-1][j+1]; } if(j > 0) vivas += rejilla[i+0][j-1]; vivas += rejilla[i+0][j+0]; if(j < col -1) vivas += rejilla[i+0][j+1]; if(i < filas-1){ if(j > 0) vivas += rejilla[i+1][j-1]; vivas += rejilla[i+1][j+0]; if(j < col -1) vivas += rejilla[i+1][j+1]; } reja[i][j] = 0; if(vivas == 2 || vivas == 3){ reja[i][j] = 1; } } } for(i=0; i<filas; i++){ for(j=0; j<col; j++){ rejilla[i][j] = reja[i][j]; } } } }
Título: Re: El juego de la vida
Publicado por: Kenji-chan en 20 Noviembre 2016, 22:51 pm
ya lo he heco funcionar gracias por la ayuda dejo el code por si alguien lo quiere probar. ;D //El juego de la vida
#include <stdio.h> #include <windows.h> #include <time.h>
int columnas;
void gotoxy(int x,int y); void dibujarrejilla(int fil, int col); void dibujarCelula(int rejilla[][columnas], int filas, int col); void llenarRejilla(int rejilla[][columnas], int filas, int col); void comprobacion(int rejilla[][columnas], int filas, int col);
int main(){
int i, j, filas, col;
do{ printf("Coloque el numero de filas\n"); scanf("%d", &filas); if(filas<10 || filas>40) printf("intodusca un numero mayor que 9 o menor que 41\n"); }while(filas<10 || filas>40);
do{ printf("Coloque el numero de columnas\n"); scanf("%d", &col); if(col<10 || col>29) printf("intodusca un numero mayor que 9 o menor que 30\n"); }while(col<10 || col>29);
columnas = col;
int rejilla[filas][col];
llenarRejilla(rejilla, filas, col); dibujarrejilla(filas, col);
do{ dibujarCelula(rejilla, filas, col); comprobacion(rejilla, filas, col); Sleep(1000); //getch();
}while(1);
getch(); return 0; }
void gotoxy(int x,int y){ HANDLE hcon; hcon = GetStdHandle(STD_OUTPUT_HANDLE); COORD dwPos; dwPos.X = x; dwPos.Y= y; SetConsoleCursorPosition(hcon,dwPos); }
void llenarRejilla(int rejilla[][columnas], int filas, int col){ int i, j;
srand(time(NULL));
for(i=0; i<filas; i++){ for(j=0; j<col; j++){ rejilla[i][j]=rand()%2; } }
}
void dibujarrejilla(int fil, int col){ int i, j;
system("cls");
for(i=0; i<fil; i++){ for(j=0; j<col; j++){ if(i==0){ if(j==0){ gotoxy(i*2,j*2); printf("%c%c",201,205); }else{ if((col-1) == j){ gotoxy(i*2,j*2); printf("%c%c",200,205); gotoxy(i*2,(j*2-1)); printf("%c",186); }else{ gotoxy(i*2,j*2); printf("%c%c",204,205); gotoxy(i*2,(j*2-1)); printf("%c",186); } } }else{ if(j==0){ gotoxy(i*2,j*2); printf("%c%c",203,205); }else{ if((col-1) == j){ gotoxy(i*2,j*2); printf("%c%c",202,205); gotoxy(i*2,(j*2-1)); printf("%c",186); }else{ gotoxy(i*2,j*2); printf("%c%c",206,205); gotoxy(i*2,(j*2-1)); printf("%c",186); } } if((fil-1) == i){ if(j==0){ gotoxy(i*2,j*2); printf("%c ",187); }else if((col-1) == j){ gotoxy(i*2,j*2); printf("%c ",188); }else{ gotoxy(i*2,j*2); printf("%c ",185); } } } } }
}
void dibujarCelula(int pos[][columnas], int filas, int col){ int j, i;
for(i=0; i<filas-1; i++){ for(j=0; j<col-1; j++){ if(pos[i][j] == 1){ gotoxy((i*2)+1,(j*2)+1); printf("%c", 219); }else{ gotoxy((i*2)+1,(j*2)+1); printf(" "); } } }
}
void comprobacion(int rejilla[][columnas], int filas, int col){ int i, j, x, y, reja[filas][col], vivas;
for(i=0; i<filas-1; i++){ for(j=0; j<col-1; j++){ if(rejilla[i][j] == 0){ vivas = 0; for(x=0; x<3; x++){ for(y=0; y<3; y++){ if((i+1)-x == -1 || (j+1)-y == -1 || (i+1)-x > filas-1 || (j+1)-y > col-1){ continue; }else if((i+1)-x == i && (j+1)-y == j){ reja[i][j] = rejilla[i][j]; }else if(rejilla[(i+1)-x][(j+1)-y] == 1){ vivas++; } } } if( vivas == 3){ reja[i][j] = 1; } }else if(rejilla[i][j] == 1){ vivas = 0; for(x=0; x<3; x++){ for(y=0; y<3; y++){ if((i+1)-x == -1 || (j+1)-y == -1 || (i+1)-x > filas-1 || (j+1)-y > col-1){ continue; }else if((i+1)-x == i && (j+1)-y == j){ reja[i][j] = rejilla[i][j]; }else if(rejilla[(i+1)-x][(j+1)-y] == 1){ vivas++; } } } if(vivas == 2 || vivas == 3){ reja[i][j] = 1; }else{ reja[i][j] = 0; } } } }
for(i=0; i<filas; i++){ for(j=0; j<col; j++){ rejilla[i][j] = reja[i][j]; } }
}
|