]> git.saurik.com Git - wxWidgets.git/blame - demos/life/game.h
Change version to 3.0.0.
[wxWidgets.git] / demos / life / game.h
CommitLineData
2480be69
GRG
1/////////////////////////////////////////////////////////////////////////////
2// Name: game.h
29b07a38 3// Purpose: Life! game logic
2480be69
GRG
4// Author: Guillermo Rodriguez Garcia, <guille@iies.es>
5// Modified by:
6// Created: Jan/2000
2480be69
GRG
7// Copyright: (c) 2000, Guillermo Rodriguez Garcia
8// Licence: wxWindows licence
9/////////////////////////////////////////////////////////////////////////////
10
11#ifndef _LIFE_GAME_H_
12#define _LIFE_GAME_H_
13
2480be69 14// --------------------------------------------------------------------------
f6bcfd97 15// LifePattern
2480be69
GRG
16// --------------------------------------------------------------------------
17
e0a40292 18// A class which holds a pattern
f6bcfd97 19class LifePattern
2480be69
GRG
20{
21public:
f6bcfd97
BP
22 // This ctor is used by the LifeReader class
23 LifePattern(wxString name,
24 wxString description,
25 wxString rules,
26 wxArrayString shape)
27 {
28 m_name = name;
29 m_description = description;
30 m_rules = rules;
31 m_shape = shape;
32 };
5d2ac6b8
WS
33
34 // A more convenient ctor for the built-in samples
f6bcfd97
BP
35 LifePattern(wxString name,
36 wxString description,
37 int width,
38 int height,
39 const char *shape)
2480be69 40 {
f6bcfd97
BP
41 m_name = name;
42 m_description = description;
5d2ac6b8 43 m_rules = wxEmptyString;
5cf83b1a
JS
44 // TODO: add the positions later, since the formatting command
45 // causes a crash due to conversion objects not being available
46 // during initialisation.
47#ifndef __WXMAC__
9a83f860 48 m_shape.Add( wxString::Format(wxT("%i %i"), -width/2, -height/2) );
5cf83b1a 49#endif
f6bcfd97 50 for(int j = 0; j < height; j++)
f37c24e0
MB
51 {
52 wxString tmp;
53
54 for(int i = 0; i < width; i++)
55 {
56 tmp += wxChar(shape[j * width + i]);
57 }
58
59 m_shape.Add( tmp );
60 }
f6bcfd97
BP
61 };
62
63 wxString m_name;
64 wxString m_description;
65 wxString m_rules;
66 wxArrayString m_shape;
2480be69
GRG
67};
68
e0a40292 69
2480be69
GRG
70// --------------------------------------------------------------------------
71// Life
72// --------------------------------------------------------------------------
73
f6bcfd97 74// A struct used to pass cell coordinates around
764835a5 75struct LifeCell
f6bcfd97
BP
76{
77 wxInt32 i;
78 wxInt32 j;
5d2ac6b8 79};
f6bcfd97
BP
80
81// A private class that contains data about a block of cells
764835a5 82class LifeCellBox;
e0a40292 83
f6bcfd97 84// A class that models a Life game instance
2480be69
GRG
85class Life
86{
87public:
88 // ctor and dtor
e0a40292 89 Life();
2480be69 90 ~Life();
e0a40292
GRG
91
92 // accessors
f6bcfd97
BP
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);
3ac8bd7b 97 void SetCell(wxInt32 x, wxInt32 y, bool alive = true);
f6bcfd97 98 void SetPattern(const LifePattern &pattern);
e0a40292
GRG
99
100 // game control
2480be69 101 void Clear();
2480be69
GRG
102 bool NextTic();
103
29b07a38 104 // navigation
764835a5
GD
105 LifeCell FindNorth();
106 LifeCell FindSouth();
107 LifeCell FindWest();
108 LifeCell FindEast();
109 LifeCell FindCenter();
29b07a38 110
e0a40292
GRG
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,
3ac8bd7b 114 // then keep calling FindMore() until it returns true.
e0a40292
GRG
115 //
116 // BeginFind:
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.
121 //
122 // FindMore:
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
3ac8bd7b 126 // false, then the operation is not complete: just process all cells
e0a40292
GRG
127 // and call FillMore() again.
128 //
f6bcfd97
BP
129 void BeginFind(wxInt32 x0, wxInt32 y0,
130 wxInt32 x1, wxInt32 y1,
e0a40292 131 bool changed);
764835a5 132 bool FindMore(LifeCell *cells[], size_t *ncells);
2480be69 133
e0a40292
GRG
134private:
135 // cellbox-related
764835a5 136 LifeCellBox *CreateBox(wxInt32 x, wxInt32 y, wxUint32 hv);
3ac8bd7b 137 LifeCellBox *LinkBox(wxInt32 x, wxInt32 y, bool create = true);
764835a5 138 void KillBox(LifeCellBox *c);
e0a40292 139
29b07a38 140 // helper for BeginFind & FindMore
f6bcfd97
BP
141 void DoLine(wxInt32 x, wxInt32 y, wxUint32 alive, wxUint32 old = 0);
142
143
144 // pattern description
145 wxString m_name; // name (currently unused)
146 wxString m_rules; // rules (currently unused)
147 wxString m_description; // description
148
149 // pattern data
764835a5
GD
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
f6bcfd97
BP
153 wxUint32 m_numcells; // population (number of alive cells)
154
155 // state vars for BeginFind & FindMore
764835a5 156 LifeCell *m_cells; // array of cells
f6bcfd97
BP
157 size_t m_ncells; // number of valid entries in m_cells
158 wxInt32 m_x, m_y, // counters and search mode
159 m_x0, m_y0,
160 m_x1, m_y1;
161 bool m_changed;
162 bool m_findmore;
2480be69
GRG
163};
164
165#endif // _LIFE_GAME_H_