el juego de la vida en c++, auxilio
por favor si alguien sabe como programar eso, ayudeme eso sera de gran ayuda es una practica.
// program LR_10.cpp -pag 308
// the Life Game, by Conway.
// writen in Borland c++ - ver 4.5
#include <conio.h>
#include <iostream.h>
#include <iomanip.h>
#include <stdlib.h>
#define N 21
#define DX 2
#define DY 1
unsigned char a[N+2][N+2][2];
int generation,alives;
void Init(int k)
{
int i,j;
generation = 1;
alives = 0;
for(i = 0;i < N+2;i++)
for(j = 0;j < N+2;j++)
a[i][j][k] = 0;
switch(k)
{
case 0 : for(i = 9; i < 12;i++)
for(j = 9;j < 14;j++)
{
a[i][j][k] = 1;
alives++;
}
for(i = 12; i < 15;i++)
for(j = 10;j < 13;j++)
{
a[i][j][k] = 1;
alives++;
}
break;
case 1 : for(i = 1; i < N+1;i++)
{
a[11][i][k] = a[i][11][k] = 1;
alives += 2;
}
alives--;
break;
}
} // INIT
void Show(int k)
{
gotoxy(55,5);
cout << "generation : " << setw(3) << generation;
gotoxy(55,7);
cout << " alives : " << setw(3) << alives;
for(int i = 1; i < N+1;i++)
for(int j = 1; j < N+1;j++)
{
gotoxy(5+j*DX,3+i*DY);
if(a[i][j][k])
cout << "x";
else
cout << "·";
}
} // SHOW
int SumAround(int i,int j,int k)
{
int counter = -a[i][j][k];
for(int di = -1;di < 2;di++)
for(int dj = -1;dj < 2;dj++)
counter += a[i+di][j+dj][k];
return counter;
} // SUM AROUND
void Check(int k)
{
int state;
generation++;
alives = 0;
for(int i = 1; i < N+1;i++)
for(int j = 1; j < N+1;j++)
{
int counter = SumAround(i,j,k);
switch(a[i][j][k])
{
case 0 : if(counter == 2) // empty cell
state = 1;
else
state = 0;
break;
case 1 : switch(counter) // alives cell
{
case 2 :
case 3 : state = 1;
break;
default : state = 0;
break;
} // switch counter;
} // switch GetBit
a[i][j][1-k] = state;
alives += state;
} // for j
} // CHECK
void Process()
{
for(int i = 0;i < 2;i++)
{
int k = i;
Init(k);
for(char choice = '*';choice != 27;)
{
Show(k);
Check(k);
k = 1 - k;
choice = getch();
Show(k);
}
}
} // PROCESS
void main()
{
clrscr();
cout << "the Life Game, by Conway : n"
<< "press any key to continue, ESC for end n";
cout << "-----------------------------------------------------"
"-------------------------n";
Process();
cout << endl;
cout << "-----------------------------------------------------"
"-------------------------n";
cout << "end of program - good bye ! ! !n";
getch();
} // MAIN
// the Life Game, by Conway.
// writen in Borland c++ - ver 4.5
#include <conio.h>
#include <iostream.h>
#include <iomanip.h>
#include <stdlib.h>
#define N 21
#define DX 2
#define DY 1
unsigned char a[N+2][N+2][2];
int generation,alives;
void Init(int k)
{
int i,j;
generation = 1;
alives = 0;
for(i = 0;i < N+2;i++)
for(j = 0;j < N+2;j++)
a[i][j][k] = 0;
switch(k)
{
case 0 : for(i = 9; i < 12;i++)
for(j = 9;j < 14;j++)
{
a[i][j][k] = 1;
alives++;
}
for(i = 12; i < 15;i++)
for(j = 10;j < 13;j++)
{
a[i][j][k] = 1;
alives++;
}
break;
case 1 : for(i = 1; i < N+1;i++)
{
a[11][i][k] = a[i][11][k] = 1;
alives += 2;
}
alives--;
break;
}
} // INIT
void Show(int k)
{
gotoxy(55,5);
cout << "generation : " << setw(3) << generation;
gotoxy(55,7);
cout << " alives : " << setw(3) << alives;
for(int i = 1; i < N+1;i++)
for(int j = 1; j < N+1;j++)
{
gotoxy(5+j*DX,3+i*DY);
if(a[i][j][k])
cout << "x";
else
cout << "·";
}
} // SHOW
int SumAround(int i,int j,int k)
{
int counter = -a[i][j][k];
for(int di = -1;di < 2;di++)
for(int dj = -1;dj < 2;dj++)
counter += a[i+di][j+dj][k];
return counter;
} // SUM AROUND
void Check(int k)
{
int state;
generation++;
alives = 0;
for(int i = 1; i < N+1;i++)
for(int j = 1; j < N+1;j++)
{
int counter = SumAround(i,j,k);
switch(a[i][j][k])
{
case 0 : if(counter == 2) // empty cell
state = 1;
else
state = 0;
break;
case 1 : switch(counter) // alives cell
{
case 2 :
case 3 : state = 1;
break;
default : state = 0;
break;
} // switch counter;
} // switch GetBit
a[i][j][1-k] = state;
alives += state;
} // for j
} // CHECK
void Process()
{
for(int i = 0;i < 2;i++)
{
int k = i;
Init(k);
for(char choice = '*';choice != 27;)
{
Show(k);
Check(k);
k = 1 - k;
choice = getch();
Show(k);
}
}
} // PROCESS
void main()
{
clrscr();
cout << "the Life Game, by Conway : n"
<< "press any key to continue, ESC for end n";
cout << "-----------------------------------------------------"
"-------------------------n";
Process();
cout << endl;
cout << "-----------------------------------------------------"
"-------------------------n";
cout << "end of program - good bye ! ! !n";
getch();
} // MAIN
