]>
Commit | Line | Data |
---|---|---|
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 | #if defined(__GNUG__) && !defined(__APPLE__) | |
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 | // LifePattern | |
33 | // -------------------------------------------------------------------------- | |
34 | ||
35 | // A class which holds a pattern | |
36 | class LifePattern | |
37 | { | |
38 | public: | |
39 | // This ctor is used by the LifeReader class | |
40 | LifePattern(wxString name, | |
41 | wxString description, | |
42 | wxString rules, | |
43 | wxArrayString shape) | |
44 | { | |
45 | m_name = name; | |
46 | m_description = description; | |
47 | m_rules = rules; | |
48 | m_shape = shape; | |
49 | }; | |
50 | ||
51 | // A more convenient ctor for the built-in samples | |
52 | LifePattern(wxString name, | |
53 | wxString description, | |
54 | int width, | |
55 | int height, | |
56 | const char *shape) | |
57 | { | |
58 | m_name = name; | |
59 | m_description = description; | |
60 | m_rules = wxEmptyString; | |
61 | m_shape.Add( wxString::Format(_T("%i %i"), -width/2, -height/2) ); | |
62 | for(int j = 0; j < height; j++) | |
63 | { | |
64 | wxString tmp; | |
65 | ||
66 | for(int i = 0; i < width; i++) | |
67 | { | |
68 | tmp += wxChar(shape[j * width + i]); | |
69 | } | |
70 | ||
71 | m_shape.Add( tmp ); | |
72 | } | |
73 | }; | |
74 | ||
75 | wxString m_name; | |
76 | wxString m_description; | |
77 | wxString m_rules; | |
78 | wxArrayString m_shape; | |
79 | }; | |
80 | ||
81 | ||
82 | // -------------------------------------------------------------------------- | |
83 | // Life | |
84 | // -------------------------------------------------------------------------- | |
85 | ||
86 | // A struct used to pass cell coordinates around | |
87 | struct LifeCell | |
88 | { | |
89 | wxInt32 i; | |
90 | wxInt32 j; | |
91 | }; | |
92 | ||
93 | // A private class that contains data about a block of cells | |
94 | class LifeCellBox; | |
95 | ||
96 | // A class that models a Life game instance | |
97 | class Life | |
98 | { | |
99 | public: | |
100 | // ctor and dtor | |
101 | Life(); | |
102 | ~Life(); | |
103 | ||
104 | // accessors | |
105 | inline wxUint32 GetNumCells() const { return m_numcells; }; | |
106 | inline wxString GetRules() const { return m_rules; }; | |
107 | inline wxString GetDescription() const { return m_description; }; | |
108 | bool IsAlive(wxInt32 x, wxInt32 y); | |
109 | void SetCell(wxInt32 x, wxInt32 y, bool alive = true); | |
110 | void SetPattern(const LifePattern &pattern); | |
111 | ||
112 | // game control | |
113 | void Clear(); | |
114 | bool NextTic(); | |
115 | ||
116 | // navigation | |
117 | LifeCell FindNorth(); | |
118 | LifeCell FindSouth(); | |
119 | LifeCell FindWest(); | |
120 | LifeCell FindEast(); | |
121 | LifeCell FindCenter(); | |
122 | ||
123 | // The following functions find cells within a given viewport; either | |
124 | // all alive cells, or only those cells which have changed since last | |
125 | // generation. You first call BeginFind() to specify the viewport, | |
126 | // then keep calling FindMore() until it returns true. | |
127 | // | |
128 | // BeginFind: | |
129 | // Specify the viewport and whether to look for alive cells or for | |
130 | // cells which have changed since the last generation and thus need | |
131 | // to be repainted. In this latter case, there is no distinction | |
132 | // between newborn or just-dead cells. | |
133 | // | |
134 | // FindMore: | |
135 | // Fills an array with cells that match the specification given with | |
136 | // BeginFind(). The array itself belongs to the Life object and must | |
137 | // not be modified or freed by the caller. If this function returns | |
138 | // false, then the operation is not complete: just process all cells | |
139 | // and call FillMore() again. | |
140 | // | |
141 | void BeginFind(wxInt32 x0, wxInt32 y0, | |
142 | wxInt32 x1, wxInt32 y1, | |
143 | bool changed); | |
144 | bool FindMore(LifeCell *cells[], size_t *ncells); | |
145 | ||
146 | private: | |
147 | // cellbox-related | |
148 | LifeCellBox *CreateBox(wxInt32 x, wxInt32 y, wxUint32 hv); | |
149 | LifeCellBox *LinkBox(wxInt32 x, wxInt32 y, bool create = true); | |
150 | void KillBox(LifeCellBox *c); | |
151 | ||
152 | // helper for BeginFind & FindMore | |
153 | void DoLine(wxInt32 x, wxInt32 y, wxUint32 alive, wxUint32 old = 0); | |
154 | ||
155 | ||
156 | // pattern description | |
157 | wxString m_name; // name (currently unused) | |
158 | wxString m_rules; // rules (currently unused) | |
159 | wxString m_description; // description | |
160 | ||
161 | // pattern data | |
162 | LifeCellBox *m_head; // list of alive boxes | |
163 | LifeCellBox *m_available; // list of reusable dead boxes | |
164 | LifeCellBox **m_boxes; // hash table of alive boxes | |
165 | wxUint32 m_numcells; // population (number of alive cells) | |
166 | ||
167 | // state vars for BeginFind & FindMore | |
168 | LifeCell *m_cells; // array of cells | |
169 | size_t m_ncells; // number of valid entries in m_cells | |
170 | wxInt32 m_x, m_y, // counters and search mode | |
171 | m_x0, m_y0, | |
172 | m_x1, m_y1; | |
173 | bool m_changed; | |
174 | bool m_findmore; | |
175 | }; | |
176 | ||
177 | #endif // _LIFE_GAME_H_ |