game.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: game.h
  3. // Purpose: Life! game logic
  4. // Author: Guillermo Rodriguez Garcia, <guille@iies.es>
  5. // Modified by:
  6. // Created: Jan/2000
  7. // Copyright: (c) 2000, Guillermo Rodriguez Garcia
  8. // Licence: wxWindows licence
  9. /////////////////////////////////////////////////////////////////////////////
  10. #ifndef _LIFE_GAME_H_
  11. #define _LIFE_GAME_H_
  12. // --------------------------------------------------------------------------
  13. // LifePattern
  14. // --------------------------------------------------------------------------
  15. // A class which holds a pattern
  16. class LifePattern
  17. {
  18. public:
  19. // This ctor is used by the LifeReader class
  20. LifePattern(wxString name,
  21. wxString description,
  22. wxString rules,
  23. wxArrayString shape)
  24. {
  25. m_name = name;
  26. m_description = description;
  27. m_rules = rules;
  28. m_shape = shape;
  29. };
  30. // A more convenient ctor for the built-in samples
  31. LifePattern(wxString name,
  32. wxString description,
  33. int width,
  34. int height,
  35. const char *shape)
  36. {
  37. m_name = name;
  38. m_description = description;
  39. m_rules = wxEmptyString;
  40. // TODO: add the positions later, since the formatting command
  41. // causes a crash due to conversion objects not being available
  42. // during initialisation.
  43. #ifndef __WXMAC__
  44. m_shape.Add( wxString::Format(wxT("%i %i"), -width/2, -height/2) );
  45. #endif
  46. for(int j = 0; j < height; j++)
  47. {
  48. wxString tmp;
  49. for(int i = 0; i < width; i++)
  50. {
  51. tmp += wxChar(shape[j * width + i]);
  52. }
  53. m_shape.Add( tmp );
  54. }
  55. };
  56. wxString m_name;
  57. wxString m_description;
  58. wxString m_rules;
  59. wxArrayString m_shape;
  60. };
  61. // --------------------------------------------------------------------------
  62. // Life
  63. // --------------------------------------------------------------------------
  64. // A struct used to pass cell coordinates around
  65. struct LifeCell
  66. {
  67. wxInt32 i;
  68. wxInt32 j;
  69. };
  70. // A private class that contains data about a block of cells
  71. class LifeCellBox;
  72. // A class that models a Life game instance
  73. class Life
  74. {
  75. public:
  76. // ctor and dtor
  77. Life();
  78. ~Life();
  79. // accessors
  80. inline wxUint32 GetNumCells() const { return m_numcells; };
  81. inline wxString GetRules() const { return m_rules; };
  82. inline wxString GetDescription() const { return m_description; };
  83. bool IsAlive(wxInt32 x, wxInt32 y);
  84. void SetCell(wxInt32 x, wxInt32 y, bool alive = true);
  85. void SetPattern(const LifePattern &pattern);
  86. // game control
  87. void Clear();
  88. bool NextTic();
  89. // navigation
  90. LifeCell FindNorth();
  91. LifeCell FindSouth();
  92. LifeCell FindWest();
  93. LifeCell FindEast();
  94. LifeCell FindCenter();
  95. // The following functions find cells within a given viewport; either
  96. // all alive cells, or only those cells which have changed since last
  97. // generation. You first call BeginFind() to specify the viewport,
  98. // then keep calling FindMore() until it returns true.
  99. //
  100. // BeginFind:
  101. // Specify the viewport and whether to look for alive cells or for
  102. // cells which have changed since the last generation and thus need
  103. // to be repainted. In this latter case, there is no distinction
  104. // between newborn or just-dead cells.
  105. //
  106. // FindMore:
  107. // Fills an array with cells that match the specification given with
  108. // BeginFind(). The array itself belongs to the Life object and must
  109. // not be modified or freed by the caller. If this function returns
  110. // false, then the operation is not complete: just process all cells
  111. // and call FillMore() again.
  112. //
  113. void BeginFind(wxInt32 x0, wxInt32 y0,
  114. wxInt32 x1, wxInt32 y1,
  115. bool changed);
  116. bool FindMore(LifeCell *cells[], size_t *ncells);
  117. private:
  118. // cellbox-related
  119. LifeCellBox *CreateBox(wxInt32 x, wxInt32 y, wxUint32 hv);
  120. LifeCellBox *LinkBox(wxInt32 x, wxInt32 y, bool create = true);
  121. void KillBox(LifeCellBox *c);
  122. // helper for BeginFind & FindMore
  123. void DoLine(wxInt32 x, wxInt32 y, wxUint32 alive, wxUint32 old = 0);
  124. // pattern description
  125. wxString m_name; // name (currently unused)
  126. wxString m_rules; // rules (currently unused)
  127. wxString m_description; // description
  128. // pattern data
  129. LifeCellBox *m_head; // list of alive boxes
  130. LifeCellBox *m_available; // list of reusable dead boxes
  131. LifeCellBox **m_boxes; // hash table of alive boxes
  132. wxUint32 m_numcells; // population (number of alive cells)
  133. // state vars for BeginFind & FindMore
  134. LifeCell *m_cells; // array of cells
  135. size_t m_ncells; // number of valid entries in m_cells
  136. wxInt32 m_x, m_y, // counters and search mode
  137. m_x0, m_y0,
  138. m_x1, m_y1;
  139. bool m_changed;
  140. bool m_findmore;
  141. };
  142. #endif // _LIFE_GAME_H_