Created
January 6, 2012 02:30
-
-
Save andrewrcollins/1568647 to your computer and use it in GitHub Desktop.
#TJHSST ~ Game of Life
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| "Life" | |
| John Horton Conway's Cellular Automaton | |
| Using meta-blocks | |
| */ | |
| #include <stdio.h> | |
| /* For IBMs only. */ | |
| #include "conio.h" | |
| #include "stdlib.h" | |
| /* The cellular plane's X and Y maximums. | |
| This plane will be a torus. */ | |
| #define MAXGRIDX 78 | |
| #define MAXGRIDY 24 | |
| #define MAXBLOCKX 26 | |
| #define MAXBLOCKY 8 | |
| /* Miscellaneous defines */ | |
| #define newold !oldnew | |
| #define ALIVE 1 | |
| #define DEAD 0 | |
| #define START 0 | |
| int oldnew=START; | |
| int main(argc,argv) | |
| int argc; | |
| char *argv[]; | |
| { | |
| int grid[2][MAXGRIDY][MAXGRIDX],meta[2][MAXBLOCKY][MAXBLOCKX]; | |
| setup(grid); | |
| while(!kbhit()) { | |
| display(grid); | |
| process(grid); | |
| oldnew=newold; | |
| } | |
| } | |
| int setup(grid,meta) | |
| int grid[2][MAXGRIDY][MAXGRIDX],meta[2][MAXBLOCKY][MAXBLOCKX]; | |
| { | |
| int x,y; | |
| for(y=0;y<MAXGRIDY;y++) | |
| for(x=0;x<MAXGRIDX;x++) | |
| grid[oldnew][y][x]=0; | |
| for(y=0;y<MAXGRIDY;y++) | |
| for(x=0;x<MAXGRIDX;x++) | |
| grid[newold][y][x]=!grid[oldnew][y][x]; | |
| for(y=0;y<MAXLOCKY;y++) | |
| for(x=0;x<MAXBLOCKX;x++) | |
| grid[oldnew][y][x]=0; | |
| for(y=0;y<MAXBLOCKY;y++) | |
| for(x=0;x<MAXBLOCKX;x++) | |
| grid[newold][y][x]=!grid[oldnew][y][x]; | |
| } | |
| int neighbors(x,y,grid) | |
| int x,y,grid[MAXGRIDY][MAXGRIDX]; | |
| { | |
| static int leftcnt,middlecnt,rightcnt,oldcell; | |
| int rightx,cnt; | |
| rightx=(x+1)%MAXGRIDX; | |
| if(x==0) { | |
| int cnt,tmpy,leftx; | |
| leftx=(x-1+MAXGRIDX)%MAXGRIDX; | |
| leftcnt=rightcnt=middlecnt=0; | |
| for(cnt=(-1);cnt<2;cnt++) { | |
| tmpy=(y+cnt+MAXGRIDY)%MAXGRIDY; | |
| leftcnt+=grid[tmpy][leftx]; | |
| rightcnt+=grid[tmpy][rightx]; | |
| middlecnt+=grid[tmpy][x]; | |
| } | |
| oldcell=grid[y][x]; | |
| middlecnt-=oldcell; | |
| } | |
| else { | |
| leftcnt=middlecnt+oldcell; | |
| oldcell=grid[y][x]; | |
| middlecnt=rightcnt-oldcell; | |
| rightcnt=0; | |
| for(cnt=(-1);cnt<2;cnt++) | |
| rightcnt+=grid[(y+cnt+MAXGRIDY)%MAXGRIDY][rightx]; | |
| } | |
| return (leftcnt+rightcnt+middlecnt); | |
| } | |
| int process(grid) | |
| int grid[2][MAXGRIDY][MAXGRIDX]; | |
| { | |
| int x,y; | |
| for(y=0;y<MAXGRIDY;y++) | |
| for(x=0;x<MAXGRIDX;x++) { | |
| switch(neighbors(x,y,grid[oldnew])) { | |
| case 0 : | |
| case 1 : | |
| case 4 : | |
| case 5 : | |
| case 6 : | |
| case 7 : | |
| case 8 : | |
| grid[newold][y][x]=DEAD; | |
| break; | |
| case 2 : | |
| grid[newold][y][x]=grid[oldnew][y][x]; | |
| break; | |
| case 3 : | |
| grid[newold][y][x]=ALIVE; | |
| break; | |
| } | |
| } | |
| return 0; | |
| } | |
| int display(grid) | |
| int grid[2][MAXGRIDY][MAXGRIDX]; | |
| { | |
| int x,y,lx=-1,ly=-1; | |
| for(y=0;y<MAXGRIDY;y++) { | |
| for(x=0;x<MAXGRIDX;x++) | |
| if(grid[oldnew][y][x]!=grid[newold][y][x]) { | |
| if(lx!=x||ly!=y) | |
| gotoxy((lx=x)+1,(ly=y)+1); | |
| switch(grid[oldnew][y][x]) { | |
| case ALIVE : | |
| putchar((char)254); | |
| break; | |
| case DEAD : | |
| putchar((char)250); | |
| break; | |
| } | |
| } | |
| } | |
| return 0; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| "Life" | |
| John Horton Conway's Cellular Automaton | |
| */ | |
| #include <stdio.h> | |
| /* For IBMs only. */ | |
| #include "conio.h" | |
| #include "stdlib.h" | |
| /* The cellular plane's X and Y maximums. | |
| This plane will be a torus. */ | |
| #define MAXGRIDX 79 | |
| #define MAXGRIDY 25 | |
| /* Miscellaneous defines */ | |
| #define newold !oldnew | |
| #define ALIVE 1 | |
| #define DEAD 0 | |
| #define START 0 | |
| int oldnew=START; | |
| int main(argc,argv) | |
| int argc; | |
| char *argv[]; | |
| { | |
| int grid[2][MAXGRIDY][MAXGRIDX]; | |
| setup(grid); | |
| while(!kbhit()) { | |
| display(grid); | |
| process(grid); | |
| oldnew=newold; | |
| } | |
| } | |
| int setup(grid) | |
| int grid[2][MAXGRIDY][MAXGRIDX]; | |
| { | |
| int x,y; | |
| for(y=0;y<MAXGRIDY;y++) | |
| for(x=0;x<MAXGRIDX;x++) | |
| grid[oldnew][y][x]=0; | |
| grid[oldnew][15][15]=1; | |
| grid[oldnew][15][16]=1; | |
| grid[oldnew][15][17]=1; | |
| grid[oldnew][14][16]=1; | |
| grid[oldnew][16][17]=1; | |
| for(y=0;y<MAXGRIDY;y++) | |
| for(x=0;x<MAXGRIDX;x++) | |
| grid[newold][y][x]=!grid[oldnew][y][x]; | |
| } | |
| int neighbors(x,y,grid) | |
| int x,y,grid[MAXGRIDY][MAXGRIDX]; | |
| { | |
| static int leftcnt,middlecnt,rightcnt,oldcell; | |
| int rightx,cnt; | |
| rightx=(x+1)%MAXGRIDX; | |
| if(x==0) { | |
| int cnt,tmpy,leftx; | |
| leftx=(x-1+MAXGRIDX)%MAXGRIDX; | |
| leftcnt=rightcnt=middlecnt=0; | |
| for(cnt=(-1);cnt<2;cnt++) { | |
| tmpy=(y+cnt+MAXGRIDY)%MAXGRIDY; | |
| leftcnt+=grid[tmpy][leftx]; | |
| rightcnt+=grid[tmpy][rightx]; | |
| middlecnt+=grid[tmpy][x]; | |
| } | |
| oldcell=grid[y][x]; | |
| middlecnt-=oldcell; | |
| } | |
| else { | |
| leftcnt=middlecnt+oldcell; | |
| oldcell=grid[y][x]; | |
| middlecnt=rightcnt-oldcell; | |
| rightcnt=0; | |
| for(cnt=(-1);cnt<2;cnt++) | |
| rightcnt+=grid[(y+cnt+MAXGRIDY)%MAXGRIDY][rightx]; | |
| } | |
| return (leftcnt+rightcnt+middlecnt); | |
| } | |
| int process(grid) | |
| int grid[2][MAXGRIDY][MAXGRIDX]; | |
| { | |
| int x,y; | |
| for(y=0;y<MAXGRIDY;y++) | |
| for(x=0;x<MAXGRIDX;x++) { | |
| switch(neighbors(x,y,grid[oldnew])) { | |
| case 0 : | |
| case 1 : | |
| case 4 : | |
| case 5 : | |
| case 6 : | |
| case 7 : | |
| case 8 : | |
| grid[newold][y][x]=DEAD; | |
| break; | |
| case 2 : | |
| grid[newold][y][x]=grid[oldnew][y][x]; | |
| break; | |
| case 3 : | |
| grid[newold][y][x]=ALIVE; | |
| break; | |
| } | |
| } | |
| return 0; | |
| } | |
| int display(grid) | |
| int grid[2][MAXGRIDY][MAXGRIDX]; | |
| { | |
| int x,y,lx=-1,ly=-1; | |
| for(y=0;y<MAXGRIDY;y++) { | |
| for(x=0;x<MAXGRIDX;x++) | |
| if(grid[oldnew][y][x]!=grid[newold][y][x]) { | |
| if(lx!=x||ly!=y) | |
| gotoxy((lx=x)+1,(ly=y)+1); | |
| switch(grid[oldnew][y][x]) { | |
| case ALIVE : | |
| putchar((char)254); | |
| break; | |
| case DEAD : | |
| putchar((char)250); | |
| break; | |
| } | |
| } | |
| } | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment