single,dimensional,Game,Life,user,inputs,coor
Quick Search for:  in language:    
single,dimensional,Game,Life,user,inputs,coor
   Code/Articles » |  Newest/Best » |  Community » |  Jobs » |  Other » |  Goto » | 
CategoriesSearch Newest CodeCoding ContestCode of the DayAsk A ProJobsUpload
C/ C++ Stats

 Code: 451,578 lines
 Jobs: 558 postings

 
Sponsored by:

 

You are in:

 
Login



Latest Code Ticker for C/ C++.
Url Test
By Richard Creary on 8/27


Nice Console Calc
By Andrew Carter on 8/26


Visual Pi Hex 2
By jo122321323 on 8/26


Cascade Clone v1.0
By Paulo Jorente - aka JungleBoy on 8/26


Bubble Sort Algo
By d1rtyw0rm on 8/26


Copy File I/O
By Olivier Bastien on 8/25


Visual Pi Hex
By jo122321323 on 8/25


Click here to see a screenshot of this code!ShortCutSample
By Massimiliano Tomassetti on 8/25

(Screen Shot)

AnPMoneyManager beta
By Anthony Tristan on 8/24


Click here to put this ticker on your site!


Add this ticker to your desktop!


Daily Code Email
To join the 'Code of the Day' Mailing List click here!





Affiliate Sites



 
 
   

The Game Of Life(one dimensional)

Print
Email
 
VB icon
Submitted on: 10/17/1999
By:  
Level: Not Given
User Rating: By 104 Users
Compatibility:C, C++ (general)

Users have accessed this code 8780 times.
 
 
     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(on
//     e 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 dimensiona
//     l)
// Description:This is a single dimensio
//     nal Game of Life. The user inputs coordi
//     nates for living cells. Using the rules 
//     of the Game of Life the program determin
//     es if a cell will live or die. It will c
//     alculate for any amount of generations. 
//     It was developed in MS Visual C++ and us
//     es an input file.
// By: 
//
//This code is copyrighted and has// limited warranties.Please see http://
//     www.Planet-Source-Code.com/xq/ASP/txtCod
//     eId.7/lngWId.3/qx/vb/scripts/ShowCode.ht
//     m//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 notify us if this entry should be deleted (i.e contains no code, is a virus, etc.).
Reason:
 
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 PM:Bruce
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
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
5/14/2002 1:54:22 AM:sachin
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 
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
6/21/2002 9:15:41 PM:WizardAKaol.com
you forgot the stdafx.h header file in 
your code
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
Add Your Feedback!
Note:Not only will your feedback be posted, but an email will be sent to the code's author in your name.

NOTICE: The author of this code has been kind enough to share it with you.  If you have a criticism, please state it politely or it will be deleted.

For feedback not related to this particular code, please click here.
 
Name:
Comment:

 

Categories | Articles and Tutorials | Advanced Search | Recommended Reading | Upload | Newest Code | Code of the Month | Code of the Day | All Time Hall of Fame | Coding Contest | Search for a job | Post a Job | Ask a Pro Discussion Forum | Live Chat | Feedback | Customize | C/ C++ Home | Site Home | Other Sites | About the Site | Feedback | Link to the Site | Awards | Advertising | Privacy

Copyright© 1997 by Exhedra Solutions, Inc. All Rights Reserved.  By using this site you agree to its Terms and Conditions.  Planet Source Code (tm) and the phrase "Dream It. Code It" (tm) are trademarks of Exhedra Solutions, Inc.