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