473,390 Members | 875 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,390 software developers and data experts.

Conways Game of Life Source code

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.
// 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[i][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[i][j];

if ((oldmedia[i][j]==0) && (n==3) || (oldmedia[i][j]==1) &&
((n==2) || (n==3)))
media[i][j] = 1;
else
media[i][j] = 0;

for (i=0; i<MAXX; ++i)
for (j=0; j<MAXY; ++j)
oldmedia[i][j] = media[i][j];

}

}
Jul 22 '05 #1
1 9474
Gina wrote:
I need to add the cell generation to a templated program. I am using
graphics magician, but my problem is with the math.
Math? There's only one function in the whole model, and that's already
implemented in your source code as far as I can see.
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..
COuld you please describe your problem in more detail. If you don't know
why you need two grids, thats quite simple: One grid stores the current
generation T, and the other one is used to store the generation T+1. You
can't simply overwrite the T generation, since you need the data there to
compute the future generation.

To get a further introduction to Conways Life, try posting this again at
comp.ai.alife
// 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[i][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[i][j];

if ((oldmedia[i][j]==0) && (n==3) || (oldmedia[i][j]==1) &&
((n==2) || (n==3)))
media[i][j] = 1;
else
media[i][j] = 0;

for (i=0; i<MAXX; ++i)
for (j=0; j<MAXY; ++j)
oldmedia[i][j] = media[i][j];

}

}


--
Dipl.-Inform. Hendrik Belitz
Central Laboratory of Electronics
Research Center Juelich
Jul 22 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: Enzo | last post by:
Hi Ng, It's possible to protect the source code of a js file? With PHP? Thanks in advance! Enzo
41
by: Matt Alanzo | last post by:
Our SOHO 2 person compay sells furniture (not programmers). In '98 we paid $,$$$ for a VBA -Access '97 accounting application, including VBA source code .... an huge investment for us then (and...
6
by: Mike | last post by:
Hi, I teach a VB .NET class, and one of the students obviously cheated on the final exam (which was to write a Tic Tac Toe game). They were smart enough to change all of the variables from the...
0
by: ziggyware | last post by:
Hi All, I've released my source code to my game engine to be viewable online. Please feel free to use any snippets of it for your own personal uses. http://www.ziggyware.com/ZiggyDocs.php
0
by: Paul | last post by:
Greetings: I have been trying to find source code for a Baccarat card game (C++ or Basic, but preferably the former). My reason for this is wanting to experiment with possible Baccarat card...
66
by: Jon Skeet [C# MVP] | last post by:
I'm sure the net will be buzzing with this news fairly soon, but just in case anyone hasn't seen it yet: Microsoft are going to make the source code for the .NET framework (parts of it,...
33
by: Adeel Hasan Akbari | last post by:
Hi! I'm new to this field. Actually i'm a new programmer. And i've never worked in C. It is my humble request to you to provide me with the source code for Spaceball (the game in which there's...
9
by: gkhn86 | last post by:
I download a game's source code from internet. this is the link RapidShare: Easy Filehosting I tried to compile it, the main.cpp file. but it gave errors. In file included from main.cpp no...
1
by: alireza6485 | last post by:
Hi, Do you know any website that has the source code for Game of life??? I tried looking for it on google and all I get is the applet code.I am looking for Java application code. Thanks
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.