/* A simple, command-line version of life, using the life board defined in LifeBoard.h. User can give single-letter commands to eXit, compute the Next generation, Fill the board randomly at 50% density, Read the board from a file, or Write the board to a file. The board is displayed after any operation that changes it. When the program starts, the board is filled with some simple demo patterns. */ #include "LifeBoard.h" #include #include #include #include /* Neatly print a LifeBoard to standard output. * Note: this is assuming that the board is 75-by-25, * which is really not very good programming practice. */ void printBoard(LifeBoard &life) { cout.put('+'); for (int i = 1; i <= 75; i++) cout.put('-'); cout.put('+'); cout.put('\n'); for (int r = 1; r <= 25; r++) { cout.put('|'); for (int c = 1; c <= 75; c++) cout.put( life.get(r,c)? '*' : ' ' ); cout.put('|'); cout.put('\n'); } cout.put('+'); for (int i = 1; i <= 75; i++) cout.put('-'); cout.put('+'); cout.put('\n'); } /* Add a few living cells to the board, to set up * a simple initial configuration for this program. */ void makeInitialBoard(LifeBoard &board) { board.set(5,5,true); // Three-cell blinker board.set(5,6,true); board.set(5,7,true); board.set(15,7,true); // Glider board.set(15,8,true); board.set(15,9,true); board.set(16,9,true); board.set(17,8,true); board.set(5,30,true); // Static block board.set(5,31,true); board.set(6,30,true); board.set(6,31,true); board.set(12,50,true); // Evolves to period-three blinker board.set(12,51,true); board.set(12,52,true); board.set(12,53,true); board.set(12,54,true); board.set(12,56,true); board.set(12,57,true); board.set(12,58,true); board.set(12,59,true); board.set(12,60,true); } int main() { LifeBoard board; // The board used for the game. makeInitialBoard(board); srand(time(0)); // Initialize random number generator. cout << "\n\n"; printBoard(board); while (true) { char ch; // Command read from user. cout << "\n\nX = Exit; N = NextGen; F = Fill; R = Read; W = Write: "; cin >> ch; cin.ignore(10000,'\n'); if (ch == 'X' || ch == 'x') // Exit. break; else if (ch == 'N' || ch == 'n') // Compute next generation. board.nextGeneration(); else if (ch == 'F' || ch == 'f') // Randomly fill board. board.randomFill(0.5); else if (ch == 'R' || ch == 'r') { // Read from file. string fileName; ifstream file; cout << "Input file name: "; cin >> fileName; cin.ignore(10000,'\n'); file.open(fileName.c_str()); if (!file) { cout << "\n*** Error: Can't open file.\n\n"; continue; } bool test = board.readFromStream(file); if (!test) { cout << "\n*** Error while trying to read from file.\n\n"; continue; } } else if (ch == 'W' || ch == 'w') { // Write to file. string fileName; ofstream file; cout << "Output file name: "; cin >> fileName; cin.ignore(10000,'\n'); file.open(fileName.c_str()); if (!file) { cout << "\n*** Error: Can't open file.\n\n"; continue; } bool test = board.writeToStream(file); if (!test) { cout << "\n*** Error while trying to write to file.\n\n"; continue; } } else { cout << "\nIllegal command; please try again.\n"; continue; } cout << "\n\n"; printBoard(board); } }