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