]>
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 // --------------------------------------------------------------------------
17 // --------------------------------------------------------------------------
19 // A class which holds a pattern
23 // This ctor is used by the LifeReader class
24 LifePattern(wxString name
,
30 m_description
= description
;
35 // A more convenient ctor for the built-in samples
36 LifePattern(wxString name
,
43 m_description
= description
;
44 m_rules
= wxEmptyString
;
45 // TODO: add the positions later, since the formatting command
46 // causes a crash due to conversion objects not being available
47 // during initialisation.
49 m_shape
.Add( wxString::Format(wxT("%i %i"), -width
/2, -height
/2) );
51 for(int j
= 0; j
< height
; j
++)
55 for(int i
= 0; i
< width
; i
++)
57 tmp
+= wxChar(shape
[j
* width
+ i
]);
65 wxString m_description
;
67 wxArrayString m_shape
;
71 // --------------------------------------------------------------------------
73 // --------------------------------------------------------------------------
75 // A struct used to pass cell coordinates around
82 // A private class that contains data about a block of cells
85 // A class that models a Life game instance
94 inline wxUint32
GetNumCells() const { return m_numcells
; };
95 inline wxString
GetRules() const { return m_rules
; };
96 inline wxString
GetDescription() const { return m_description
; };
97 bool IsAlive(wxInt32 x
, wxInt32 y
);
98 void SetCell(wxInt32 x
, wxInt32 y
, bool alive
= true);
99 void SetPattern(const LifePattern
&pattern
);
106 LifeCell
FindNorth();
107 LifeCell
FindSouth();
110 LifeCell
FindCenter();
112 // The following functions find cells within a given viewport; either
113 // all alive cells, or only those cells which have changed since last
114 // generation. You first call BeginFind() to specify the viewport,
115 // then keep calling FindMore() until it returns true.
118 // Specify the viewport and whether to look for alive cells or for
119 // cells which have changed since the last generation and thus need
120 // to be repainted. In this latter case, there is no distinction
121 // between newborn or just-dead cells.
124 // Fills an array with cells that match the specification given with
125 // BeginFind(). The array itself belongs to the Life object and must
126 // not be modified or freed by the caller. If this function returns
127 // false, then the operation is not complete: just process all cells
128 // and call FillMore() again.
130 void BeginFind(wxInt32 x0
, wxInt32 y0
,
131 wxInt32 x1
, wxInt32 y1
,
133 bool FindMore(LifeCell
*cells
[], size_t *ncells
);
137 LifeCellBox
*CreateBox(wxInt32 x
, wxInt32 y
, wxUint32 hv
);
138 LifeCellBox
*LinkBox(wxInt32 x
, wxInt32 y
, bool create
= true);
139 void KillBox(LifeCellBox
*c
);
141 // helper for BeginFind & FindMore
142 void DoLine(wxInt32 x
, wxInt32 y
, wxUint32 alive
, wxUint32 old
= 0);
145 // pattern description
146 wxString m_name
; // name (currently unused)
147 wxString m_rules
; // rules (currently unused)
148 wxString m_description
; // description
151 LifeCellBox
*m_head
; // list of alive boxes
152 LifeCellBox
*m_available
; // list of reusable dead boxes
153 LifeCellBox
**m_boxes
; // hash table of alive boxes
154 wxUint32 m_numcells
; // population (number of alive cells)
156 // state vars for BeginFind & FindMore
157 LifeCell
*m_cells
; // array of cells
158 size_t m_ncells
; // number of valid entries in m_cells
159 wxInt32 m_x
, m_y
, // counters and search mode
166 #endif // _LIFE_GAME_H_