/* This header file defines a class that can be used to represent boards for the "Game of Life". A board consists of a rectangular grid of "cells". Each cell can be either alive or dead. Given a current board configuration there are rules for computing the next "generation" of cells. */ #include const int ROWS = 25; // The number of rows in the grid of cells. const int COLS = 75; // The number of columns in the grid of cells. class LifeBoard { bool data[ROWS+2][COLS+2]; // This array represents the grid of cells, plus a one-cell // border of dead cells around the edge of the board. // The value of data[r][c] is true if the cell in row r // column c is alive, and is false if that cell is dead. Rows // are numbered from 1 to ROWS. Columns are numbered from // 1 to COLS. public: LifeBoard(); // Constructor creates an initially empty life board. void set(int row, int col, bool alive); // Precondition: 1 <= row <= ROWS and 1 <= col <= COLS. // Sets the state of the cell in the specified row and column. bool get(int row, int col); // Precondition: 1 <= row <= ROWS and 1 <= col <= COLS. // Gets the state of the cell in the specified row and column. // The return value is true for an alive cell and is false // for a dead cell. void randomFill(double fillDensity); // Precondition 0 <= fillDensity <= 1. // Fills the board with random values. The parameter gives // the probability that a given cell is alive. A fillDensity // of 0 will give an empty board; a fillDensity of 0.5 will // give a half-empty board; and so on. void clear(); // Makes all the cells dead. void nextGeneration(); // This function computes the next generation of cells, using // the rules of the game of life, and changes the board to // show the new generation. bool readFromStream(istream &input); // Tries to read the contents of a board from a stream. The // stream must contain board data in the same format that is // written by the writeToStream function. If any error occurs // while reading the data, the return value of the function will // be false and the board WILL NOT BE CHANGED. A return value // of true indicates that a new value was read successfullly. // Note that the parameter can, in particular, be an ifstream. bool writeToStream(ostream &output); // Writes the contents of the board onto the steam in the // following format: The first line contains the number of // rows and the number of columns, separated by a space. // After that, there is one line for each row of the board. // On this line, a living cell is represented by a '*' // charactder and a dead cell is represented by a '.'. // The return value is false if some error occurs while // trying to write the data. Note that the parameter can, // in particular, be an ofstream. };