G
Gina
I need to add the cell generation to a templated program. I am using
graphics magician, but my problem is with the math. I cannot figure
out my cell generations. I do know that I need two different arrays.
One array is the original grid, and one is the copy of that grid.
But, I am stuck..here is my code, if anyone could please help me I
would greatly appreciate it.
#include "GraphicsMagician.h"
#include "GMDefines.h"
#include <stdlib.h>
#include <time.h>
GMMachine Machine;
const int MAXX = 47; // Largest x cell
const int MAXY = 47; // Largest y cell
const int PIXSIZE = 10; // the size of each life cell
const int GENTIME = 30; // the time between generations (1/30 of a
second ticks)
const int DEBOUNCETIME = 5; // time to wait before accepting
another mouse click
int PASCAL WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR
lpCmdLine, int nWinMode)
{
if (Machine.Start("Life", hInst, nWinMode))
try
{
int media[MAXX][MAXY] = {0}; // the media that the cells grow in
int x=0,y=0; // cursor coordinates
int x1, y1; // cursor coordinates converted to cell coordinated
int gen = 0; // the current generation
int sim = 0; // are we simulating? 0=no, 1=yes
int genTimer = 0; // how long until the next generation
int deBounceR=0; // how long to wait before accepting another
right mouse click
int deBounceL=0; // how long to wait before accepting another
left mouse click
int i,j; // loop counters
// this function calculates the next generation from the current one
void nextgen(int media[MAXX][MAXY]);
Machine.DrawOnHiddenScreen(); // we draw on the hidden screen
and then display it at the end
Machine.TextColor(RGB(0,255,0)); // yellow text
while (Machine.Active())
{
// compute the new x,y coordinates from the mouse inputs
x += Machine.Mouse.x;
y += Machine.Mouse.y;
// draw grid
Machine.ClearScreen(Machine.Black);
for (i=0; i<=MAXX; i++)
Machine.Line(i*PIXSIZE, 0, i*PIXSIZE, MAXY*PIXSIZE, Machine.Red);
for (i=0; i<=MAXY; i++)
Machine.Line(0, i*PIXSIZE, MAXX*PIXSIZE, i*PIXSIZE, Machine.Red);
// display life
for (i=0; i<MAXX; i++)
for (j=0; j<MAXY; j++)
if (media[j] > 0)
Machine.Rectangle(i*PIXSIZE+1, j*PIXSIZE+1, i*PIXSIZE+PIXSIZE,
j*PIXSIZE+PIXSIZE, gmFILL, Machine.Violet);
// display cursor
if (x<0) x=0; // first make sure the cursor does not go off of
the screen
if (x>639) x=639;
if (y<0) y=0;
if (y>479) x=479;
Machine.Circle(x,y,PIXSIZE/2-1,gmFILL, Machine.Violet); //put up
cursor
if (sim == 1) // if the simulation is active
{
// check for next generation
if (genTimer <= 0) // if we are done waiting
{
genTimer = GENTIME; // reset timer
gen++; // move to next generation
nextgen(media);
}
else
{
genTimer--; // keep waiting
}
}
// display status
gotoxy(500,10);
cout << "Generation: " << gen;
gotoxy(500,35);
cout << "Simulating: " << sim;
// handle left mouse click (set cell status)
if (deBounceL>0) deBounceL--; // count down the debounce timer
if(Machine.MouseLeftButton && deBounceL == 0) // if the button is
down and the timer has expired
{
x1 = x/PIXSIZE; // calculate the cell that the mouse is
over
y1 = y/PIXSIZE;
if (x1 >= 0 && x1 < MAXX && y1 >= 0 && y1 < MAXY) // if it is on
the media
{
if (media[x1][y1] == 0) // flip the status of the cell
media[x1][y1] = 1;
else
media[x1][y1] = 0;
}
deBounceL = DEBOUNCETIME; // start the debounce timer
}
// handle left mouse click (start or stop simulation)
if (deBounceR>0) deBounceR--; // count down the debounce timer
if(Machine.MouseRightButton && deBounceR == 0)// if the button is
down and the timer has expired
{
if (sim == 0) // flip the status of the simulation
sim = 1;
else
sim = 0;
deBounceR = DEBOUNCETIME; // start the debounce timer
}
// display the new screen
Machine.FlipTimed();
}
}
catch (GMExit){};
return Machine.ReturnValue;
}
// this function calculates the next generation from the current one
void nextgen(int media[MAXX][MAXY])
{
int i,j,n/*number of neighbors*/,x1,y1;
int oldmedia[MAXX][MAXY];
oldmedia[MAXX][MAXY]=media[MAXX][MAXY]=0;
for (i=0; i<MAXX; ++i)
for (j=0; j<MAXY; ++j)
{
n = 0;
for (x1=i-1; x1<=i+1; ++x1)
for (y1=j-1; y1<=j+1; ++y1)
n = n + oldmedia[x1][y1];
n = n - oldmedia[j];
if ((oldmedia[j]==0) && (n==3) || (oldmedia[j]==1) &&
((n==2) || (n==3)))
media[j] = 1;
else
media[j] = 0;
for (i=0; i<MAXX; ++i)
for (j=0; j<MAXY; ++j)
oldmedia[j] = media[j];
}
}
graphics magician, but my problem is with the math. I cannot figure
out my cell generations. I do know that I need two different arrays.
One array is the original grid, and one is the copy of that grid.
But, I am stuck..here is my code, if anyone could please help me I
would greatly appreciate it.
// John Horton Conway's "Game of Life"
#include "GraphicsMagician.h"
#include "GMDefines.h"
#include <stdlib.h>
#include <time.h>
GMMachine Machine;
const int MAXX = 47; // Largest x cell
const int MAXY = 47; // Largest y cell
const int PIXSIZE = 10; // the size of each life cell
const int GENTIME = 30; // the time between generations (1/30 of a
second ticks)
const int DEBOUNCETIME = 5; // time to wait before accepting
another mouse click
int PASCAL WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR
lpCmdLine, int nWinMode)
{
if (Machine.Start("Life", hInst, nWinMode))
try
{
int media[MAXX][MAXY] = {0}; // the media that the cells grow in
int x=0,y=0; // cursor coordinates
int x1, y1; // cursor coordinates converted to cell coordinated
int gen = 0; // the current generation
int sim = 0; // are we simulating? 0=no, 1=yes
int genTimer = 0; // how long until the next generation
int deBounceR=0; // how long to wait before accepting another
right mouse click
int deBounceL=0; // how long to wait before accepting another
left mouse click
int i,j; // loop counters
// this function calculates the next generation from the current one
void nextgen(int media[MAXX][MAXY]);
Machine.DrawOnHiddenScreen(); // we draw on the hidden screen
and then display it at the end
Machine.TextColor(RGB(0,255,0)); // yellow text
while (Machine.Active())
{
// compute the new x,y coordinates from the mouse inputs
x += Machine.Mouse.x;
y += Machine.Mouse.y;
// draw grid
Machine.ClearScreen(Machine.Black);
for (i=0; i<=MAXX; i++)
Machine.Line(i*PIXSIZE, 0, i*PIXSIZE, MAXY*PIXSIZE, Machine.Red);
for (i=0; i<=MAXY; i++)
Machine.Line(0, i*PIXSIZE, MAXX*PIXSIZE, i*PIXSIZE, Machine.Red);
// display life
for (i=0; i<MAXX; i++)
for (j=0; j<MAXY; j++)
if (media[j] > 0)
Machine.Rectangle(i*PIXSIZE+1, j*PIXSIZE+1, i*PIXSIZE+PIXSIZE,
j*PIXSIZE+PIXSIZE, gmFILL, Machine.Violet);
// display cursor
if (x<0) x=0; // first make sure the cursor does not go off of
the screen
if (x>639) x=639;
if (y<0) y=0;
if (y>479) x=479;
Machine.Circle(x,y,PIXSIZE/2-1,gmFILL, Machine.Violet); //put up
cursor
if (sim == 1) // if the simulation is active
{
// check for next generation
if (genTimer <= 0) // if we are done waiting
{
genTimer = GENTIME; // reset timer
gen++; // move to next generation
nextgen(media);
}
else
{
genTimer--; // keep waiting
}
}
// display status
gotoxy(500,10);
cout << "Generation: " << gen;
gotoxy(500,35);
cout << "Simulating: " << sim;
// handle left mouse click (set cell status)
if (deBounceL>0) deBounceL--; // count down the debounce timer
if(Machine.MouseLeftButton && deBounceL == 0) // if the button is
down and the timer has expired
{
x1 = x/PIXSIZE; // calculate the cell that the mouse is
over
y1 = y/PIXSIZE;
if (x1 >= 0 && x1 < MAXX && y1 >= 0 && y1 < MAXY) // if it is on
the media
{
if (media[x1][y1] == 0) // flip the status of the cell
media[x1][y1] = 1;
else
media[x1][y1] = 0;
}
deBounceL = DEBOUNCETIME; // start the debounce timer
}
// handle left mouse click (start or stop simulation)
if (deBounceR>0) deBounceR--; // count down the debounce timer
if(Machine.MouseRightButton && deBounceR == 0)// if the button is
down and the timer has expired
{
if (sim == 0) // flip the status of the simulation
sim = 1;
else
sim = 0;
deBounceR = DEBOUNCETIME; // start the debounce timer
}
// display the new screen
Machine.FlipTimed();
}
}
catch (GMExit){};
return Machine.ReturnValue;
}
// this function calculates the next generation from the current one
void nextgen(int media[MAXX][MAXY])
{
int i,j,n/*number of neighbors*/,x1,y1;
int oldmedia[MAXX][MAXY];
oldmedia[MAXX][MAXY]=media[MAXX][MAXY]=0;
for (i=0; i<MAXX; ++i)
for (j=0; j<MAXY; ++j)
{
n = 0;
for (x1=i-1; x1<=i+1; ++x1)
for (y1=j-1; y1<=j+1; ++y1)
n = n + oldmedia[x1][y1];
n = n - oldmedia[j];
if ((oldmedia[j]==0) && (n==3) || (oldmedia[j]==1) &&
((n==2) || (n==3)))
media[j] = 1;
else
media[j] = 0;
for (i=0; i<MAXX; ++i)
for (j=0; j<MAXY; ++j)
oldmedia[j] = media[j];
}
}