2000-01-08 13:27:19 -05:00
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Name: game.h
|
2000-03-03 13:21:48 -05:00
|
|
|
// Purpose: Life! game logic
|
2000-01-08 13:27:19 -05:00
|
|
|
// 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_
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
2000-07-15 15:51:35 -04:00
|
|
|
// LifePattern
|
2000-01-08 13:27:19 -05:00
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
2000-02-08 15:04:48 -05:00
|
|
|
// A class which holds a pattern
|
2000-07-15 15:51:35 -04:00
|
|
|
class LifePattern
|
2000-01-08 13:27:19 -05:00
|
|
|
{
|
|
|
|
public:
|
2000-07-15 15:51:35 -04:00
|
|
|
// This ctor is used by the LifeReader class
|
2019-04-05 13:21:04 -04:00
|
|
|
LifePattern(const wxString& name,
|
|
|
|
const wxString& description,
|
|
|
|
const wxString& rules,
|
|
|
|
const wxArrayString& shape)
|
|
|
|
: m_name(name)
|
|
|
|
, m_description(description)
|
|
|
|
, m_rules(rules)
|
|
|
|
, m_shape(shape)
|
2000-07-15 15:51:35 -04:00
|
|
|
{
|
2018-11-25 14:22:55 -05:00
|
|
|
}
|
2005-01-31 13:18:18 -05:00
|
|
|
|
|
|
|
// A more convenient ctor for the built-in samples
|
2019-04-05 13:21:04 -04:00
|
|
|
LifePattern(const wxString& name,
|
|
|
|
const wxString& description,
|
2000-07-15 15:51:35 -04:00
|
|
|
int width,
|
|
|
|
int height,
|
|
|
|
const char *shape)
|
2019-04-05 13:21:04 -04:00
|
|
|
: m_name(name)
|
|
|
|
, m_description(description)
|
2000-01-08 13:27:19 -05:00
|
|
|
{
|
2006-03-25 07:04:37 -05:00
|
|
|
// TODO: add the positions later, since the formatting command
|
|
|
|
// causes a crash due to conversion objects not being available
|
|
|
|
// during initialisation.
|
|
|
|
#ifndef __WXMAC__
|
2002-12-22 14:57:17 -05:00
|
|
|
m_shape.Add( wxString::Format(wxT("%i %i"), -width/2, -height/2) );
|
2006-03-25 07:04:37 -05:00
|
|
|
#endif
|
2000-07-15 15:51:35 -04:00
|
|
|
for(int j = 0; j < height; j++)
|
2002-12-22 14:57:17 -05:00
|
|
|
{
|
|
|
|
wxString tmp;
|
|
|
|
|
|
|
|
for(int i = 0; i < width; i++)
|
|
|
|
{
|
|
|
|
tmp += wxChar(shape[j * width + i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
m_shape.Add( tmp );
|
|
|
|
}
|
2018-11-25 14:22:55 -05:00
|
|
|
}
|
2000-07-15 15:51:35 -04:00
|
|
|
|
|
|
|
wxString m_name;
|
|
|
|
wxString m_description;
|
|
|
|
wxString m_rules;
|
|
|
|
wxArrayString m_shape;
|
2000-01-08 13:27:19 -05:00
|
|
|
};
|
|
|
|
|
2000-02-08 15:04:48 -05:00
|
|
|
|
2000-01-08 13:27:19 -05:00
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
// Life
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
2000-07-15 15:51:35 -04:00
|
|
|
// A struct used to pass cell coordinates around
|
2001-02-04 15:11:49 -05:00
|
|
|
struct LifeCell
|
2000-07-15 15:51:35 -04:00
|
|
|
{
|
|
|
|
wxInt32 i;
|
|
|
|
wxInt32 j;
|
2005-01-31 13:18:18 -05:00
|
|
|
};
|
2000-07-15 15:51:35 -04:00
|
|
|
|
|
|
|
// A private class that contains data about a block of cells
|
2001-02-04 15:11:49 -05:00
|
|
|
class LifeCellBox;
|
2000-02-08 15:04:48 -05:00
|
|
|
|
2000-07-15 15:51:35 -04:00
|
|
|
// A class that models a Life game instance
|
2000-01-08 13:27:19 -05:00
|
|
|
class Life
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
// ctor and dtor
|
2000-02-08 15:04:48 -05:00
|
|
|
Life();
|
2000-01-08 13:27:19 -05:00
|
|
|
~Life();
|
2000-02-08 15:04:48 -05:00
|
|
|
|
|
|
|
// accessors
|
2018-11-25 14:22:55 -05:00
|
|
|
inline wxUint32 GetNumCells() const { return m_numcells; }
|
|
|
|
inline wxString GetRules() const { return m_rules; }
|
|
|
|
inline wxString GetDescription() const { return m_description; }
|
2000-07-15 15:51:35 -04:00
|
|
|
bool IsAlive(wxInt32 x, wxInt32 y);
|
2004-05-27 13:38:07 -04:00
|
|
|
void SetCell(wxInt32 x, wxInt32 y, bool alive = true);
|
2000-07-15 15:51:35 -04:00
|
|
|
void SetPattern(const LifePattern &pattern);
|
2000-02-08 15:04:48 -05:00
|
|
|
|
|
|
|
// game control
|
2000-01-08 13:27:19 -05:00
|
|
|
void Clear();
|
|
|
|
bool NextTic();
|
|
|
|
|
2000-03-03 13:21:48 -05:00
|
|
|
// navigation
|
2001-02-04 15:11:49 -05:00
|
|
|
LifeCell FindNorth();
|
|
|
|
LifeCell FindSouth();
|
|
|
|
LifeCell FindWest();
|
|
|
|
LifeCell FindEast();
|
|
|
|
LifeCell FindCenter();
|
2000-03-03 13:21:48 -05:00
|
|
|
|
2000-02-08 15:04:48 -05:00
|
|
|
// 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,
|
2004-05-27 13:38:07 -04:00
|
|
|
// then keep calling FindMore() until it returns true.
|
2000-02-08 15:04:48 -05:00
|
|
|
//
|
|
|
|
// 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
|
2004-05-27 13:38:07 -04:00
|
|
|
// false, then the operation is not complete: just process all cells
|
2000-02-08 15:04:48 -05:00
|
|
|
// and call FillMore() again.
|
|
|
|
//
|
2000-07-15 15:51:35 -04:00
|
|
|
void BeginFind(wxInt32 x0, wxInt32 y0,
|
|
|
|
wxInt32 x1, wxInt32 y1,
|
2000-02-08 15:04:48 -05:00
|
|
|
bool changed);
|
2001-02-04 15:11:49 -05:00
|
|
|
bool FindMore(LifeCell *cells[], size_t *ncells);
|
2000-01-08 13:27:19 -05:00
|
|
|
|
2000-02-08 15:04:48 -05:00
|
|
|
private:
|
|
|
|
// cellbox-related
|
2001-02-04 15:11:49 -05:00
|
|
|
LifeCellBox *CreateBox(wxInt32 x, wxInt32 y, wxUint32 hv);
|
2004-05-27 13:38:07 -04:00
|
|
|
LifeCellBox *LinkBox(wxInt32 x, wxInt32 y, bool create = true);
|
2001-02-04 15:11:49 -05:00
|
|
|
void KillBox(LifeCellBox *c);
|
2000-02-08 15:04:48 -05:00
|
|
|
|
2000-03-03 13:21:48 -05:00
|
|
|
// helper for BeginFind & FindMore
|
2000-07-15 15:51:35 -04:00
|
|
|
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
|
2001-02-04 15:11:49 -05:00
|
|
|
LifeCellBox *m_head; // list of alive boxes
|
|
|
|
LifeCellBox *m_available; // list of reusable dead boxes
|
|
|
|
LifeCellBox **m_boxes; // hash table of alive boxes
|
2000-07-15 15:51:35 -04:00
|
|
|
wxUint32 m_numcells; // population (number of alive cells)
|
|
|
|
|
|
|
|
// state vars for BeginFind & FindMore
|
2001-02-04 15:11:49 -05:00
|
|
|
LifeCell *m_cells; // array of cells
|
2000-07-15 15:51:35 -04:00
|
|
|
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;
|
2000-01-08 13:27:19 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif // _LIFE_GAME_H_
|