File: game.h

package info (click to toggle)
wxwidgets3.0 3.0.5.1%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 120,464 kB
  • sloc: cpp: 896,633; makefile: 52,303; ansic: 21,971; sh: 5,713; python: 2,940; xml: 1,534; perl: 264; javascript: 33
file content (165 lines) | stat: -rw-r--r-- 5,309 bytes parent folder | download | duplicates (10)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/////////////////////////////////////////////////////////////////////////////
// Name:        game.h
// Purpose:     Life! game logic
// Author:      Guillermo Rodriguez Garcia, <guille@iies.es>
// Modified by:
// Created:     Jan/2000
// Copyright:   (c) 2000, Guillermo Rodriguez Garcia
// Licence:     wxWindows licence
/////////////////////////////////////////////////////////////////////////////

#ifndef _LIFE_GAME_H_
#define _LIFE_GAME_H_

// --------------------------------------------------------------------------
// LifePattern
// --------------------------------------------------------------------------

// A class which holds a pattern
class LifePattern
{
public:
    // This ctor is used by the LifeReader class
    LifePattern(wxString      name,
                wxString      description,
                wxString      rules,
                wxArrayString shape)
    {
        m_name        = name;
        m_description = description;
        m_rules       = rules;
        m_shape       = shape;
    };

    // A more convenient ctor for the built-in samples
    LifePattern(wxString      name,
                wxString      description,
                int           width,
                int           height,
                const char   *shape)
    {
        m_name        = name;
        m_description = description;
        m_rules       = wxEmptyString;
        // TODO: add the positions later, since the formatting command
        // causes a crash due to conversion objects not being available
        // during initialisation.
#ifndef __WXMAC__
        m_shape.Add( wxString::Format(wxT("%i %i"), -width/2, -height/2) );
#endif
        for(int j = 0; j < height; j++)
        {
            wxString tmp;

            for(int i = 0; i < width; i++)
            {
                tmp += wxChar(shape[j * width + i]);
            }

            m_shape.Add( tmp );
        }
    };

    wxString      m_name;
    wxString      m_description;
    wxString      m_rules;
    wxArrayString m_shape;
};


// --------------------------------------------------------------------------
// Life
// --------------------------------------------------------------------------

// A struct used to pass cell coordinates around
struct LifeCell
{
    wxInt32 i;
    wxInt32 j;
};

// A private class that contains data about a block of cells
class LifeCellBox;

// A class that models a Life game instance
class Life
{
public:
    // ctor and dtor
    Life();
    ~Life();

    // accessors
    inline wxUint32 GetNumCells() const    { return m_numcells; };
    inline wxString GetRules() const       { return m_rules; };
    inline wxString GetDescription() const { return m_description; };
    bool IsAlive(wxInt32 x, wxInt32 y);
    void SetCell(wxInt32 x, wxInt32 y, bool alive = true);
    void SetPattern(const LifePattern &pattern);

    // game control
    void Clear();
    bool NextTic();

    // navigation
    LifeCell FindNorth();
    LifeCell FindSouth();
    LifeCell FindWest();
    LifeCell FindEast();
    LifeCell FindCenter();

    // The following functions find cells within a given viewport; either
    // all alive cells, or only those cells which have changed since last
    // generation. You first call BeginFind() to specify the viewport,
    // then keep calling FindMore() until it returns true.
    //
    // BeginFind:
    //  Specify the viewport and whether to look for alive cells or for
    //  cells which have changed since the last generation and thus need
    //  to be repainted. In this latter case, there is no distinction
    //  between newborn or just-dead cells.
    //
    // FindMore:
    //  Fills an array with cells that match the specification given with
    //  BeginFind(). The array itself belongs to the Life object and must
    //  not be modified or freed by the caller. If this function returns
    //  false, then the operation is not complete: just process all cells
    //  and call FillMore() again.
    //
    void BeginFind(wxInt32 x0, wxInt32 y0,
                   wxInt32 x1, wxInt32 y1,
                   bool changed);
    bool FindMore(LifeCell *cells[], size_t *ncells);

private:
    // cellbox-related
    LifeCellBox *CreateBox(wxInt32 x, wxInt32 y, wxUint32 hv);
    LifeCellBox *LinkBox(wxInt32 x, wxInt32 y, bool create = true);
    void KillBox(LifeCellBox *c);

    // helper for BeginFind & FindMore
    void DoLine(wxInt32 x, wxInt32 y, wxUint32 alive, wxUint32 old = 0);


    // pattern description
    wxString   m_name;          // name (currently unused)
    wxString   m_rules;         // rules (currently unused)
    wxString   m_description;   // description

    // pattern data
    LifeCellBox   *m_head;          // list of alive boxes
    LifeCellBox   *m_available;     // list of reusable dead boxes
    LifeCellBox  **m_boxes;         // hash table of alive boxes
    wxUint32   m_numcells;      // population (number of alive cells)

    // state vars for BeginFind & FindMore
    LifeCell  *m_cells;         // array of cells
    size_t     m_ncells;        // number of valid entries in m_cells
    wxInt32    m_x, m_y,        // counters and search mode
               m_x0, m_y0,
               m_x1, m_y1;
    bool       m_changed;
    bool       m_findmore;
};

#endif  // _LIFE_GAME_H_