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

Interface and game organization. More...

#include <Game.hpp>

+ Inheritance diagram for Game:

Public Member Functions

 Game ()=delete
 Constructor deleted. More...
 
 Game (const int argc, char *argv[])
 Construct a new Game object. More...
 
 ~Game ()=default
 Destroy the Game object (default destructor) More...
 
void click (const sf::RenderWindow &window, const sf::Mouse::Button butt)
 Click managment. More...
 
int time () const
 Stopwatch clock. More...
 
bool changeClock ()
 Is needed to change clock on the screen? + Change stopwatch value. More...
 
bool isOn () const
 Is game on? More...
 
void finish ()
 Finish the game. More...
 
void start ()
 Start the game. More...
 
void update ()
 Update values on the interface (stopwatch, score) More...
 
void draw (sf::RenderTarget &target, sf::RenderStates states) const override
 Draw method. More...
 
short int width () const
 Width of game window. More...
 
short int height () const
 Height of game window. More...
 

Private Member Functions

bool firstAction (const sf::Mouse::Button butt) const
 Is it first mouse action now? More...
 

Static Private Member Functions

static Vector2D position (const sf::Vector2i &pos)
 Convert coordinates. More...
 

Private Attributes

Board board
 Board of game. More...
 
Display display
 Care about display a game. More...
 
bool buttRev {false}
 Current button mode. 0 - normal, 1 - revers button. More...
 
bool running {false}
 Is game running on? More...
 
sf::Clock clock
 Stopwatch clock. More...
 
int stopwatch {0}
 Stopwatch value. More...
 
int lastClickTime {0}
 Time of last on the board click (or last hint serching) More...
 
bool allowHint {false}
 Is hint is allowed. More...
 

Detailed Description

Interface and game organization.

Constructor & Destructor Documentation

◆ Game() [1/2]

Game::Game ( )
delete

Constructor deleted.

◆ Game() [2/2]

Game::Game ( const int  argc,
char *  argv[] 
)

Construct a new Game object.

Parameters
argc- number of args
argv- values of argso

If user don't give console args, it will open a GUI menu

Console arguments:

  • -w - width of the board
  • -h - height of the board
  • -m - number of mines
  • -H - hints in the game on
  • --h, --help flag
  • Unknow argument handling
Postcondition
31  {
32  unsigned int w = DEFAULT_X_SIZE,
33  h = DEFAULT_Y_SIZE,
34  m = DEFAULT_MINE;
35 
37  if( argc == 1 )
38  Menu::handling( argc, argv );
39 
40 
42  int id = 1;
43  stringstream strm;
44 
45  while( id < argc ){
46 
47  if( argv[id][0] == '-' ){
48  switch( argv[id][1] ){
49 
51  case 'w':
52  strm << argv[ ++id ] << ' ';
53  strm >> w;
54  break;
55 
57  case 'h':
58  strm << argv[ ++id ] << ' ';
59  strm >> h;
60  break;
61 
63  case 'm':
64  strm << argv[ ++id ] << ' ';
65  strm >> m;
66  break;
67 
69  case 'H':
70  this->allowHint = true;
71  break;
72 
73  case '-':
75  if( argv[id][2] == 'h' ){
76  throw EndGame( HELP_INFO );
77  break;
78  }
79  //hack: 'else' go to default case
80 
82  default:
83  cerr << "!!! Warning: unknow input argument" << endl
84  << "! " << argv[id] << endl;
85  }
86  }
87  else {
88  cerr << "!!! Warning: unknow input argument" << endl
89  << "! " << argv[id] << endl;
90  }
91 
92  ++id;
93  }
94 
97  this->board.set( w, h, m );
98 
100  this->display.config( Vector2D(w,h) );
101 
103  this->buttRev = false;
104 }

◆ ~Game()

Game::~Game ( )
default

Destroy the Game object (default destructor)

Member Function Documentation

◆ click()

void Game::click ( const sf::RenderWindow &  window,
const sf::Mouse::Button  butt 
)

Click managment.

Parameters
window- window handle
butt- mouse button
  • Check the click is in the window
  • First action on the board: uncover the field.
  • Second action on the covered field: flag the field. If field is uncovered: if it's possible uncover fields around.
  • If board isn't created, second move is create and uncover too.
  • After correct move:
  • Start the game
  • If clicked on the board, uncover fields yet
126  {
127 
128  Vector2i mouse_pos = Mouse::getPosition(window);
129 
131  if( mouse_pos.x < 0 || mouse_pos.x > width() ) return;
132  if( mouse_pos.y < 0 || mouse_pos.y > height() ) return;
133 
134  if( this->isOn() ){
135 
136  if( mouse_pos.y > GUI_MARGIN_T ){ //inside board
137 
138  if( this->firstAction( butt ) ){
140  if( ! this->board.uncover( Game::position(mouse_pos) ) ) return;
141  }
142  else {
143  if( this->board.created() ){
146  if( ! this->board.action( Game::position(mouse_pos) ) ) return;
147  }
148  else{
150  if( ! this->board.uncover( Game::position(mouse_pos) ) ) return;
151  }
152  }
153 
156  display.hideHint();
157 
158  }
159  else { // outside board (top bar)
160  buttRev = ! buttRev;
161  this->display.changeButt( buttRev );
162  }
163  }
164  else {
166  this->start();
167 
169  if( mouse_pos.y > GUI_MARGIN_T ) this->click( window, butt );
170  }
171 }

◆ time()

int Game::time ( ) const
inline

Stopwatch clock.

Returns
int -
53 { return stopwatch; }

◆ changeClock()

bool Game::changeClock ( )
inline

Is needed to change clock on the screen? + Change stopwatch value.

Postcondition
stopwatch value change
Returns
true - yes
false - no
61  {
62  if( running )
63  if( stopwatch != clock.getElapsedTime().asSeconds() ){
64  stopwatch = clock.getElapsedTime().asSeconds();
65  return true;
66  }
67  return false;
68  }

◆ isOn()

bool Game::isOn ( ) const
inline

Is game on?

Returns
true - yes
false - no
76  { return running; }

◆ finish()

void Game::finish ( )

Finish the game.

  • Set running to false
107  {
109  running = false;
110 }

◆ start()

void Game::start ( )

Start the game.

  • Stopwatch clear
  • Clear board
  • Restart the board
  • Start running mode
113  {
115  clock.restart();
116  stopwatch = 0;
117 
119  this->board.restart();
120 
122  running = true;
123 }

◆ firstAction()

bool Game::firstAction ( const sf::Mouse::Button  butt) const
inlineprivate

Is it first mouse action now?

Parameters
butt- value of button
Returns
true - yes
false - no
96  { return butt == sf::Mouse::Left ? ! this->buttRev : this->buttRev; }

◆ position()

Vector2D Game::position ( const sf::Vector2i &  pos)
staticprivate

Convert coordinates.

Parameters
pos- position on the window
Returns
Vector2D - position on the board
175 { return Vector2D( pos.x/FIELD_SIZE, (pos.y-GUI_MARGIN_T)/FIELD_SIZE ); }

◆ update()

void Game::update ( )

Update values on the interface (stopwatch, score)

  • Update info in the window
  • Check the hint
186  {
188  this->display.mineCounter( this->board.noFlaggedMines() );
189  this->display.stopwatch( this->time() );
190 
192  if( allowHint ){
193 
194  if( stopwatch - lastClickTime > HINT_TIME && display.hintPos == NULL ){
195  lastClickTime = stopwatch; //to delay serching hint
196  this->board.hint( this->display.hintPos );
197 
198  /* test: autoplay
199  if( this->display.hintPos != NULL ){
200  this->board.uncover( *(this->display.hintPos) );
201  delete this->display.hintPos;
202  this->display.hintPos = NULL;
203  }
204  // test end */
205  }
206  }
207 }

◆ draw()

void Game::draw ( sf::RenderTarget &  target,
sf::RenderStates  states 
) const
override

Draw method.

Parameters
target-
states-
178  {
179  this->display.board( target, &this->board );
180  this->display.draw( target );
181 
182  if( ! this->isOn() ) this->display.drawStart( target );
183 }

◆ width()

short int Game::width ( ) const
inline

Width of game window.

Returns
short int - width
123  { return this->board.w()*FIELD_SIZE; }

◆ height()

short int Game::height ( ) const
inline

Height of game window.

Returns
short int - height
130  { return this->board.h()*FIELD_SIZE + GUI_MARGIN_T; }

Member Data Documentation

◆ board

Board Game::board
private

Board of game.

◆ display

Display Game::display
private

Care about display a game.

◆ buttRev

bool Game::buttRev {false}
private

Current button mode. 0 - normal, 1 - revers button.

◆ running

bool Game::running {false}
private

Is game running on?

◆ clock

sf::Clock Game::clock
private

Stopwatch clock.

◆ stopwatch

int Game::stopwatch {0}
private

Stopwatch value.

◆ lastClickTime

int Game::lastClickTime {0}
private

Time of last on the board click (or last hint serching)

◆ allowHint

bool Game::allowHint {false}
private

Is hint is allowed.


The documentation for this class was generated from the following files:
Display::board
void board(sf::RenderTarget &target, const Board *const board) const
Display board on the window.
Definition: Display.cpp:121
DEFAULT_X_SIZE
#define DEFAULT_X_SIZE
Default X size of board.
Definition: Const.hpp:9
Display::stopwatch
void stopwatch(unsigned int seconds)
Update a value of stopwatch time.
Definition: Display.cpp:89
Game::firstAction
bool firstAction(const sf::Mouse::Button butt) const
Is it first mouse action now?
Definition: Game.hpp:95
Game::width
short int width() const
Width of game window.
Definition: Game.hpp:122
Game::time
int time() const
Stopwatch clock.
Definition: Game.hpp:53
Display::drawStart
void drawStart(sf::RenderTarget &target) const
Draw start button on interface.
Definition: Display.hpp:101
GUI_MARGIN_T
#define GUI_MARGIN_T
GUI top margin.
Definition: Const.hpp:20
Board::set
void set(unsigned int w, unsigned int h, unsigned int m)
Set values on the board.
Definition: Board.cpp:208
Display::hideHint
void hideHint()
Hide a hint.
Definition: Display.hpp:119
Display::hintPos
Vector2D * hintPos
Position of hint, NULL if no hint to display.
Definition: Display.hpp:49
Game::board
Board board
Board of game.
Definition: Game.hpp:13
Game::allowHint
bool allowHint
Is hint is allowed.
Definition: Game.hpp:23
Game::click
void click(const sf::RenderWindow &window, const sf::Mouse::Button butt)
Click managment.
Definition: Game.cpp:126
HINT_TIME
#define HINT_TIME
Time to display a hint after correct click [s].
Definition: Const.hpp:11
Game::running
bool running
Is game running on?
Definition: Game.hpp:17
Display::mineCounter
void mineCounter(short signed int mine)
Update value of mine on the display.
Definition: Display.cpp:99
Board::created
bool created() const
Is array created?
Definition: Board.hpp:93
Menu::handling
static void handling(int &argc, char **&argv)
Menu GUI handling.
Definition: Menu.cpp:46
Board::noFlaggedMines
short int noFlaggedMines() const
Number of no flagged mines on the board.
Definition: Board.hpp:70
Game::lastClickTime
int lastClickTime
Time of last on the board click (or last hint serching)
Definition: Game.hpp:21
Game::display
Display display
Care about display a game.
Definition: Game.hpp:14
HELP_INFO
const char *const HELP_INFO
Help messege.
Definition: Game.cpp:13
Display::config
void config(const Vector2D &size)
Config a Display class.
Definition: Display.cpp:30
Board::h
int h() const
Height of board.
Definition: Board.hpp:85
DEFAULT_Y_SIZE
#define DEFAULT_Y_SIZE
Default Y size of board.
Definition: Const.hpp:8
Game::position
static Vector2D position(const sf::Vector2i &pos)
Convert coordinates.
Definition: Game.cpp:174
Game::buttRev
bool buttRev
Current button mode. 0 - normal, 1 - revers button.
Definition: Game.hpp:16
Board::uncover
bool uncover(const Vector2D &click)
Uncover the choosen field.
Definition: Board.cpp:23
Board::restart
void restart()
Restart the board.
Definition: Board.cpp:223
Display::draw
void draw(sf::RenderTarget &target) const
Draw a interface of the game.
Definition: Display.cpp:106
FIELD_SIZE
#define FIELD_SIZE
Sizes of field on the board.
Definition: Const.hpp:19
Board::w
int w() const
Width of board.
Definition: Board.hpp:78
DEFAULT_MINE
#define DEFAULT_MINE
Default number of mines.
Definition: Const.hpp:10
Game::isOn
bool isOn() const
Is game on?
Definition: Game.hpp:75
Game::start
void start()
Start the game.
Definition: Game.cpp:113
Vector2D
Vector2D_t< short int > Vector2D
Vector2D typedef.
Definition: Vector2D.hpp:126
Game::stopwatch
int stopwatch
Stopwatch value.
Definition: Game.hpp:20
Board::hint
bool hint(Vector2D *&pos) const
Find a hint for player with AI.
Definition: Board.cpp:114
EndGame
End game exception.
Definition: Error.hpp:60
Board::action
bool action(const Vector2D &click)
Do second action on selected field.
Definition: Board.cpp:76
Game::clock
sf::Clock clock
Stopwatch clock.
Definition: Game.hpp:19
Display::changeButt
void changeButt(const bool mode)
Change a view of mode button.
Definition: Display.cpp:75
Game::height
short int height() const
Height of game window.
Definition: Game.hpp:129