Split the sample in three source files + three header files, for improved
[wxWidgets.git] / demos / life / game.h
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: game.h
3 // Purpose: Life! game logic
4 // Author: Guillermo Rodriguez Garcia, <guille@iies.es>
5 // Modified by:
6 // Created: Jan/2000
7 // RCS-ID: $Id$
8 // Copyright: (c) 2000, Guillermo Rodriguez Garcia
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifndef _LIFE_GAME_H_
13 #define _LIFE_GAME_H_
14
15 #ifdef __GNUG__
16 #pragma interface "game.h"
17 #endif
18
19 // for compilers that support precompilation, includes "wx/wx.h"
20 #include "wx/wxprec.h"
21
22 #ifdef __BORLANDC__
23 #pragma hdrstop
24 #endif
25
26 // for all others, include the necessary headers
27 #ifndef WX_PRECOMP
28 #include "wx/wx.h"
29 #endif
30
31 // --------------------------------------------------------------------------
32 // constants
33 // --------------------------------------------------------------------------
34
35 // minimum and maximum table size, in each dimension
36 #define LIFE_MIN 20
37 #define LIFE_MAX 200
38
39 // --------------------------------------------------------------------------
40 // Cell and CellArray;
41 // --------------------------------------------------------------------------
42
43 typedef long Cell;
44 typedef wxArrayLong CellArray;
45
46 // --------------------------------------------------------------------------
47 // LifeShape
48 // --------------------------------------------------------------------------
49
50 class LifeShape
51 {
52 public:
53 LifeShape::LifeShape(wxString name,
54 wxString desc,
55 int width, int height, char *data,
56 int fieldWidth = 20, int fieldHeight = 20,
57 bool wrap = TRUE)
58 {
59 m_name = name;
60 m_desc = desc;
61 m_width = width;
62 m_height = height;
63 m_data = data;
64 m_fieldWidth = fieldWidth;
65 m_fieldHeight = fieldHeight;
66 m_wrap = wrap;
67 }
68
69 wxString m_name;
70 wxString m_desc;
71 int m_width;
72 int m_height;
73 char *m_data;
74 int m_fieldWidth;
75 int m_fieldHeight;
76 bool m_wrap;
77 };
78
79 // --------------------------------------------------------------------------
80 // Life
81 // --------------------------------------------------------------------------
82
83 class Life
84 {
85 public:
86 // ctor and dtor
87 Life(int width, int height);
88 ~Life();
89 void Create(int width, int height);
90 void Destroy();
91
92 // game field
93 inline int GetWidth() const { return m_width; };
94 inline int GetHeight() const { return m_height; };
95 inline void SetBorderWrap(bool on) { m_wrap = on; };
96
97 // cells
98 inline bool IsAlive(int i, int j) const;
99 inline bool IsAlive(Cell c) const;
100 inline int GetX(Cell c) const;
101 inline int GetY(Cell c) const;
102 const CellArray* GetCells() const { return &m_cells; };
103 const CellArray* GetChangedCells() const { return &m_changed; };
104
105 // game logic
106 void Clear();
107 Cell SetCell(int i, int j, bool alive = TRUE);
108 void SetShape(LifeShape &shape);
109 bool NextTic();
110
111 private:
112 int GetNeighbors(int i, int j) const;
113 inline Cell MakeCell(int i, int j, bool alive) const;
114
115 enum CellFlags
116 {
117 CELL_DEAD = 0x0000, // is dead
118 CELL_ALIVE = 0x0001, // is alive
119 CELL_MARK = 0x0002, // will change / has changed
120 };
121
122 int m_width;
123 int m_height;
124 CellArray m_cells;
125 CellArray m_changed;
126 bool m_wrap;
127 };
128
129 #endif // _LIFE_GAME_H_