]>
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>
7 // Copyright: (c) 2000, Guillermo Rodriguez Garcia
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
14 // --------------------------------------------------------------------------
16 // --------------------------------------------------------------------------
18 // A class which holds a pattern
22 // This ctor is used by the LifeReader class
23 LifePattern(wxString name
,
29 m_description
= description
;
34 // A more convenient ctor for the built-in samples
35 LifePattern(wxString 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.
48 m_shape
.Add( wxString::Format(wxT("%i %i"), -width
/2, -height
/2) );
50 for(int j
= 0; j
< height
; j
++)
54 for(int i
= 0; i
< width
; i
++)
56 tmp
+= wxChar(shape
[j
* width
+ i
]);
64 wxString m_description
;
66 wxArrayString m_shape
;
70 // --------------------------------------------------------------------------
72 // --------------------------------------------------------------------------
74 // A struct used to pass cell coordinates around
81 // A private class that contains data about a block of cells
84 // A class that models a Life game instance
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
);
105 LifeCell
FindNorth();
106 LifeCell
FindSouth();
109 LifeCell
FindCenter();
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.
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.
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.
129 void BeginFind(wxInt32 x0
, wxInt32 y0
,
130 wxInt32 x1
, wxInt32 y1
,
132 bool FindMore(LifeCell
*cells
[], size_t *ncells
);
136 LifeCellBox
*CreateBox(wxInt32 x
, wxInt32 y
, wxUint32 hv
);
137 LifeCellBox
*LinkBox(wxInt32 x
, wxInt32 y
, bool create
= true);
138 void KillBox(LifeCellBox
*c
);
140 // helper for BeginFind & FindMore
141 void DoLine(wxInt32 x
, wxInt32 y
, wxUint32 alive
, wxUint32 old
= 0);
144 // pattern description
145 wxString m_name
; // name (currently unused)
146 wxString m_rules
; // rules (currently unused)
147 wxString m_description
; // description
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)
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
165 #endif // _LIFE_GAME_H_