// Dackral Phillips // CSE 3700 // 6 September 2K //************************************************************************* // This is a program that imitates the behavior of the game of life coded // in Object Oriented C++. It uses classes to define the cells which // contain the following fields: // State: (1 - alive || 0 - dead) // Array of 8 pointers for nearest neighbors // // The game proceeds on a 6x6 "board" according to the following rules: // // 1) An alive cell with 2 or 3 neighbors, which are alive, remains alive. // 2) A dead cell with 3 neighbors, which are alive, becomes alive. // 3) Otherwise a cell dies or remains dead. //************************************************************************* #include #include // This is the class cell which defines the actual cell class Cell { public: Cell() { nwPtr = NULL; nPtr = NULL; nePtr = NULL; wPtr = NULL; ePtr = NULL; swPtr = NULL; sPtr = NULL; sePtr = NULL; }; // constructor ~Cell() { }; // destructor // Setstate sets the state of a cell (1 || 0) void SetState(int State) { state = State; }; // Getstate returns state of a cell (1 || 0) int GetState() { return state; }; // SetNeighbor() sets the pointer of a cell to the address // of its neighbor void SetNeighbors(Cell cell[6][6], int i, int j); // CountNeighbors returns how many neighbors are alive int CountNeighbors(); // PrintState prints to the screen whether the cell is dead or alive void PrintState() { cout << state; }; private: int state; // holds the state of the cell // pointers to neighboring cells Cell *nwPtr; Cell *nPtr; Cell *nePtr; Cell *wPtr; Cell *ePtr; Cell *swPtr; Cell *sPtr; Cell *sePtr; }; void Cell::SetNeighbors(Cell cell[6][6], int i, int j) { if(i > 0 && j > 0) { nwPtr = &cell[i - 1][j - 1]; } if(i > 0) { nPtr = &cell[i - 1][j]; } if(i > 0 && j < 5) { nePtr = &cell[i - 1][j + 1]; } if(j < 5) { ePtr = &cell[i][j + 1]; } if(i < 5 && j < 5) { sePtr = &cell[i + 1][j + 1]; } if(i < 5) { sPtr = &cell[i + 1][j]; } if(i < 5 && j > 0) { swPtr = &cell[i + 1][j - 1]; } if(j > 0) { wPtr = &cell[i][j - 1]; } } int Cell::CountNeighbors() { int count = 0; if (nwPtr != NULL) { if (nwPtr->GetState() == 1) { count += 1; } } if (nPtr != NULL) { if (nPtr->GetState() == 1) { count += 1; } } if (nePtr != NULL) { if (nePtr->GetState() == 1) { count += 1; } } if (wPtr != NULL) { if (wPtr->GetState() == 1) { count += 1; } } if (ePtr != NULL) { if (ePtr->GetState() == 1) { count += 1; } } if (swPtr != NULL) { if (swPtr->GetState() == 1) { count += 1; } } if (sPtr != NULL) { if (sPtr->GetState() == 1) { count += 1; } } if (sePtr != NULL) { if (sePtr->GetState() == 1) { count += 1; } } return count; } void initState(Cell cell[6][6]); void linkCells(Cell cell[6][6]); void updateStates(Cell cell[6][6]); void printBoard(Cell cell[6][6]); // main() gets the proverbial ball rolling by calling an initialize state // function, and a function that connects all the pointers to their // neighbors. It then iterates through 10 generations of the game, // and pauses after each iteration with a question whether or not to // continue. void main() { Cell cell[6][6]; int i; char again; initState(cell); linkCells(cell); cout << endl << "Generation 1" << endl << endl; printBoard(cell); cout << endl << "Type a character and press [ENTER] to continue: "; cin >> again; for(i = 2; i <= 10; i++) { updateStates(cell); cout << endl << "Generation " << i << endl << endl; printBoard(cell); cout << endl << "Type a character and press [ENTER] to continue: "; cin >> again; } cout << endl; } // initState() sets the initial state of the cells in the array void initState(Cell cell[6][6]) { int i, j, State, seed; for(i = 0; i <= 5; i++) { for(j = 0; j <= 5; j++) { State = (rand() % 2); cell[i][j].SetState(State); } } } // linkCells() makes links to the neighboring cells of each individual // cell void linkCells(Cell cell[6][6]) { int i, j; for(i = 0; i <= 5; i++) { for(j = 0; j <= 5; j++) { cell[i][j].SetNeighbors(cell, i, j); } } } // updateStates() is the function that handles the official rules of the // game and sets up how the subsequent generations will be handled. void updateStates(Cell cell[6][6]) { int i, j, count, mortality; for(i = 0; i <= 5; i++) { for(j = 0; j <= 5; j++) { count = cell[i][j].CountNeighbors(); mortality = cell[i][j].GetState(); if((mortality == 1) && (count == 2 || count == 3)) { cell[i][j].SetState(1); } else if((mortality == 0) && count == 3) { cell[i][j].SetState(1); } else { cell[i][j].SetState(0); } } } } // printBoard() prints out the state of all cells on the board. void printBoard(Cell cell[6][6]) { int i, j; for(i = 0; i <= 5; i++) { for(j = 0; j <= 5; j++) { cell[i][j].PrintState(); } cout << endl; } }