]>
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 m_shape
.Add( wxString::Format(_T("%i %i"), -width
/2, -height
/2) );
58 for(int j
= 0; j
< height
; j
++)
62 for(int i
= 0; i
< width
; i
++)
64 tmp
+= wxChar(shape
[j
* width
+ i
]);
72 wxString m_description
;
74 wxArrayString m_shape
;
78 // --------------------------------------------------------------------------
80 // --------------------------------------------------------------------------
82 // A struct used to pass cell coordinates around
89 // A private class that contains data about a block of cells
92 // A class that models a Life game instance
101 inline wxUint32
GetNumCells() const { return m_numcells
; };
102 inline wxString
GetRules() const { return m_rules
; };
103 inline wxString
GetDescription() const { return m_description
; };
104 bool IsAlive(wxInt32 x
, wxInt32 y
);
105 void SetCell(wxInt32 x
, wxInt32 y
, bool alive
= true);
106 void SetPattern(const LifePattern
&pattern
);
113 LifeCell
FindNorth();
114 LifeCell
FindSouth();
117 LifeCell
FindCenter();
119 // The following functions find cells within a given viewport; either
120 // all alive cells, or only those cells which have changed since last
121 // generation. You first call BeginFind() to specify the viewport,
122 // then keep calling FindMore() until it returns true.
125 // Specify the viewport and whether to look for alive cells or for
126 // cells which have changed since the last generation and thus need
127 // to be repainted. In this latter case, there is no distinction
128 // between newborn or just-dead cells.
131 // Fills an array with cells that match the specification given with
132 // BeginFind(). The array itself belongs to the Life object and must
133 // not be modified or freed by the caller. If this function returns
134 // false, then the operation is not complete: just process all cells
135 // and call FillMore() again.
137 void BeginFind(wxInt32 x0
, wxInt32 y0
,
138 wxInt32 x1
, wxInt32 y1
,
140 bool FindMore(LifeCell
*cells
[], size_t *ncells
);
144 LifeCellBox
*CreateBox(wxInt32 x
, wxInt32 y
, wxUint32 hv
);
145 LifeCellBox
*LinkBox(wxInt32 x
, wxInt32 y
, bool create
= true);
146 void KillBox(LifeCellBox
*c
);
148 // helper for BeginFind & FindMore
149 void DoLine(wxInt32 x
, wxInt32 y
, wxUint32 alive
, wxUint32 old
= 0);
152 // pattern description
153 wxString m_name
; // name (currently unused)
154 wxString m_rules
; // rules (currently unused)
155 wxString m_description
; // description
158 LifeCellBox
*m_head
; // list of alive boxes
159 LifeCellBox
*m_available
; // list of reusable dead boxes
160 LifeCellBox
**m_boxes
; // hash table of alive boxes
161 wxUint32 m_numcells
; // population (number of alive cells)
163 // state vars for BeginFind & FindMore
164 LifeCell
*m_cells
; // array of cells
165 size_t m_ncells
; // number of valid entries in m_cells
166 wxInt32 m_x
, m_y
, // counters and search mode
173 #endif // _LIFE_GAME_H_