#include #include #include /* * nearest neighbor computation. * 2 or 3 cells remain alive * 3 cells generate new life * Other die. * * Perodic boundry conditions. * */ using namespace std; const int MAX_ROWS = 20; const int MAX_COLS = 40; const float LIFE_CHANCE = .3; bool DEBUG = false; void InitBoard(int board[][MAX_COLS], int rows, int cols); void InitBoard1(int board[][MAX_COLS], int rows, int cols); void PrintBoard(int board[][MAX_COLS], int rows, int cols); void UpdateBoard(int board1[][MAX_COLS],int board2[][MAX_COLS], int rows, int cols); void CopyBoard(int board1[][MAX_COLS],int board2[][MAX_COLS], int rows, int cols); int NeighborCount(int board[][MAX_COLS], int rows, int cols, int r, int c); int main (int argc, char * argv[]) { int board1[MAX_ROWS][MAX_COLS]; int board2[MAX_ROWS][MAX_COLS]; int rows = MAX_ROWS, columns = MAX_COLS; int turn=0; bool randInit = true; int i; i = 1; while(i < argc) { cout << "Looking at argv[i] = " << argv[i] << endl; if (!strcmp(argv[i],"-x")){ if (i+1 < argc) { i++; columns = atoi(argv[i]); cout << "Changing columns to be " << columns << endl; } i++; } else if (!strcmp(argv[i],"-y")){ if (i+1 < argc) { i++; rows = atoi(argv[i]); cout << "Changing rows to be " << rows << endl; } i++; } else if (!strcmp(argv[i],"-v")){ cout << "Settnig debug to true " << endl; DEBUG = true; i++; } else if (!strcmp(argv[i],"-r")){ cout << "setting andom init false" << endl; randInit = false; i++; } else { cout << "Unknown Argument " << argv[i] << endl; i++; } } cout << "Playing LIFE" << endl; cout << "with rows = " << rows << " cols = " << columns << endl; srand(time(NULL)); if (randInit) { InitBoard(board1, rows, columns); } else { InitBoard1(board1, rows, columns); } PrintBoard(board1, rows, columns); for(turn=0;;turn++) { UpdateBoard(board1, board2, rows, columns); cout << "Turn " << turn << endl; PrintBoard(board2, rows, columns); CopyBoard(board2, board1, rows, columns); } } void InitBoard(int board[][MAX_COLS], int rows, int cols){ int i,j; for(i=0;i1 && cols > 1) { board[0][0] = 1; board[0][1] = 1; board[1][0] = 1; board[1][1] = 1; } // a blinker if (rows > 5 && cols > 7) { board[5][5] = 1; board[5][6] = 1; board[5][7] = 1; } // a glider if(rows > 10 && cols > 3) { board [10][1] = 1; board [10][2] = 1; board [10][3] = 1; board [9][3] = 1; board [8][2] = 1; } return ; } void BlankLine(int cols) { int j; cout << "+"; for(j=0;j 0){ cout << "#"; } else { cout << " "; } } cout << "|"; cout << endl; } BlankLine(cols); return; } int FixCoord(int value, int max) { if (value < 0) { value = max-1; } else if (value >= max) { value = 0; } return value; } int GetCell(int board[][MAX_COLS], int rows, int cols, int r, int c) { r = FixCoord(r, rows); c = FixCoord(c, cols); return board[r][c]; } int NeighborCount(int board[][MAX_COLS], int rows, int cols, int r, int c){ int count = 0; int i,j; for(i=-1;i<2;i++) { for(j=-1;j<2;j++) { if (GetCell(board, rows,cols,r+i,c+j) > 0) { count ++; } } } if (board[r][c] > 0) { count --; } return count; } void UpdateBoard(int board1[][MAX_COLS], int board2[][MAX_COLS], int rows,int cols){ int neighbors; int i,j; for(i=0;i 0 && DEBUG) { cout << "cell " << i << ", " << j << " has " << neighbors << " neighbors " << endl; } if (neighbors < 2) { board2[i][j] = 0; } else if (neighbors == 2 && board1[i][j] != 0) { board2[i][j] = board1[i][j] + 1; } else if (neighbors == 3) { board2[i][j] = board1[i][j] + 1; } else { board2[i][j] = 0; } } } return; } void CopyBoard(int board1[][MAX_COLS], int board2[][MAX_COLS], int rows, int cols){ int i,j; for(i=0;i