473,789 Members | 2,746 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 "GraphicsMagici an.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(HINSTAN CE 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.DrawOnH iddenScreen(); // we draw on the hidden screen
and then display it at the end

Machine.TextCol or(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.ClearSc reen(Machine.Bl ack);
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.Rectang le(i*PIXSIZE+1, j*PIXSIZE+1, i*PIXSIZE+PIXSI ZE,
j*PIXSIZE+PIXSI ZE, 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.Mous eLeftButton && 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.Mous eRightButton && 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.FlipTim ed();

}
}
catch (GMExit){};
return Machine.ReturnV alue;
}

// 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 9514
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 "GraphicsMagici an.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(HINSTAN CE 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.DrawOnH iddenScreen(); // we draw on the hidden screen
and then display it at the end

Machine.TextCol or(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.ClearSc reen(Machine.Bl ack);
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.Rectang le(i*PIXSIZE+1, j*PIXSIZE+1, i*PIXSIZE+PIXSI ZE,
j*PIXSIZE+PIXSI ZE, 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.Mous eLeftButton && 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.Mous eRightButton && 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.FlipTim ed();

}
}
catch (GMExit){};
return Machine.ReturnV alue;
}

// 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
3396
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
3094
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 now!). The application publisher went belly up years ago. Over time we've made a number of VBA code changes (< 500 lines total). Now our CPA is urging us to switch to Quickbooks Premier for Contractors at a cost of $,$$$ plus data entry. Argh, no...
6
1534
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 program that was copied, though, and I'm having a difficult time finding the original source. Can anyone suggest a place that would let me search their database of codes for snips of code, or something like that? I'm hesitant to post
0
1116
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
3720
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 counting methodologies, which is a controversial subject but I'd like to try out some ideas. However, I know very little C++ and am having real trouble even just setting up an 8 deck card shoe. Also, I can't find source code for Baccarat anywhere...
66
7476
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, including the BCL, ASP.NET and LINQ) available both for viewing and debugging into. I won't go into all the details here, as they're covered on Scott Guthrie's blog:
33
2413
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 a ball...etc). Thanks in Anticipation
9
2379
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 newline at end of file can somebody help me? this is the main.cpp code
1
9791
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
9666
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10408
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10199
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9020
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6769
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5417
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5551
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3700
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2909
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.