home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgLangD.iso / C++-7 / DISK4 / SAMPLES / CPPTUTOR / OOD / LIFEWIN.H$ / LIFEWIN
Encoding:
Text File  |  1991-10-22  |  3.0 KB  |  105 lines

  1. /********************************************************************
  2.  
  3.  FILE: LIFEWIN.H
  4.  
  5.  Defines LifeWin (derived from TextWin).
  6.  
  7. ********************************************************************/
  8.  
  9. #if !defined( _LIFEWIN_H_ )
  10.  
  11. #define _LIFEWIN_H_
  12.  
  13. #include "textwin.h"
  14.  
  15. /********************************************************************
  16.  
  17.  LifeWin
  18.  
  19.  Defines windows that play the game of Life.
  20.  
  21.  The game of Life was invented in 1970 by mathematics professor John
  22.  Horton Conway.
  23.  
  24.  The game of life is based on the following laws:
  25.  
  26.      1. Law of Survival - If a living cell has either two or or three
  27.         neighbors, it survives.
  28.  
  29.      2. Law of Death - A living cell with more than three neighbors
  30.         dies of overcrowding. A living cell with less than two
  31.         neighbors dies of isolation.
  32.  
  33.      3. Law of Birth - A dead cell with exactly three neighbors is born
  34.         in the next generation.
  35.  
  36.  These simple laws result in complex interactions. For example,
  37.  try entering the following patterns:
  38.  
  39.        ■■       ■            ■■                  ■    ■
  40.       ■ ■         ■         ■■       ■■■■■     ■■ ■■■■ ■■
  41.         ■      ■■  ■■■       ■       ■   ■       ■    ■
  42.  
  43.  A LifeWin contains three buttons: CLEAR, which clears the playing
  44.  field; RANDOM, which places a random distribution of live cells on
  45.  the playing field; and NEXT, which calculates the next generation.
  46.  Cells can be added or deleted with the mouse, or by moving the
  47.  cursor with the arrow keys and using the spacebar.
  48.  
  49.  Public Interface:
  50.  
  51.      LifeWin - constructor taking a position, the dimensions of the
  52.          window, and a text buffer for displaying the Life field.
  53.  
  54.      LifeWin - copy constructor.
  55.  
  56.      operator= - assignment operator.
  57.  
  58.  (Redefined from TextWin)
  59.  
  60.      resize - redefined to reposition buttons, and handle scrollbars.
  61.  
  62.      activate - redefined to activate buttons.
  63.  
  64.      deactivate - redefined to deactivate buttons.
  65.  
  66.      paint - redefined to paint Life window.
  67.  
  68.      handleEvent - redefined to check for button presses and give
  69.          new responses to keyboard and mouse events.
  70.  
  71. ********************************************************************/
  72.  
  73. const int LIFEWIN_MINWIDTH = 14;
  74. const int LIFEWIN_MINHEIGHT = 5;
  75.  
  76. const char CELL_CHAR = (char)254;
  77.  
  78. class LifeWin : public TextWin
  79. {
  80. public:
  81.    LifeWin(Point location, int wid, int len, Buffer *sheet );
  82.    LifeWin( const LifeWin &other );
  83.    LifeWin &operator=( const LifeWin &other );
  84.    void resize( int newWidth, int newLength );
  85.    void activate();
  86.    void deactivate();
  87.    void paint( Rect region );
  88.    void handleEvent( const Event &action );
  89.   ~LifeWin();
  90. private:
  91.    void handleKey( const KbdEvent &key );
  92.    void handleMouse( const MouseEvent &action );
  93.    void handlePush( const PushEvent &action );
  94.    void clearField();
  95.    void randomize();
  96.    void nextGeneration();
  97.    void updateField();
  98.  
  99.    PushButton *buttons[3];
  100.    char *currField;
  101.    char *neighborField;
  102. };
  103.  
  104. #endif // _LIFEWIN_H_
  105.