Advertisement
Guest User

Untitled

a guest
Jul 17th, 2019
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.21 KB | None | 0 0
  1. #include <iostream>
  2. #include <windows.h>
  3. #include <time.h>
  4.  
  5. void gotoxy(int x,int y) {
  6.     HANDLE hcon;
  7.     hcon = GetStdHandle(STD_OUTPUT_HANDLE);
  8.     COORD dwPos;
  9.     dwPos.X = x;
  10.     dwPos.Y= y;
  11.     SetConsoleCursorPosition(hcon,dwPos);
  12. }
  13.  
  14. int Contar_Matriz(int tablero[150][150],int x,int y,int fila,int columna) {
  15.     int co=0;
  16.     if(x+1!=fila+1) {
  17.         if(tablero[x+1][y]==1)
  18.             co++;
  19.     }
  20.     if(x-1!=-1) {
  21.         if(tablero[x-1][y]==1)
  22.             co++;
  23.     }
  24.     if(y+1!=columna+1) {
  25.         if(tablero[x][y+1]==1)
  26.             co++;
  27.     }
  28.     if(y-1!=-1) {
  29.         if(tablero[x][y-1]==1)
  30.             co++;
  31.     }
  32.     if(x-1!=-1 && y-1!=-1) {
  33.         if(tablero[x-1][y-1]==1)
  34.             co++;
  35.     }
  36.     if(x+1!=fila+1 && y-1!=-1) {
  37.         if(tablero[x+1][y-1]==1)
  38.             co++;
  39.     }
  40.     if(x-1!=-1 && y+1!=columna+1) {
  41.         if(tablero[x-1][y+1]==1)
  42.             co++;
  43.     }
  44.     if(x+1!=fila+1 && y+1!=columna+1) {
  45.         if(tablero[x+1][y+1]==1)
  46.             co++;
  47.     }
  48.     return co;
  49. }
  50.  
  51. void Condicion_de_Vida(int tablero[150][150],int tablero_2[150][150],int x,int y,int fila,int columna) {
  52.     int co;
  53.     co=Contar_Matriz(tablero,x,y,fila,columna);
  54.     if (tablero[x][y]==1) {
  55.         if (co==3 || co==2) {
  56.             tablero_2[x][y]=1;
  57.         } else {
  58.             tablero_2[x][y]=0;
  59.         }
  60.     } else if (tablero[x][y]==0) {
  61.         if (co==3) {
  62.             tablero_2[x][y]=1;
  63.         } else {
  64.             tablero_2[x][y]=0;
  65.         }
  66.     }
  67. }
  68.  
  69. void Imprimir_Matriz(int tablero[150][150],int fila,int columna) {
  70.     int x,y;
  71.     for(y=0 ; y<columna ; y++) {
  72.         for(x=0 ; x<fila ; x++) {
  73.             if(tablero[x][y]==1)
  74.                 cout<<char(219);
  75.             else
  76.                 cout<<" ";
  77.         }
  78.         cout<<endl;
  79.     }
  80. }
  81.  
  82. int main() {
  83.     int tablero[150][150],tablero_2[150][150];
  84.     int x,y,fila,columna,i=0;
  85.     int repeticiones,op,vel;
  86.     srand(time(NULL));
  87.     //system("mode 650");
  88.     //ShowWindow(GetConsoleWindow(), SW_MAXIMIZE);
  89.     system("color f0");
  90.     do {
  91.         gotoxy(4,3);
  92.         cout<<"EL JUEGO DE LA VIDA\n";
  93.         gotoxy(4,4);
  94.         cout<<"<------------------>\n\n";
  95.         gotoxy(4,6);
  96.         cout<<"Digite el valor de las filas: ";
  97.         cin>>fila;
  98.         gotoxy(4,8);
  99.         cout<<"Digite el valor de las columnas: ";
  100.         cin>>columna;
  101.         gotoxy(4,10);
  102.         cout<<"Digite el numero de repeticiones: ";
  103.         cin>>repeticiones;
  104.         gotoxy(4,12);
  105.         cout<<"Digite la velocidad de cambio de generacion: ";
  106.         cin>>vel;
  107.         gotoxy(4,14);
  108.         cout<<"Introducira datos?";
  109.         gotoxy(4,15);
  110.         cout<<"1 = si";
  111.         gotoxy(4,16);
  112.         cout<<"2 = no";
  113.         gotoxy(4,20);
  114.         cout<<"Opcion: ";
  115.         cin>>op;
  116.         system("cls");
  117.         //Lectura de la Matriz
  118.         for(y=0 ; y<columna ; y++) {
  119.             for(x=0 ; x<fila ; x++) {
  120.                 if(op==1) {
  121.                     cout<<"Fila "<<x<<" Columna "<<y<<": ";
  122.                     cin>>tablero[x][y];
  123.                 } else {
  124.                     tablero[x][y]=rand()%2;
  125.                     gotoxy(5,5);
  126.                     cout<<"...Cargando...";
  127.                 }
  128.             }
  129.         }
  130.         system("cls");
  131.         //Proceso de muerte y nacimiento
  132.         do {
  133.             gotoxy(0,0);
  134.             Imprimir_Matriz(tablero,fila,columna);
  135.             for(y=0 ; y<columna ; y++) {
  136.                 for(x=0 ; x<fila ; x++) {
  137.  
  138.                     Condicion_de_Vida(tablero,tablero_2,x,y,fila,columna);
  139.                 }
  140.             }
  141.             //Intercambio de una generacion a otra
  142.             for(y=0 ; y<columna ; y++) {
  143.                 for(x=0 ; x<fila ; x++) {
  144.                     tablero[x][y]=tablero_2[x][y];
  145.                 }
  146.             }
  147.             i=i+1;
  148.             Sleep(vel);
  149.         } while(i!=repeticiones);
  150.         system("pause");
  151.         system("cls");
  152.         gotoxy(10,10);
  153.         cout<<"Digite 0 para salir: ";
  154.         cin>>op;
  155.         system("cls");
  156.     } while(op!=0);
  157.     return 0;
  158. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement