Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minesweeper: Make things a little more friendly at the start #62

Merged
merged 1 commit into from
May 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Minesweeper: Make things a little more friendly at the start
Rather than having the first click hit a bomb, if the first click would
hit a bomb, instead, reset the game board.

This is a (sort of) feature of Windows minesweeper, and IMO makes
playing a bit more fun :-)
  • Loading branch information
rburchell committed May 19, 2019
commit b5e6007d7df9c0216673a8cfb906ab00e3d1468f
19 changes: 14 additions & 5 deletions Games/Minesweeper/Field.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ Field::Field(GLabel& flag_label, GLabel& time_label, GButton& face_button, GWidg
, m_flag_label(flag_label)
, m_time_label(time_label)
{
srand(time(nullptr));
m_timer.on_timeout = [this] {
++m_time_elapsed;
m_time_label.set_text(String::format("%u.%u", m_time_elapsed / 10, m_time_elapsed % 10));
Expand Down Expand Up @@ -167,6 +168,7 @@ void Square::for_each_neighbor(Callback callback)

void Field::reset()
{
m_first_click = true;
set_updates_enabled(false);
m_time_elapsed = 0;
m_time_label.set_text("0");
Expand All @@ -175,7 +177,6 @@ void Field::reset()
m_timer.stop();
set_greedy_for_hits(false);
set_face(Face::Default);
srand(time(nullptr));

m_squares.resize(max(m_squares.size(), rows() * columns()));

Expand Down Expand Up @@ -292,6 +293,13 @@ void Field::paint_event(GPaintEvent& event)

void Field::on_square_clicked(Square& square)
{
if (m_first_click) {
while (square.has_mine) {
reset();
}
}
m_first_click = false;

if (square.is_swept)
return;
if (square.has_flag)
Expand All @@ -307,12 +315,13 @@ void Field::on_square_clicked(Square& square)
if (square.has_mine) {
square.label->set_fill_with_background_color(true);
game_over();
} else {
--m_unswept_empties;
if (square.number == 0)
flood_fill(square);
return;
}

--m_unswept_empties;
if (square.number == 0)
flood_fill(square);

if (!m_unswept_empties)
win();
}
Expand Down
1 change: 1 addition & 0 deletions Games/Minesweeper/Field.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,5 @@ class Field final : public GFrame {
int m_flags_left { 0 };
Face m_face { Face::Default };
bool m_chord_preview { false };
bool m_first_click { true };
};