]>
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 #if defined(__GNUG__) && !defined(__APPLE__)
16 #pragma interface "game.h"
19 // for compilers that support precompilation, includes "wx/wx.h"
20 #include "wx/wxprec.h"
26 // for all others, include the necessary headers
31 // --------------------------------------------------------------------------
33 // --------------------------------------------------------------------------
35 // A class which holds a pattern
39 // This ctor is used by the LifeReader class
40 LifePattern(wxString name
,
46 m_description
= description
;
51 // A more convenient ctor for the built-in samples
52 LifePattern(wxString name
,
59 m_description
= description
;
61 m_shape
.Add( wxString::Format(_T("%i %i"), -width
/2, -height
/2) );
62 for(int j
= 0; j
< height
; j
++)
66 for(int i
= 0; i
< width
; i
++)
68 tmp
+= wxChar(shape
[j
* width
+ i
]);
76 wxString m_description
;
78 wxArrayString m_shape
;
82 // --------------------------------------------------------------------------
84 // --------------------------------------------------------------------------
86 // A struct used to pass cell coordinates around
93 // A private class that contains data about a block of cells
96 // A class that models a Life game instance
105 inline wxUint32
GetNumCells() const { return m_numcells
; };
106 inline wxString
GetRules() const { return m_rules
; };
107 inline wxString
GetDescription() const { return m_description
; };
108 bool IsAlive(wxInt32 x
, wxInt32 y
);
109 void SetCell(wxInt32 x
, wxInt32 y
, bool alive
= TRUE
);
110 void SetPattern(const LifePattern
&pattern
);
117 LifeCell
FindNorth();
118 LifeCell
FindSouth();
121 LifeCell
FindCenter();
123 // The following functions find cells within a given viewport; either
124 // all alive cells, or only those cells which have changed since last
125 // generation. You first call BeginFind() to specify the viewport,
126 // then keep calling FindMore() until it returns TRUE.
129 // Specify the viewport and whether to look for alive cells or for
130 // cells which have changed since the last generation and thus need
131 // to be repainted. In this latter case, there is no distinction
132 // between newborn or just-dead cells.
135 // Fills an array with cells that match the specification given with
136 // BeginFind(). The array itself belongs to the Life object and must
137 // not be modified or freed by the caller. If this function returns
138 // FALSE, then the operation is not complete: just process all cells
139 // and call FillMore() again.
141 void BeginFind(wxInt32 x0
, wxInt32 y0
,
142 wxInt32 x1
, wxInt32 y1
,
144 bool FindMore(LifeCell
*cells
[], size_t *ncells
);
148 LifeCellBox
*CreateBox(wxInt32 x
, wxInt32 y
, wxUint32 hv
);
149 LifeCellBox
*LinkBox(wxInt32 x
, wxInt32 y
, bool create
= TRUE
);
150 void KillBox(LifeCellBox
*c
);
152 // helper for BeginFind & FindMore
153 void DoLine(wxInt32 x
, wxInt32 y
, wxUint32 alive
, wxUint32 old
= 0);
156 // pattern description
157 wxString m_name
; // name (currently unused)
158 wxString m_rules
; // rules (currently unused)
159 wxString m_description
; // description
162 LifeCellBox
*m_head
; // list of alive boxes
163 LifeCellBox
*m_available
; // list of reusable dead boxes
164 LifeCellBox
**m_boxes
; // hash table of alive boxes
165 wxUint32 m_numcells
; // population (number of alive cells)
167 // state vars for BeginFind & FindMore
168 LifeCell
*m_cells
; // array of cells
169 size_t m_ncells
; // number of valid entries in m_cells
170 wxInt32 m_x
, m_y
, // counters and search mode
177 #endif // _LIFE_GAME_H_