]> git.saurik.com Git - wxWidgets.git/blame - demos/life/game.h
a temporary hack to make testing wxTLW/Univ easier
[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
7// RCS-ID: $Id$
8// Copyright: (c) 2000, Guillermo Rodriguez Garcia
9// Licence: wxWindows licence
10/////////////////////////////////////////////////////////////////////////////
11
12#ifndef _LIFE_GAME_H_
13#define _LIFE_GAME_H_
14
15#ifdef __GNUG__
16 #pragma interface "game.h"
17#endif
18
19// for compilers that support precompilation, includes "wx/wx.h"
20#include "wx/wxprec.h"
21
22#ifdef __BORLANDC__
23 #pragma hdrstop
24#endif
25
26// for all others, include the necessary headers
27#ifndef WX_PRECOMP
28 #include "wx/wx.h"
29#endif
30
31// --------------------------------------------------------------------------
f6bcfd97 32// LifePattern
2480be69
GRG
33// --------------------------------------------------------------------------
34
e0a40292 35// A class which holds a pattern
f6bcfd97 36class LifePattern
2480be69
GRG
37{
38public:
f6bcfd97
BP
39 // This ctor is used by the LifeReader class
40 LifePattern(wxString name,
41 wxString description,
42 wxString rules,
43 wxArrayString shape)
44 {
45 m_name = name;
46 m_description = description;
47 m_rules = rules;
48 m_shape = shape;
49 };
50
51 // A more convenient ctor for the built-in samples
52 LifePattern(wxString name,
53 wxString description,
54 int width,
55 int height,
56 const char *shape)
2480be69 57 {
f6bcfd97
BP
58 m_name = name;
59 m_description = description;
60 m_rules = _("");
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) );
64 };
65
66 wxString m_name;
67 wxString m_description;
68 wxString m_rules;
69 wxArrayString m_shape;
2480be69
GRG
70};
71
e0a40292 72
2480be69
GRG
73// --------------------------------------------------------------------------
74// Life
75// --------------------------------------------------------------------------
76
f6bcfd97 77// A struct used to pass cell coordinates around
764835a5 78struct LifeCell
f6bcfd97
BP
79{
80 wxInt32 i;
81 wxInt32 j;
82};
83
84// A private class that contains data about a block of cells
764835a5 85class LifeCellBox;
e0a40292 86
f6bcfd97 87// A class that models a Life game instance
2480be69
GRG
88class Life
89{
90public:
91 // ctor and dtor
e0a40292 92 Life();
2480be69 93 ~Life();
e0a40292
GRG
94
95 // accessors
f6bcfd97
BP
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);
e0a40292
GRG
102
103 // game control
2480be69 104 void Clear();
2480be69
GRG
105 bool NextTic();
106
29b07a38 107 // navigation
764835a5
GD
108 LifeCell FindNorth();
109 LifeCell FindSouth();
110 LifeCell FindWest();
111 LifeCell FindEast();
112 LifeCell FindCenter();
29b07a38 113
e0a40292
GRG
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.
118 //
119 // BeginFind:
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.
124 //
125 // FindMore:
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.
131 //
f6bcfd97
BP
132 void BeginFind(wxInt32 x0, wxInt32 y0,
133 wxInt32 x1, wxInt32 y1,
e0a40292 134 bool changed);
764835a5 135 bool FindMore(LifeCell *cells[], size_t *ncells);
2480be69 136
e0a40292
GRG
137private:
138 // cellbox-related
764835a5
GD
139 LifeCellBox *CreateBox(wxInt32 x, wxInt32 y, wxUint32 hv);
140 LifeCellBox *LinkBox(wxInt32 x, wxInt32 y, bool create = TRUE);
141 void KillBox(LifeCellBox *c);
e0a40292 142
29b07a38 143 // helper for BeginFind & FindMore
f6bcfd97
BP
144 void DoLine(wxInt32 x, wxInt32 y, wxUint32 alive, wxUint32 old = 0);
145
146
147 // pattern description
148 wxString m_name; // name (currently unused)
149 wxString m_rules; // rules (currently unused)
150 wxString m_description; // description
151
152 // pattern data
764835a5
GD
153 LifeCellBox *m_head; // list of alive boxes
154 LifeCellBox *m_available; // list of reusable dead boxes
155 LifeCellBox **m_boxes; // hash table of alive boxes
f6bcfd97
BP
156 wxUint32 m_numcells; // population (number of alive cells)
157
158 // state vars for BeginFind & FindMore
764835a5 159 LifeCell *m_cells; // array of cells
f6bcfd97
BP
160 size_t m_ncells; // number of valid entries in m_cells
161 wxInt32 m_x, m_y, // counters and search mode
162 m_x0, m_y0,
163 m_x1, m_y1;
164 bool m_changed;
165 bool m_findmore;
2480be69
GRG
166};
167
168#endif // _LIFE_GAME_H_