Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <windows.h>
- #include <time.h>
- void gotoxy(int x,int y) {
- HANDLE hcon;
- hcon = GetStdHandle(STD_OUTPUT_HANDLE);
- COORD dwPos;
- dwPos.X = x;
- dwPos.Y= y;
- SetConsoleCursorPosition(hcon,dwPos);
- }
- int Contar_Matriz(int tablero[150][150],int x,int y,int fila,int columna) {
- int co=0;
- if(x+1!=fila+1) {
- if(tablero[x+1][y]==1)
- co++;
- }
- if(x-1!=-1) {
- if(tablero[x-1][y]==1)
- co++;
- }
- if(y+1!=columna+1) {
- if(tablero[x][y+1]==1)
- co++;
- }
- if(y-1!=-1) {
- if(tablero[x][y-1]==1)
- co++;
- }
- if(x-1!=-1 && y-1!=-1) {
- if(tablero[x-1][y-1]==1)
- co++;
- }
- if(x+1!=fila+1 && y-1!=-1) {
- if(tablero[x+1][y-1]==1)
- co++;
- }
- if(x-1!=-1 && y+1!=columna+1) {
- if(tablero[x-1][y+1]==1)
- co++;
- }
- if(x+1!=fila+1 && y+1!=columna+1) {
- if(tablero[x+1][y+1]==1)
- co++;
- }
- return co;
- }
- void Condicion_de_Vida(int tablero[150][150],int tablero_2[150][150],int x,int y,int fila,int columna) {
- int co;
- co=Contar_Matriz(tablero,x,y,fila,columna);
- if (tablero[x][y]==1) {
- if (co==3 || co==2) {
- tablero_2[x][y]=1;
- } else {
- tablero_2[x][y]=0;
- }
- } else if (tablero[x][y]==0) {
- if (co==3) {
- tablero_2[x][y]=1;
- } else {
- tablero_2[x][y]=0;
- }
- }
- }
- void Imprimir_Matriz(int tablero[150][150],int fila,int columna) {
- int x,y;
- for(y=0 ; y<columna ; y++) {
- for(x=0 ; x<fila ; x++) {
- if(tablero[x][y]==1)
- cout<<char(219);
- else
- cout<<" ";
- }
- cout<<endl;
- }
- }
- int main() {
- int tablero[150][150],tablero_2[150][150];
- int x,y,fila,columna,i=0;
- int repeticiones,op,vel;
- srand(time(NULL));
- //system("mode 650");
- //ShowWindow(GetConsoleWindow(), SW_MAXIMIZE);
- system("color f0");
- do {
- gotoxy(4,3);
- cout<<"EL JUEGO DE LA VIDA\n";
- gotoxy(4,4);
- cout<<"<------------------>\n\n";
- gotoxy(4,6);
- cout<<"Digite el valor de las filas: ";
- cin>>fila;
- gotoxy(4,8);
- cout<<"Digite el valor de las columnas: ";
- cin>>columna;
- gotoxy(4,10);
- cout<<"Digite el numero de repeticiones: ";
- cin>>repeticiones;
- gotoxy(4,12);
- cout<<"Digite la velocidad de cambio de generacion: ";
- cin>>vel;
- gotoxy(4,14);
- cout<<"Introducira datos?";
- gotoxy(4,15);
- cout<<"1 = si";
- gotoxy(4,16);
- cout<<"2 = no";
- gotoxy(4,20);
- cout<<"Opcion: ";
- cin>>op;
- system("cls");
- //Lectura de la Matriz
- for(y=0 ; y<columna ; y++) {
- for(x=0 ; x<fila ; x++) {
- if(op==1) {
- cout<<"Fila "<<x<<" Columna "<<y<<": ";
- cin>>tablero[x][y];
- } else {
- tablero[x][y]=rand()%2;
- gotoxy(5,5);
- cout<<"...Cargando...";
- }
- }
- }
- system("cls");
- //Proceso de muerte y nacimiento
- do {
- gotoxy(0,0);
- Imprimir_Matriz(tablero,fila,columna);
- for(y=0 ; y<columna ; y++) {
- for(x=0 ; x<fila ; x++) {
- Condicion_de_Vida(tablero,tablero_2,x,y,fila,columna);
- }
- }
- //Intercambio de una generacion a otra
- for(y=0 ; y<columna ; y++) {
- for(x=0 ; x<fila ; x++) {
- tablero[x][y]=tablero_2[x][y];
- }
- }
- i=i+1;
- Sleep(vel);
- } while(i!=repeticiones);
- system("pause");
- system("cls");
- gotoxy(10,10);
- cout<<"Digite 0 para salir: ";
- cin>>op;
- system("cls");
- } while(op!=0);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement