game.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // Name: bombs1.cpp
  3. // Purpose: Implementation of the class BombsGame
  4. // Author: P. Foggia 1996
  5. // Modified by: Wlodzimierz Skiba (ABX) since 2003
  6. // Created: 1996
  7. // Copyright: (c) 1996 P. Foggia
  8. // Licence: wxWindows licence
  9. ///////////////////////////////////////////////////////////////////////////////
  10. #include "wx/wxprec.h"
  11. #ifdef __BORLANDC__
  12. #pragma hdrstop
  13. #endif
  14. #ifndef WX_PRECOMP
  15. # include "wx/wx.h"
  16. #endif
  17. #include "game.h"
  18. #include <stdlib.h>
  19. #include <limits.h>
  20. #define PROB 0.2
  21. #ifndef RAND_MAX
  22. # define RAND_MAX INT_MAX
  23. #endif
  24. BombsGame::~BombsGame()
  25. {
  26. if (m_field)
  27. {
  28. delete[] m_field;
  29. }
  30. }
  31. // Initialize the play field. Returns false on failure
  32. bool BombsGame::Init(int aWidth, int aHeight, bool easyCorner)
  33. {
  34. m_gridFocusX = m_gridFocusY = -1;
  35. int x, y;
  36. int xx, yy;
  37. if (m_field)
  38. {
  39. delete[] m_field;
  40. }
  41. m_field = new short[aWidth*aHeight];
  42. if (!m_field)
  43. {
  44. m_width = m_height = 0;
  45. return false;
  46. }
  47. m_width = aWidth;
  48. m_height = aHeight;
  49. for(x=0; x<m_width; x++)
  50. {
  51. for(y=0; y<m_height; y++)
  52. {
  53. m_field[x+y*m_width] = ((float)rand()/RAND_MAX <PROB)
  54. ? BG_HIDDEN | BG_BOMB
  55. : BG_HIDDEN;
  56. }
  57. }
  58. /* Force (0,0) not to have a bomb for those that don't want to have
  59. to guess on the first move. Better would be to for the MS rule that
  60. whatever is picked first isn't a bomb.
  61. */
  62. if(easyCorner)
  63. {
  64. m_field[0] = BG_HIDDEN;
  65. }
  66. m_numBombCells = 0;
  67. for(x=0; x<m_width; x++)
  68. for(y=0; y<m_height; y++)
  69. if (m_field[x+y*m_width] & BG_BOMB)
  70. {
  71. m_numBombCells++;
  72. for(xx=x-1; xx<=x+1; xx++)
  73. if (xx>=0 && xx<m_width)
  74. for(yy=y-1; yy<=y+1; yy++)
  75. if (yy>=0 && yy<m_height && (yy!=y || xx!=x))
  76. m_field[xx+yy*m_width]++;
  77. }
  78. m_numRemainingCells = m_height*m_width-m_numBombCells;
  79. m_numMarkedCells = 0;
  80. return true;
  81. }
  82. void BombsGame::Mark(int x, int y)
  83. {
  84. m_field[x+y*m_width] ^= BG_MARKED;
  85. if (IsMarked(x, y))
  86. m_numMarkedCells++;
  87. else
  88. m_numMarkedCells--;
  89. }
  90. void BombsGame::Unhide(int x, int y, bool b_selected)
  91. {
  92. if (!IsHidden(x,y))
  93. {
  94. return;
  95. }
  96. if (b_selected)
  97. m_field[x+y*m_width] |= BG_SELECTED;
  98. m_field[x+y*m_width] &= ~BG_HIDDEN;
  99. if (!IsBomb(x,y))
  100. {
  101. m_numRemainingCells--;
  102. }
  103. }
  104. void BombsGame::Explode(int x, int y)
  105. {
  106. m_field[x+y*m_width] |= BG_EXPLODED;
  107. }