 |
Minesweeper2
V1.1.1
Game in C++ by Górka Mateusz
|
Board of game.
More...
#include <Board.hpp>
◆ Board()
Construct a new Board object (default constructor)
◆ ~Board()
Destroy the Board object.
◆ 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
◆ restart()
Restart the board.
- Postcondition
- Free board
- Restart covered value
◆ 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!)
33 if( (*
this)(click).empty() ){
35 for(uint8_t i=0; i<8; ++i ){
42 if( (*
this)(click).mine() ){
◆ 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).
78 if( (*
this)(click).flagged() ){
80 (*this)(click).unflag();
86 (*this)(click).flag();
94 for(uint8_t i=0; i<8; ++i )
100 if( nflags == (*
this)(click).val() ){
101 for(uint8_t i=0; i<8; ++i )
◆ debug()
void Board::debug |
( |
| ) |
const |
Display board on the console to debuging.
326 for(
int x=0;x<
size.
x;++x) cout <<
"|" << setw(4) << x <<
" ";
328 for(
int y=0;y<
size.
y;++y){
330 for(
int x=0;x<
size.
x;++x)
◆ 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
◆ w()
Width of board.
- Returns
- unsigned int - width
79 {
return this->
size.
x; }
◆ h()
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
-
- 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
124 uint8_t probably_mine, hidden;
126 bool *mines_arr =
new bool[
size.
x *
size.
y ];
129 for(
short int i=0; i < (
size.
x *
size.
y); ++i )
130 mines_arr[i] =
false;
133 for( next.
x = 0; next.
x <
size.
x; ++next.
x )
134 for( next.
y = 0; next.
y <
size.
y; ++next.
y ){
136 if( (*
this)( next ).uncovered() ){
137 if( (*
this)( next ).empty() )
continue;
141 for( uint8_t i=0; i<8; ++i ){
145 if( (*
this)( vec ).hidden() )
151 if( hidden == (*
this)( next ).val() )
152 for( uint8_t i=0; i<8; ++i ){
156 if( (*
this)( vec ).hidden() )
157 mines_arr[ vec.
y*
size.
x + vec.
x ] =
true;
170 for( next.
x = 0; next.
x <
size.
x; ++next.
x )
171 for( next.
y = 0; next.
y <
size.
y; ++next.
y ){
173 if( (*
this)( next ).uncovered() ){
174 if( (*
this)( next ).empty() )
continue;
178 for( uint8_t i=0; i<8; ++i ){
182 if( mines_arr[ vec.
y*
size.
x + vec.
x ] )
187 if( probably_mine == (*
this)( next ).val() )
188 for( uint8_t i=0; i<8; ++i ){
192 if( ! mines_arr[ vec.
y*
size.
x + vec.
x ] && (*
this)( vec ).
covered() ){
◆ operator()() [1/2]
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]
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.
310 for(; pos.
x <
size.
x; ++pos.
x )
311 for( pos.
y=0; pos.
y <
size.
y; ++pos.
y )
◆ flagAll()
Set flag on the all of mines fields.
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();
◆ alloc()
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
252 for(
int i=0; i<
size.
x; ++i ){
◆ free()
Free memory if board array exist.
235 if( this->
board != NULL ){
236 for(
short int i=0; i<
size.
y; ++i )
237 delete[] this->
board[i];
239 delete[] this->
board;
◆ randMines()
void Board::randMines |
( |
const Vector2D & |
click | ) |
|
|
private |
Rand mines position on the board.
- Exceptions
-
Possible | exceptions:
- 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.
262 short unsigned int num_mines =
mines;
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 );
269 while( num_mines > 0 ){
270 pos.
x = distribution_x(generator);
271 pos.
y = distribution_y(generator);
273 if( (*
this)( pos ).mine() ==
false && ! click.
adj( pos ) ){
279 throw ErrSys(
"Too much attempts to rand location of mines.");
◆ calcFields()
void Board::calcFields |
( |
| ) |
|
|
private |
Calculate values of field after rand mines position.
290 for( pos.
x=0; pos.
x <
size.
x; ++pos.
x )
291 for( pos.
y=0; pos.
y <
size.
y; ++pos.
y ){
293 if( ! (*
this)(pos).mine() ){
296 for(uint8_t i=0; i<8; ++i ){
298 if( this->
inside( around ) )
299 if( (*
this)(around).mine() ) ++count;
302 (*this)(pos).val( count );
◆ size
◆ 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
Number of no flagged mines.
◆ covered
short unsigned int Board::covered |
|
private |
Number of covered fields.
◆ AROUND
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:
void flagAll()
Set flag on the all of mines fields.
Definition: Board.cpp:316
void alloc()
Create a array of board.
Definition: Board.cpp:245
Vector2D size
Size od board.
Definition: Board.hpp:13
unsigned int area() const
Area of rectangle extened on the vector.
Definition: Vector2D.hpp:94
short int mines_init
Number of mines in beginning of the game.
Definition: Board.hpp:15
#define MIN_AREA
Minum area of board.
Definition: Const.hpp:16
void free()
Free memory if board array exist.
Definition: Board.cpp:234
T x
X coordinate of vectior.
Definition: Vector2D.hpp:11
Error.
Definition: Error.hpp:48
Class of Alloc errors.
Definition: Error.hpp:27
bool created() const
Is array created?
Definition: Board.hpp:93
int h() const
Height of board.
Definition: Board.hpp:85
static const Vector2D AROUND[8]
Vectors of around coordinates.
Definition: Board.hpp:178
bool adj(const Vector2D_t &vec) const
Is points are adjoining?
Definition: Vector2D.hpp:103
void calcFields()
Calculate values of field after rand mines position.
Definition: Board.cpp:285
bool inside(const Vector2D &pos) const
Is filel inside the board.
Definition: Board.hpp:102
bool uncover(const Vector2D &click)
Uncover the choosen field.
Definition: Board.cpp:23
#define MIN_MINES
Minimum numbers of mines on the board.
Definition: Const.hpp:15
void randMines(const Vector2D &click)
Rand mines position on the board.
Definition: Board.cpp:259
short int mines
Number of no flagged mines.
Definition: Board.hpp:16
int w() const
Width of board.
Definition: Board.hpp:78
Field ** board
Array of fields on the board. Coordinates: Board[ x ][ y ].
Definition: Board.hpp:14
void uncoverAll()
Uncover all fields on the board.
Definition: Board.cpp:308
T y
Y coordinate of vectior.
Definition: Vector2D.hpp:12
short unsigned int covered
Number of covered fields.
Definition: Board.hpp:17
Vector2D_t< short int > Vector2D
Vector2D typedef.
Definition: Vector2D.hpp:126
Field of the board.
Definition: Field.hpp:26
End game exception.
Definition: Error.hpp:60
#define MAX_ATTEMPTS
Max amount of attempts of rand.
Definition: Const.hpp:48