/* A sample object oriented program for Conway's Game of Life using C++ File: life1.cc Date: Sun Nov 6 12:22:13 PST 1994 Author: Richard J Botting */ /* ANALYSIS The natural classes in this project are cell_state, cell, board, and possibly user BECAUSE they are nouns in the spec and refer to identifiable entities. In a simple problem the user doesn't have a very interesting history and only occurs once per program. Hence the user does not need to be a class or data type. Refinement: cell_state Two values alive and dead, // more a type than a class cell Has a cell_state identified by line and row in board initially dead can come alive can die may be seen to be alive or dead // definitely a class board Has boundaries and many cells identified by the time it occurs change state of cell (if inside the boundary) can find state of cell (assumed dead on or outside boundary) can count the number of alive neighbors(inside!) (The above let other functions forget about the boundary) can change according to the rules of Life can be copied from another board (=) can be compared with another board can be input by the user can be shown to the user can be filled with random patterns(perhaps?) can be saved in a file(later prototype) can be loaded from a file(later prototype) // definitely a class The remaining entity (the user) is best modelled by the main function...unless you want a multi-user life-game server. Note. The idea of a nieghbor or a boundary can not exist except in the context of a board... So I've associated them with board not cell. Decision: it might simplify maintenance to separate out a model of the cells and boards as viewed by the user but I chose not to. Decision: I could save space by coding both the current value and the next value into a single cell. This takes more time and moves away from the goal of showing a simple OO design. Decision: I could have a dynamic list of cells that are about to be alive instead of a second board. This may be faster and save storage. It complicates the solution. */ #include #include #include //Small board to allow simple testing #define LINES 9 #define COLS 9 /* Following are more typical #define LINES 23 #define COLS 78 */ enum cellstate{ dead, alive } ; //C++ simplifies the definition of types char outmap[]={'.', 'X'}; //laziness and amendability class cell { // a structure with data and functions private: cellstate mystate; public: cell(void){ mystate=dead; }// initially dead void born(void){mystate=alive; };// can come alive void die(void){mystate=dead; }// can die cellstate state(void){return mystate%2;}// alive or dead };//end cell class board { private: cell b[LINES][COLS]; int inside(int r, int c){ return 0>(istream& s, board &b); //can be input by the user //can be filled with random patterns(perhaps?) //can be saved in a file(later prototype) //can be loaded from a file(later prototype) };//end board int more() { int ch; cout<<"More?[Y/N] ";flush(cout); while((ch=getchar())!='\n' && ch!='Y' && ch!='y' && ch!='n' && ch!='N' && ch!=EOF); return(ch=='\n' || ch=='Y' || ch=='y'); } int main(void) { board b0, b1; cin >> b0; system("clear"); cout<< b0; while(more()) { b1=b0.next(); system("clear"); cout<< b1; b0=b1; } exit(0); } ostream& operator<<(ostream& s, board b) { int r,c; for(c=0;c>(istream& s, board &b) { int r,c; char ch; cout <<"\nInput a board:\n"<<' '; for(c=1;c>olb; while(some_condition) { use *old to find *new swap old and new print } */