//life-2_nt.cpp //(c) 1997, Oguz Yetkin //physics 505 //revised 2/1998 for phys499 // //Implements "academic life" where cells interact with "colleagues" //otherwise, rules are the same as life (unless otherwise specified) // //Game of life, and other good stuff // //version 2: self-neighbor not allowed 2/18/1998 //#include "graph.h" //#include //will have to use borland c++ graphics directly :( //for getpixel and putpixel //define one of these for appropriate platform!!! //#define DOS 1 #define UNIX 1 #define FORCELOCAL 1 //define this to turn the game into the regular LIFE //#define FORCELOCAL 0 #ifdef DOS //for windows //include //for windows sleep(), MUST ALSO LINK kernel32.lib //#include //#include //#define CLEARCMD "cls" //command to clear the screen const unsigned char DEAD=255; //space const unsigned char LIVE=221; //white block void SetGraphicsMode( void ) { __asm { //mov ax,0x13 mov eax, 13h //int 0x10 int 10h } } void SetTextMode( void ) { __asm { //mov ax,0x03 mov eax, 3h //int 0x10 int 10h } } #endif #ifndef DOS const char DEAD='-'; const char LIVE='*'; #endif #ifdef UNIX #define CLEARCMD "clear" //command to clear the screen under UNIX #endif #include #include #include //for exit #include //#include //for sound //change to 1 in order to make it legal for self to be a neighbor //#define SELF_NEIGHBOR 0 //#include const int GEN=2; //number of generations to keep+ 1 reserved layer //(the GENth layer) for neighbor index //const int I=70; //const int J=70; const int I=75; //to fit on screen const int J=48 ; const int NEIGHBORS_PER_CELL=8; //we are no longer limited by geometry //#define NUM_CELLS I * J const int NUM_CELLS=I*J; const int offset=20; //offset to plot struct point{ int i; int j; }; struct neighlist{ point list[NEIGHBORS_PER_CELL]; }; //const int ITER=300; //remains unchanged in academic life //GLOBAL FUNCTIONS inline int fate(int value,int num_neighbors); //return 0 or 1 inline int count_neighbor(neighlist neighbors[NUM_CELLS], int array[GEN+1][I][J], int i, int j,int gen); //assume I and J there int count_alive(int world[GEN+1][I][J],int gen); //how many alive? void dump(int world[GEN+1][I][J],int gen); //dump as text void dump2(int world[GEN+1][I][J], int world2[GEN+1][I][J],int gen); //dump 2 worlds side by side /* GLOBALS */ extern void Sleep(double); //defined in winbase.h, including it creates problems double sleepvalue=0; double sleepstep=200; char ch='p'; //start out paused int main(int argc, char** argv){ int visual_output=0; //SetGraphicsMode(); int self_neighbor_legal=0; int world[GEN+1][I][J]; //2 2-d arrays in a 3-d array //the extra layer (layer 3) is for neighbor index int world2[GEN+1][I][J]; //parallel universe neighlist neighbors[NUM_CELLS]; //virtual "neighbors" can be anywhere //on the grid (more like colleagues) //neighlist neighbors2[NUM_CELLS]; long int neighboridx; int gen=0; //either 0 or 1 int iter=0; int curr_generation=0; int randx, randy, thisx, thisy; //temp variables to hold randomly generated values int living_count=0; int living_count2=0; int maxcolor; long int i,j,k,m,n,o; //for iterating up to millions char ch; float percentage, percentage2; //for calculting # of cells alive cout<<"\nAcadLife 2.0, by Oguz Yetkin, oguz@writeme.com"; cout<<"\npress q to stop the program"; cout<<"\nThis is a non-straighforward implementation of LIFE,"; cout<<"\nthis version (2.0), can preclude self as a neighbor"; cout<<"\nThe code is probably on"; cout<<"\nhttp://www.cs.wisc.edu/~yetkin/code/life\n"<>iter; cout<<"\nare self-neighbors legal? (y/n) "; cin>>ch; if(ch=='y'){ self_neighbor_legal=1; }else{ self_neighbor_legal=0; } cout<<"\ndisplay visual output? (y/n)"; cin>>ch; if(ch=='y'){ visual_output=1; }else{ visual_output=0; } //SetTextMode(); int maxx,maxy; //initialize graphics //************************************ /* int driver=DETECT; int mode; registerbgidriver(EGAVGA_driver); //int maxx, maxy; //will be 640x480 //**************INIT GRAPHICS********************** initgraph(&driver, &mode, ""); //driver linked in //mode=CGAC0; //initgraph(&driver, &mode,"h:\\borlandc\\bgi"); maxx=getmaxx(); maxy=getmaxy(); maxcolor=getmaxcolor(); //cout<0){ sleepvalue-=sleepstep; if(sleepvalue<0){ sleepvalue=0; //just in case } } break; case 'p': //cout<>ch; /* commented out VERY temporarily ??? if(curr_generation%100==0){ cout<<"\ngeneration: "<