Important alert: (current site time 7/15/2013 6:49:04 PM EDT)
 

VB icon

The Game Of Life(one dimensional)

Email
Submitted on: 10/17/1999
By: Zoe Edington (full name) 
Level: Not Given
User Rating: By 2 Users
Compatibility: C, C++ (general)
Views: 26984
 
     This is a single dimensional Game of Life. The user inputs coordinates for living cells. Using the rules of the Game of Life the program determines if a cell will live or die. It will calculate for any amount of generations. It was developed in MS Visual C++ and uses an input file.
 

INCLUDE files:

Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
//**************************************
//INCLUDE files for :The Game Of Life(one dimensional)
//**************************************
Additional Header Files:
Common.h:
#ifndef COMMON_H
#define COMMON_H
#include <stdio.h>
#include <stdlib.h>
typedef enum boolean { FALSE, TRUE } Boolean;
void Error(char *);
void Warning(char *);
 /* Error: report program error.
Pre: s points to the message to be printed.
Post: The function prints the message and terminates the program.
 */
void Error(char *s)
{
 fprintf(stderr, "%s\n", s);
 exit(1);
}
/*
Pre: s points to the message to be printed.
Post: The function prints the message.
 */
void Warning(char *s)
{
 fprintf(stderr, "%s\n", s);
}
#endif
Life.h
#define MAXCOL 60 /* maximum column range */
typedef enum state { DEAD, ALIVE } State;
typedef State Grid[MAXCOL+2];
FILE *inputs;
FILE *outputs;
void CopyMap(Grid map, Grid newmap);
void UserSaysYes(Grid map, Grid newmap);
void Initialize(Grid map);
int NeighborCount(Grid map, int column);
void WriteMap(Grid map);
void NextGeneration(Grid map,Grid newmap);
int BoundsMap(Grid map,int i);
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
//**************************************
// Name: The Game Of Life(one dimensional)
// Description:This is a single dimensional Game of Life. The user inputs coordinates for living cells. Using the rules of the Game of Life the program determines if a cell will live or die. It will calculate for any amount of generations. It was developed in MS Visual C++ and uses an input file.
// By: Zoe Edington (full name)
//
//This code is copyrighted and has// limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=7&lngWId=3//for details.//**************************************

/* Simulation of Conway's game of Life on a bounded grid in 
one dimension.
Name: J. Edgington Date: 09/08/99
Pre: The user must supply an initial configuration of living cells.
Post: The program prints a sequence of maps showing the changes in
the configuration of living cells according to the rules for the
game of Life.
Uses: functions Initialize, WriteMap, NeighborCount, NextGeneration, 
CopyMap, and UserSaysYes
 */
#include "stdafx.h"
#include "common.h" /* common include files and definitions */
#include "life.h"/* Life's defines, typedefs, and prototypes */
void main(void)
{
 int col;
 Grid map;/* current generation */
 Grid newmap;/* next generation*/
 if(( inputs = fopen( "input.dat", "r" ))== NULL )
 fprintf( outputs,"The file input.dat was not opened\n" );
 if(( outputs = fopen( "output.dat", "w" )) == NULL )
 fprintf( outputs,"The file ouput.dat. was not opened\n" );
 else
 {
 col=99;
 Initialize(map);
 WriteMap(map);
fprintf(outputs,"[]This is the initial configuration you have chosen.[]");
	NextGeneration(map,newmap);
 CopyMap(map, newmap);
 WriteMap(map);
fprintf(outputs,"[]Do you wish to continue viewing the new generations(y,n)?[]");
	}
 UserSaysYes(map, newmap);
 fclose( outputs );
	fclose( inputs );
}
/* NeighborCount: count neighbors of row,col.
Pre: The col is a valid cell in a Life configuration.
Post: The function returns the number of living neighbors of the living cell.
 */
int NeighborCount(Grid map, int col)
{
 int i=0; /* column of a neighbor of the cell (col) */ 
 int count = 0;/* counter of living neighbors */
 for (i = col - 2; i <= col + 2; i++)
 
	if (BoundsMap(map,i) == ALIVE)
count++;
 if (map[col] == ALIVE)
count--;
 return count;
}
/*pre:index of array to look at. example:3
post:ALIVE or DEAD
 Also handles case of index < 1 or index > MAXCOL 
(i.e. out of bounds)*/
int BoundsMap(Grid map,int i)
{
	if (i<1 || i>MAXCOL)
		return DEAD;
	else
		return map[i];
}
/* Initialize: initialize grid map.
Pre: None.
Post: All the cells in the grid map have been set to
 initial configuration of living cells.
 */
