/********************************************** * Attempt at Conway's Game of Life in C * * used on linux for sleep() * */ #include #include #include #include #define COLS 70 #define ROWS 30 #define LIVE_CELL '*' #define DEAD_CELL ' ' void printGrid(); void initializeGrid(); void fillGrid(); void gen(); void junkFunc(); void killCell(int x, int y); void birthCell(int x, int y); void decideFate(int x, int y); int getStat(int x, int y); int isCorner(int x, int y); int isEdge(int x, int y); void hndCorner(int x, int y); void hndEdge(int x, int y); void hndTopEdge(int x, int y); void hndLeftEdge(int x, int y); void hndRightEdge(int x, int y); void hndBottomEdge(int x, int y); void hndMid(int x, int y); void writeChar(int x, int y, char toWrite); HANDLE hOut; COORD Position; void clrscr(); struct cell { char stat; // 1 for alive, 0 for dead char sym; // symbol to display char buds; // number of neighbors } grid[COLS][ROWS]; int dbg = 0; int main() { hOut = GetStdHandle(STD_OUTPUT_HANDLE); CONSOLE_CURSOR_INFO ConCurInf; ConCurInf.bVisible = FALSE; SetConsoleCursorInfo(hOut, &ConCurInf); clrscr(); initializeGrid(); fillGrid(); int w = 0; int speed = 400; char key = '0'; while(key != 'q'){ printGrid(); gen(); printf("Speed: %d ", speed); if(dbg) getchar(); if( kbhit() ) { key = getch(); putchar(key); if(key == '+'){ if(speed > 50){ speed -= 50; } else if (speed > 2){ speed -= 10; } else{ speed = 0; } } if(key == '-'){ if(speed > 50){ speed += 50; } else{ speed += 10; } } } Sleep(speed); // w++; } } void printGrid(){ int i, j; for(i=0; i0){ s--; long l = s * 10; while( l > 0 ){ l = l - (14/2-1); } } } void killCell(int x, int y){ grid[x][y].sym = DEAD_CELL; grid[x][y].stat = 0; } void birthCell(int x, int y){ grid[x][y].sym = LIVE_CELL; grid[x][y].stat = 1; } int getStat(int x, int y){ return grid[x][y].stat; } void decideFate(int x, int y){ int b = grid[x][y].buds; if( !getStat(x,y) && b == 3){ birthCell(x,y); } else if( b < 2 ){ killCell(x,y); } else if( b > 3 ){ killCell(x,y); } } void hndTopEdge(int x, int y){ int n = 0; n += getStat(x-1,ROWS-1); n += getStat(x-1,y); n += getStat(x-1,y+1); n += getStat(x,ROWS-1); n += getStat(x,y+1); n += getStat(x+1,ROWS-1); n += getStat(x+1,y); n += getStat(x+1,y+1); grid[x][y].buds = n; } void hndLeftEdge(int x, int y){ int n = 0; n += getStat(COLS-1,y-1); n += getStat(COLS-1,y); n += getStat(COLS-1,y+1); n += getStat(x,y-1); n += getStat(x,y+1); n += getStat(x+1,y-1); n += getStat(x+1,y); n += getStat(x+1,y+1); grid[x][y].buds = n; } void hndRightEdge(int x, int y){ int n = 0; n += getStat(x-1,y-1); n += getStat(x-1,y); n += getStat(x-1,y+1); n += getStat(x,y-1); n += getStat(x,y+1); n += getStat(0,y-1); n += getStat(0,y); n += getStat(0,y+1); grid[x][y].buds = n; } void hndBottomEdge(int x, int y){ int n = 0; n += getStat(x-1,y-1); n += getStat(x-1,y); n += getStat(x-1,0); n += getStat(x,y-1); n += getStat(x,0); n += getStat(x+1,y-1); n += getStat(x+1,y); n += getStat(x+1,0); grid[x][y].buds = n; } void clrscr() { HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE); COORD coord = {0, 0}; DWORD count; CONSOLE_SCREEN_BUFFER_INFO csbi; GetConsoleScreenBufferInfo(hStdOut, &csbi); FillConsoleOutputCharacter(hStdOut, ' ', csbi.dwSize.X * csbi.dwSize.Y, coord, &count); SetConsoleCursorPosition(hStdOut, coord); } void writeChar(int x, int y, char toWrite){ Position.X = x; Position.Y = y; SetConsoleCursorPosition(hOut, Position); putchar(toWrite); }