]>
git.saurik.com Git - wxWidgets.git/blob - demos/life/game.h
cca12fae4bed9985d355e35c63f3dd2f14d6e805
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 /////////////////////////////////////////////////////////////////////////////
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("%i %i", -width
/2, -height
/2) );
62 for(int j
= 0; j
< height
; j
++)
63 m_shape
.Add( wxString(shape
+ (j
* width
), (size_t) width
) );
67 wxString m_description
;
69 wxArrayString m_shape
;
73 // --------------------------------------------------------------------------
75 // --------------------------------------------------------------------------
77 // A struct used to pass cell coordinates around
84 // A private class that contains data about a block of cells
87 // A class that models a Life game instance
96 inline wxUint32
GetNumCells() const { return m_numcells
; };
97 inline wxString
GetRules() const { return m_rules
; };
98 inline wxString
GetDescription() const { return m_description
; };
99 bool IsAlive(wxInt32 x
, wxInt32 y
);
100 void SetCell(wxInt32 x
, wxInt32 y
, bool alive
= TRUE
);
101 void SetPattern(const LifePattern
&pattern
);
114 // The following functions find cells within a given viewport; either
115 // all alive cells, or only those cells which have changed since last
116 // generation. You first call BeginFind() to specify the viewport,
117 // then keep calling FindMore() until it returns TRUE.
120 // Specify the viewport and whether to look for alive cells or for
121 // cells which have changed since the last generation and thus need
122 // to be repainted. In this latter case, there is no distinction
123 // between newborn or just-dead cells.
126 // Fills an array with cells that match the specification given with
127 // BeginFind(). The array itself belongs to the Life object and must
128 // not be modified or freed by the caller. If this function returns
129 // FALSE, then the operation is not complete: just process all cells
130 // and call FillMore() again.
132 void BeginFind(wxInt32 x0
, wxInt32 y0
,
133 wxInt32 x1
, wxInt32 y1
,
135 bool FindMore(Cell
*cells
[], size_t *ncells
);
139 CellBox
*CreateBox(wxInt32 x
, wxInt32 y
, wxUint32 hv
);
140 CellBox
*LinkBox(wxInt32 x
, wxInt32 y
, bool create
= TRUE
);
141 void KillBox(CellBox
*c
);
143 // helper for BeginFind & FindMore
144 void DoLine(wxInt32 x
, wxInt32 y
, wxUint32 alive
, wxUint32 old
= 0);
147 // pattern description
148 wxString m_name
; // name (currently unused)
149 wxString m_rules
; // rules (currently unused)
150 wxString m_description
; // description
153 CellBox
*m_head
; // list of alive boxes
154 CellBox
*m_available
; // list of reusable dead boxes
155 CellBox
**m_boxes
; // hash table of alive boxes
156 wxUint32 m_numcells
; // population (number of alive cells)
158 // state vars for BeginFind & FindMore
159 Cell
*m_cells
; // array of cells
160 size_t m_ncells
; // number of valid entries in m_cells
161 wxInt32 m_x
, m_y
, // counters and search mode
168 #endif // _LIFE_GAME_H_