Minesweeper2  V1.1.1 Game in C++ by Górka Mateusz
Board Class Reference

Board of game. More...

#include <Board.hpp>

Public Member Functions

 Board ()=default
 Construct a new Board object (default constructor) More...
 
 ~Board ()
 Destroy the Board object. More...
 
void set (unsigned int w, unsigned int h, unsigned int m)
 Set values on the board. More...
 
void restart ()
 Restart the board. More...
 
bool uncover (const Vector2D &click)
 Uncover the choosen field. More...
 
bool action (const Vector2D &click)
 Do second action on selected field. More...
 
void debug () const
 Display board on the console to debuging. More...
 
short int noFlaggedMines () const
 Number of no flagged mines on the board. More...
 
int w () const
 Width of board. More...
 
int h () const
 Height of board. More...
 
bool created () const
 Is array created? More...
 
bool inside (const Vector2D &pos) const
 Is filel inside the board. More...
 
bool hint (Vector2D *&pos) const
 Find a hint for player with AI. More...
 
const Fieldoperator() (const Vector2D &vec) const
 Value of field. More...
 

Private Member Functions

Fieldoperator() (const Vector2D &vec)
 Value of field. More...
 
void uncoverAll ()
 Uncover all fields on the board. More...
 
void flagAll ()
 Set flag on the all of mines fields. More...
 
void alloc ()
 Create a array of board. More...
 
void free ()
 Free memory if board array exist. More...
 
void randMines (const Vector2D &click)
 Rand mines position on the board. More...
 
void calcFields ()
 Calculate values of field after rand mines position. More...
 

Private Attributes

Vector2D size {0,0}
 Size od board. More...
 
Field ** board {NULL}
 Array of fields on the board. Coordinates: Board[ x ][ y ]. More...
 
short int mines_init
 Number of mines in beginning of the game. More...
 
short int mines
 Number of no flagged mines. More...
 
short unsigned int covered
 Number of covered fields. More...
 

Static Private Attributes

static const Vector2D AROUND [8]
 Vectors of around coordinates. More...
 

Detailed Description

Board of game.

Constructor & Destructor Documentation

◆ Board()

Board::Board ( )
default

Construct a new Board object (default constructor)

◆ ~Board()

Board::~Board ( )

Destroy the Board object.

18  {
19  this->free();
20 }

Member Function Documentation

◆ set()

void Board::set ( unsigned int  w,
unsigned int  h,
unsigned int  m 
)

Set values on the board.

Parameters
w- width
h- height
m- number of minse
Postcondition
Modifly size and mines
  • Check exceptions
  • Set values
208  {
210  if( m < MIN_MINES ) throw ErrInput("Too least mines!");
211  if( m > size.area() - 9 ) throw ErrInput("Too much mines!");
212 
213  if( size.area() > MIN_AREA ) throw ErrInput("Too little area!");
214 
216  this->mines = m;
217  this->mines_init = m;
218  this->size = Vector2D( w, h );
219  this->covered = size.area() - m;
220 }

◆ restart()

void Board::restart ( )

Restart the board.

Postcondition
  • Free board
  • Restart covered value
223  {
226  this->free();
227 
229  this->mines = this->mines_init;
230  covered = size.area() - this->mines_init;
231 }

◆ uncover()

bool Board::uncover ( const Vector2D click)

Uncover the choosen field.

Parameters
click- position of the field
Return values
true- Done a click
false- click unavaliable
  • Nothing do when click on the uncovered
  • Uncover around if click on the empty field
  • Lose the game when uncover a mine
  • Win the game when uncover every right fields
  • Create, randMines and uncover the field (recursive!)
23  {
24 
25  if( this->created() ){
26 
28  if( (*this)(click).covered() ){
29 
30  (*this)(click).uncover();
31  --this->covered;
32 
33  if( (*this)(click).empty() ){
35  for(uint8_t i=0; i<8; ++i ){
36  if( this->inside( click + Board::AROUND[i] ) )
37  this->uncover( click + Board::AROUND[i] ); //recursive!
38  }
39  }
40  else
42  if( (*this)(click).mine() ){
43  this->uncoverAll();
44  throw EndGame("Boooom!");
45  }
46 
48  if( this->covered == 0 ){
49  this->flagAll();
50  this->mines = 0;
51  throw EndGame("Win!");
52  }
53  }
54  else return false; // move unavaliable
55  }
56  else {
58  alloc();
59  randMines( click );
60  calcFields();
61  this->uncover( click );
62 
63  /* Test fragment <---
64  for( int i=0; i<10; ++i ){
65  (*this)(Vector2D(1,i)).val( i );
66  (*this)(Vector2D(1,i)).uncover();
67  }
68  (*this)(Vector2D(3,3)).flag();
69  // ---> */
70  }
71 
72  return true;
73 }

