]>
git.saurik.com Git - wxWidgets.git/blob - demos/life/game.h
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Life! game logic
4 // Author: Guillermo Rodriguez Garcia, <guille@iies.es>
8 // Copyright: (c) 2000, Guillermo Rodriguez Garcia
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
15 // for compilers that support precompilation, includes "wx/wx.h"
16 #include "wx/wxprec.h"
22 // for all others, include the necessary headers
27 // --------------------------------------------------------------------------
29 // --------------------------------------------------------------------------
31 // A class which holds a pattern
35 // This ctor is used by the LifeReader class
36 LifePattern(wxString name
,
42 m_description
= description
;
47 // A more convenient ctor for the built-in samples
48 LifePattern(wxString name
,
55 m_description
= description
;
56 m_rules
= wxEmptyString
;
57 // TODO: add the positions later, since the formatting command
58 // causes a crash due to conversion objects not being available
59 // during initialisation.
61 m_shape
.Add( wxString::Format(_T("%i %i"), -width
/2, -height
/2) );
63 for(int j
= 0; j
< height
; j
++)
67 for(int i
= 0; i
< width
; i
++)
69 tmp
+= wxChar(shape
[j
* width
+ i
]);
77 wxString m_description
;
79 wxArrayString m_shape
;
83 // --------------------------------------------------------------------------
85 // --------------------------------------------------------------------------
87 // A struct used to pass cell coordinates around
94 // A private class that contains data about a block of cells
97 // A class that models a Life game instance
106 inline wxUint32
GetNumCells() const { return m_numcells
; };
107 inline wxString
GetRules() const { return m_rules
; };
108 inline wxString
GetDescription() const { return m_description
; };
109 bool IsAlive(wxInt32 x
, wxInt32 y
);
110 void SetCell(wxInt32 x
, wxInt32 y
, bool alive
= true);
111 void SetPattern(const LifePattern
&pattern
);
118 LifeCell
FindNorth();
119 LifeCell
FindSouth();
122 LifeCell
FindCenter();
124 // The following functions find cells within a given viewport; either
125 // all alive cells, or only those cells which have changed since last
126 // generation. You first call BeginFind() to specify the viewport,
127 // then keep calling FindMore() until it returns true.
130 // Specify the viewport and whether to look for alive cells or for
131 // cells which have changed since the last generation and thus need
132 // to be repainted. In this latter case, there is no distinction
133 // between newborn or just-dead cells.
136 // Fills an array with cells that match the specification given with
137 // BeginFind(). The array itself belongs to the Life object and must
138 // not be modified or freed by the caller. If this function returns
139 // false, then the operation is not complete: just process all cells
140 // and call FillMore() again.
142 void BeginFind(wxInt32 x0
, wxInt32 y0
,
143 wxInt32 x1
, wxInt32 y1
,
145 bool FindMore(LifeCell
*cells
[], size_t *ncells
);
149 LifeCellBox
*CreateBox(wxInt32 x
, wxInt32 y
, wxUint32 hv
);
150 LifeCellBox
*LinkBox(wxInt32 x
, wxInt32 y
, bool create
= true);
151 void KillBox(LifeCellBox
*c
);
153 // helper for BeginFind & FindMore
154 void DoLine(wxInt32 x
, wxInt32 y
, wxUint32 alive
, wxUint32 old
= 0);
157 // pattern description
158 wxString m_name
; // name (currently unused)
159 wxString m_rules
; // rules (currently unused)
160 wxString m_description
; // description
163 LifeCellBox
*m_head
; // list of alive boxes
164 LifeCellBox
*m_available
; // list of reusable dead boxes
165 LifeCellBox
**m_boxes
; // hash table of alive boxes
166 wxUint32 m_numcells
; // population (number of alive cells)
168 // state vars for BeginFind & FindMore
169 LifeCell
*m_cells
; // array of cells
170 size_t m_ncells
; // number of valid entries in m_cells
171 wxInt32 m_x
, m_y
, // counters and search mode
178 #endif // _LIFE_GAME_H_