void Initialize(Grid map)
{
 int col=0; /* coordinates of a cell */
	
 fprintf(outputs,"[]This program is a simulation of the game of Life.[]"
 "[]The grid has a size of %d columns.[]", MAXCOL);
 fprintf(outputs,"[]On each line give an index for a living cell.[]"
	 "[]Terminate the list with the special index 0.[]");
	
 for (col = 0; col <= MAXCOL + 1; col++)
 {
 map[col] = DEAD; /* Set all cells empty, including the hedge. */
 }
 fscanf( inputs, "%d", &col );
 do 
 {
 
 if ( col >= 1 && col <= MAXCOL)
map[col] = ALIVE;
 else
	 fprintf(outputs,"[]Values are not within range.[]");
fscanf(inputs,"%d", &col);
 }
 while ( col != 0); /* Checks for termination condition. */
}
/* WriteMap: display grid map.
Pre: The single line array map contains the current Life configuration.
Post: The current Life configuration is written for the user.
*/
void WriteMap(Grid map)
{
 int col;
 char a='*',d='-';
 for (col = 1; col <= MAXCOL; col++)
 {
	 if (map[col] == ALIVE)
 	fprintf(outputs,"%c",a);
else
	 fprintf(outputs,"%c",d);
 }	
 
}
/* CopyMap: copy newmap into map.
Pre: The grid newmap has the current Life configuration.
Post: The grid map has a copy of newmap.
 */
void CopyMap(Grid map, Grid newmap)
{
 int col;
 for (col = 0; col <= MAXCOL + 1; col++)
map[col] = newmap[col];
}
/* UserSaysYes: TRUE if execution is to continue.
Pre: None.
Post: determines if user wants to get another generation of cells.
 */
void UserSaysYes(Grid map,Grid newmap)
{
 char c;
 
 do 
 {
 fscanf(inputs, "%c", &c );
 if (c!= 10 )
	{
	if (c != 'n')
	 {
	 NextGeneration(map,newmap);
	 CopyMap(map, newmap);
	 WriteMap(map);
		fprintf(outputs,"Do you wish to continue viewing "
		"the new generations[]");
	}
	}
	else
	 c='y';
 }
 
 while (c == 'y' || c == 'Y');
}
/* NextGeneration
Pre: None
Post: Calculates the cells in the next generation.
*/
void NextGeneration(Grid map, Grid newmap)
{
 int col;
 
 for (col = 1; col <= MAXCOL; col++)
	
 switch(NeighborCount(map, col)) 
	{
 case 0:
 case 1:
	 newmap[col]= DEAD;
break;
 case 2:
newmap[col]= ALIVE;
break;
 case 3:
	 if (map[col] == ALIVE)
	 newmap[col]=DEAD;
	 else
		newmap[col]= ALIVE;
	 break;
 case 4:
	 if (map[col] == ALIVE)
		newmap[col]=ALIVE;
	 else
		newmap[col]= DEAD;
break;
 }
}


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Not Given category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments

5/16/2001 8:24:00 PMBruce

Hi, I am wondering why after copying and pasting the code in Visual C++ 6.0, it did not compile. I am new to programming and want to see how this game runs. What did I do wrong?
Please advice, my email is psbforever@yahoo.com

Thank you very much
(If this comment was disrespectful, please report it.)

 
5/14/2002 1:54:22 AMsachin

hi,

Along with the code a sample input.dat should also be provided.As a new user dont know about this file,he will not be having that file and will definately get an exception error.
As per the code in function main(), if the file input.dat doesnt exist the code is writting the error in output.dat file which is not yet opened.
So an exception is thrown!

Thanks

(If this comment was disrespectful, please report it.)

 
6/21/2002 9:15:41 PMWizardAKaol.com

you forgot the stdafx.h header file in your code
(If this comment was disrespectful, please report it.)

 
11/7/2002 8:16:38 PMnhsxth

If it's one-dimensional, it's not the game of life. Simple as that.
(If this comment was disrespectful, please report it.)

 
5/23/2003 1:24:36 AM

Ummm...looks like a great program.....just too bad it doesn't even compile because you didn't include the code for the "common.h" and "life.h" files. It doesn't work without these files.
(If this comment was disrespectful, please report it.)

 
2/4/2006 1:02:06 PMTom

This is at least two-dimensional.
One-dimensional doesn't really exist. It most closely resembles a line with an unlimited small thickness.
(If this comment was disrespectful, please report it.)

 
6/20/2007 12:49:07 PMRayan D.

He did put the common.h and the life.h header files. And I think it is quite code. And folks, he did when the contest . So he is better than you. and me for that matter
(If this comment was disrespectful, please report it.)

 
6/20/2007 12:50:16 PMRayan D.

she* srry
(If this comment was disrespectful, please report it.)

 
6/20/2007 12:50:29 PMRayan D.

she* srry

(If this comment was disrespectful, please report it.)

 
4/30/2008 5:24:00 AMbwalya greg

the stdafx.h header file is missing the program cannot compile without it.
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.