◆ action()

bool Board::action ( const Vector2D click)

Do second action on selected field.

Parameters
click- position of the field
Return values
true- Done a move
false- move unavaliable
  • If field is flagged, unflag.
  • If field is covered, flag.
  • If field is uncover, uncover around (if it's possible).
76  {
77 
78  if( (*this)(click).flagged() ){
80  (*this)(click).unflag();
81  ++this->mines;
82  }
83  else {
85  if( (*this)(click).covered() ){
86  (*this)(click).flag();
87  --this->mines;
88  }
90  else{
91  // uncover around
92  uint8_t nflags = 0; // number of flags around
93 
94  for(uint8_t i=0; i<8; ++i )
95  if( this->inside( click + Board::AROUND[i] ) )
96  if( (*this)(click + Board::AROUND[i] ).flagged() )
97  ++nflags;
98 
99  // uncover around is possible
100  if( nflags == (*this)(click).val() ){
101  for(uint8_t i=0; i<8; ++i )
102  if( this->inside( click + Board::AROUND[i] ) )
103  this->uncover( click + Board::AROUND[i] );
104  }
105  else
106  return false; //action is unavaliable!
107  }
108  }
109 
110  return true;
111 }

◆ debug()

void Board::debug ( ) const

Display board on the console to debuging.

324  {
325  cout << " ";
326  for(int x=0;x<size.x;++x) cout << "|" << setw(4) << x << " ";
327  cout << endl;
328  for(int y=0;y<size.y;++y){
329  cout << y << " ";
330  for(int x=0;x<size.x;++x)
331  cout << (*this)(Vector2D(x,y));
332  cout << endl;
333  }
334 }

◆ noFlaggedMines()

short int Board::noFlaggedMines ( ) const
inline

Number of no flagged mines on the board.

Returns
short int - number of no flagged mines = mines_init - flag
71  { return mines; }

◆ w()

int Board::w ( ) const
inline

Width of board.

Returns
unsigned int - width
79  { return this->size.x; }

◆ h()

int Board::h ( ) const
inline

Height of board.

Returns
unsigned int - width
86  { return this->size.y; }

◆ created()

bool Board::created ( ) const
inline

Is array created?

Returns
true - yes
false - no
94  { return this->board != NULL; }

◆ inside()

bool Board::inside ( const Vector2D pos) const
inline

Is filel inside the board.

Parameters
pos- position of field
Returns
true - yes
false - no
103  { return ( pos.x >= 0 && pos.x < size.x ) && ( pos.y >= 0 && pos.y < size.y ); }

◆ hint()

bool Board::hint ( Vector2D *&  pos) const

Find a hint for player with AI.

Parameters
[out]pos- Position of hint move
Returns
true - is hint
false - isn't hint
  • Claer hint position
  • Find probably mines position
  • Find safe position
114  {
115 
117  if( pos != NULL ){
118  delete pos;
119  pos = NULL;
120  }
121 
122  Vector2D vec, next;
123  Field value;
124  uint8_t probably_mine, hidden;
125  //** Bool array of probable position mines
126  bool *mines_arr = new bool[ size.x * size.y ];
127 
128  // Set false on the whole mines_arr array
129  for( short int i=0; i < (size.x * size.y); ++i )
130  mines_arr[i] = false;
131 
133  for( next.x = 0; next.x < size.x; ++next.x )
134  for( next.y = 0; next.y < size.y; ++next.y ){
135 
136  if( (*this)( next ).uncovered() ){ // uncovered
137  if( (*this)( next ).empty() ) continue; // and not empty
138 
139  // count all of uncovered fields around
140  hidden = 0;
141  for( uint8_t i=0; i<8; ++i ){
142  vec = next + Board::AROUND[i];
143 
144  if( this->inside( vec ) ) // inside the board
145  if( (*this)( vec ).hidden() ) // count hidden
146  ++hidden;
147  }
148 
149  // if hidden == value then all of them is probably mines
150  // hidden = covered + flagged
151  if( hidden == (*this)( next ).val() )
152  for( uint8_t i=0; i<8; ++i ){
153  vec = next + Board::AROUND[i];
154 
155  if( this->inside( vec ) )
156  if( (*this)( vec ).hidden() )
157  mines_arr[ vec.y*size.x + vec.x ] = true;
158  }
159  }
160  }
161 
162  /* test: display mines_arr
163  for( next.y = 0; next.y < size.y; ++next.y ){
164  for( next.x = 0; next.x < size.x; ++next.x )
165  std::cout << mines_arr[ next.y*size.x + next.x ] << " ";
166  std::cout << std::endl;
167  } // test end */
168 
170  for( next.x = 0; next.x < size.x; ++next.x )
171  for( next.y = 0; next.y < size.y; ++next.y ){
172 
173  if( (*this)( next ).uncovered() ){ // uncovered
174  if( (*this)( next ).empty() ) continue; // and not empty
175 
176  // count all flag around
177  probably_mine = 0;
178  for( uint8_t i=0; i<8; ++i ){
179  vec = next + Board::AROUND[i];
180 
181  if( this->inside( vec ) ) // inside
182  if( mines_arr[ vec.y*size.x + vec.x ] )
183  ++probably_mine;
184  }
185 
186  // if probably_mine == value then covered and ! mines_arr[] fields are safe
187  if( probably_mine == (*this)( next ).val() )
188  for( uint8_t i=0; i<8; ++i ){
189  vec = next + Board::AROUND[i];
190 
191  if( this->inside( vec ) )
192  if( ! mines_arr[ vec.y*size.x + vec.x ] && (*this)( vec ).covered() ){
193  // found the hint
194  pos = new Vector2D(vec);
195  delete[] mines_arr;
196  return true;
197  }
198  }
199  }
200  }
201 
202  // end of fun.
203  delete[] mines_arr;
204  return false;
205 }

◆ operator()() [1/2]

const Field& Board::operator() ( const Vector2D vec) const
inline

Value of field.

Without checking a correct of position!

Warning
Method don't check, is field exist!
Parameters
vec- position on the board
Returns
Field& - field const
123  { return board[ vec.x ][ vec.y ]; }

◆ operator()() [2/2]

Field& Board::operator() ( const Vector2D vec)
inlineprivate

Value of field.

Without checking a correct of position!

Warning
Method don't check, is field exist!
Parameters
vec- position on the board
Returns
Field& - field to edit
133  { return board[ vec.x ][ vec.y ]; }

◆ uncoverAll()

void Board::uncoverAll ( )
private

Uncover all fields on the board.

308  {
309  Vector2D pos;
310  for(; pos.x < size.x; ++pos.x )
311  for( pos.y=0; pos.y < size.y; ++pos.y )
312  (*this)(pos).uncover();
313 }

◆ flagAll()

void Board::flagAll ( )
private

Set flag on the all of mines fields.

316  {
317  Vector2D pos;
318  for(; pos.x < size.x; ++pos.x )
319  for( pos.y=0; pos.y < size.y; ++pos.y )
320  if( (*this)(pos).mine() ) (*this)(pos).flag();
321 }

◆ alloc()

void Board::alloc ( )
private

Create a array of board.

Default value of field is: empty, covered.

Postcondition
Modify only board.
Parameters
size_- Vector2D of new boards sizes.

Create new board

245  {
246  if( size.x < 1 || size.y < 1 ) throw ErrSys("ALLOC 001");
247 
249  this->board = new Field*[ size.x ];
250  if( this->board == NULL ) throw ErrAlloc();
251 
252  for( int i=0; i<size.x; ++i ){
253  this->board[i] = new Field[ size.y ];
254  if( this->board[i] == NULL ) throw ErrAlloc();
255  }
256 }

◆ free()

void Board::free ( )
private

Free memory if board array exist.

234  {
235  if( this->board != NULL ){
236  for( short int i=0; i<size.y; ++i )
237  delete[] this->board[i];
238 
239  delete[] this->board;
240  }
241  this->board = NULL;
242 }

◆ randMines()

void Board::randMines ( const Vector2D click)
private

Rand mines position on the board.

Exceptions
Possibleexceptions:
  • Too much mines! Maximum number of mines is area of board - 9 (first click is on the empty field!)
  • Too least mines! Minumum number of mines is const in Const.hpp.
Parameters
click- where was the first click

Rand a position of mines on the board.

259  {
261  Vector2D pos;
262  short unsigned int num_mines = mines;
263  unsigned max_att = MAX_ATTEMPTS;
264 
265  std::default_random_engine generator( time(NULL) );
266  std::uniform_int_distribution<int> distribution_x( 0, size.x-1 );
267  std::uniform_int_distribution<int> distribution_y( 0, size.y-1 );
268 
269  while( num_mines > 0 ){
270  pos.x = distribution_x(generator);
271  pos.y = distribution_y(generator);
272 
273  if( (*this)( pos ).mine() == false && ! click.adj( pos ) ){
274  (*this)( pos ) = FieldCode::Mine;
275  --num_mines;
276  }
277  else {
278  if( 0 == --max_att )
279  throw ErrSys("Too much attempts to rand location of mines.");
280  }
281  }
282 }

◆ calcFields()

void Board::calcFields ( )
private

Calculate values of field after rand mines position.

285  {
286 
287  uint8_t count; // counter of mines around
288  Vector2D pos, around;
289 
290  for( pos.x=0; pos.x < size.x; ++pos.x )
291  for( pos.y=0; pos.y < size.y; ++pos.y ){
292 
293  if( ! (*this)(pos).mine() ){
294  count = 0; // clear counter
295 
296  for(uint8_t i=0; i<8; ++i ){ // check around
297  around = pos + Board::AROUND[i];
298  if( this->inside( around ) )
299  if( (*this)(around).mine() ) ++count;
300  }
301 
302  (*this)(pos).val( count ); // set values
303  }
304  }
305 }

Member Data Documentation

◆ size

Vector2D Board::size {0,0}
private

Size od board.

◆ board

Field** Board::board {NULL}
private

Array of fields on the board. Coordinates: Board[ x ][ y ].

◆ mines_init

short int Board::mines_init
private

Number of mines in beginning of the game.

◆ mines

short int Board::mines
private

Number of no flagged mines.

◆ covered

short unsigned int Board::covered
private

Number of covered fields.

◆ AROUND

const Vector2D Board::AROUND
staticprivate
Initial value:
= {
{-1,-1},{0,-1},{1,-1},
{-1,0},{1,0},
{-1,1},{0,1},{1,1}
}

Vectors of around coordinates.


The documentation for this class was generated from the following files:
ErrInput
Error Input.
Definition: Error.hpp:36
Board::flagAll
void flagAll()
Set flag on the all of mines fields.
Definition: Board.cpp:316
Vector2D_t< short int >
Board::alloc
void alloc()
Create a array of board.
Definition: Board.cpp:245
Board::size
Vector2D size
Size od board.
Definition: Board.hpp:13
Vector2D_t::area
unsigned int area() const
Area of rectangle extened on the vector.
Definition: Vector2D.hpp:94
Board::mines_init
short int mines_init
Number of mines in beginning of the game.
Definition: Board.hpp:15
MIN_AREA
#define MIN_AREA
Minum area of board.
Definition: Const.hpp:16
Board::free
void free()
Free memory if board array exist.
Definition: Board.cpp:234
Vector2D_t::x
T x
X coordinate of vectior.
Definition: Vector2D.hpp:11
ErrSys
Error.
Definition: Error.hpp:48
ErrAlloc
Class of Alloc errors.
Definition: Error.hpp:27
Board::created
bool created() const
Is array created?
Definition: Board.hpp:93
Board::h
int h() const
Height of board.
Definition: Board.hpp:85
Board::AROUND
static const Vector2D AROUND[8]
Vectors of around coordinates.
Definition: Board.hpp:178
Vector2D_t::adj
bool adj(const Vector2D_t &vec) const
Is points are adjoining?
Definition: Vector2D.hpp:103
Board::calcFields
void calcFields()
Calculate values of field after rand mines position.
Definition: Board.cpp:285
Board::inside
bool inside(const Vector2D &pos) const
Is filel inside the board.
Definition: Board.hpp:102
Board::uncover
bool uncover(const Vector2D &click)
Uncover the choosen field.
Definition: Board.cpp:23
FieldCode::Mine
@ Mine
Code of mine.
MIN_MINES
#define MIN_MINES
Minimum numbers of mines on the board.
Definition: Const.hpp:15
Board::randMines
void randMines(const Vector2D &click)
Rand mines position on the board.
Definition: Board.cpp:259
Board::mines
short int mines
Number of no flagged mines.
Definition: Board.hpp:16
Board::w
int w() const
Width of board.
Definition: Board.hpp:78
Board::board
Field ** board
Array of fields on the board. Coordinates: Board[ x ][ y ].
Definition: Board.hpp:14
Board::uncoverAll
void uncoverAll()
Uncover all fields on the board.
Definition: Board.cpp:308
Vector2D_t::y
T y
Y coordinate of vectior.
Definition: Vector2D.hpp:12
Board::covered
short unsigned int covered
Number of covered fields.
Definition: Board.hpp:17
Vector2D
Vector2D_t< short int > Vector2D
Vector2D typedef.
Definition: Vector2D.hpp:126
Field
Field of the board.
Definition: Field.hpp:26
EndGame
End game exception.
Definition: Error.hpp:60
MAX_ATTEMPTS
#define MAX_ATTEMPTS
Max amount of attempts of rand.
Definition: Const.hpp:48