Merge pull request #3 from panki27/flagsbased-uncover

Uncover surrounding fields if number of set flags is correct. Thanks @gelin
wrap-around
Felix Pankratz 2 years ago committed by GitHub
commit addd984e4e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -9,6 +9,7 @@ This is a Minesweeper implementation for the Flipper Zero device.
- Arrow buttons to move - Arrow buttons to move
- Push center button to open field - Push center button to open field
- Hold center button to toggle flag - Hold center button to toggle flag
- Push center button on an already open field that has the correct amount of flags surrounding it to auto-open the remaining ones (thanks @gelin!)
## Compiling ## Compiling

@ -305,7 +305,6 @@ static bool game_won(Minesweeper* minesweeper_state) {
dialog_message_set_header(message, header_text, 64, 3, AlignCenter, AlignTop); dialog_message_set_header(message, header_text, 64, 3, AlignCenter, AlignTop);
dialog_message_set_text(message, furi_string_get_cstr(tempStr), 64, 32, AlignCenter, AlignCenter); dialog_message_set_text(message, furi_string_get_cstr(tempStr), 64, 32, AlignCenter, AlignCenter);
dialog_message_set_buttons(message, NULL, "Play again", NULL); dialog_message_set_buttons(message, NULL, "Play again", NULL);
// TODO: create icon
dialog_message_set_icon(message, NULL, 72, 17); dialog_message_set_icon(message, NULL, 72, 17);
DialogMessageButton choice = dialog_message_show(dialogs, message); DialogMessageButton choice = dialog_message_show(dialogs, message);
@ -315,18 +314,22 @@ static bool game_won(Minesweeper* minesweeper_state) {
return choice == DialogMessageButtonCenter; return choice == DialogMessageButtonCenter;
} }
// returns false if the move loses the game - otherwise true
static bool play_move(Minesweeper* minesweeper_state, int cursor_x, int cursor_y) { static bool play_move(Minesweeper* minesweeper_state, int cursor_x, int cursor_y) {
if (minesweeper_state->playfield[cursor_x][cursor_y] != TileTypeUncleared) { if (minesweeper_state->playfield[cursor_x][cursor_y] == TileTypeFlag) {
// we're on an already uncovered field // we're on a flagged field, do nothing
return true; return true;
} }
if (minesweeper_state->minefield[cursor_x][cursor_y] == FieldMine) { if (minesweeper_state->minefield[cursor_x][cursor_y] == FieldMine) {
// TODO: player loses! // player loses - draw mine
minesweeper_state->playfield[cursor_x][cursor_y] = TileTypeMine; minesweeper_state->playfield[cursor_x][cursor_y] = TileTypeMine;
return false; return false;
} else { }
// get number of surrounding mines.
int hint = 0; if (minesweeper_state->playfield[cursor_x][cursor_y] >= TileType1 && minesweeper_state->playfield[cursor_x][cursor_y] <= TileType8) {
// click on a cleared cell with a number
// count the flags around
int flags = 0;
for (int y = cursor_y-1; y <= cursor_y+1; y++) { for (int y = cursor_y-1; y <= cursor_y+1; y++) {
for (int x = cursor_x-1; x <= cursor_x+1; x++) { for (int x = cursor_x-1; x <= cursor_x+1; x++) {
if ( x == cursor_x && y == cursor_y ) { if ( x == cursor_x && y == cursor_y ) {
@ -335,18 +338,15 @@ static bool play_move(Minesweeper* minesweeper_state, int cursor_x, int cursor_y
} }
// make sure we don't go OOB // make sure we don't go OOB
if ( x >= 0 && x < PLAYFIELD_WIDTH && y >= 0 && y < PLAYFIELD_HEIGHT) { if ( x >= 0 && x < PLAYFIELD_WIDTH && y >= 0 && y < PLAYFIELD_HEIGHT) {
if(minesweeper_state->minefield[x][y] == FieldMine) { if (minesweeper_state->playfield[x][y] == TileTypeFlag) {
hint ++; flags ++;
} }
} }
} }
} }
// 〜( ̄▽ ̄〜) don't judge me (〜 ̄▽ ̄)〜 int mines = minesweeper_state->playfield[cursor_x][cursor_y]; // ¯\_(ツ)_/¯
minesweeper_state->playfield[cursor_x][cursor_y] = hint; if (flags == mines) {
minesweeper_state->fields_cleared++; // auto uncover all non-flags around (to win faster ;)
FURI_LOG_D("Minesweeper", "Setting %d,%d to %d", cursor_x, cursor_y, hint);
if (hint == 0) {
// auto open surrounding fields.
for (int auto_y = cursor_y-1; auto_y <= cursor_y+1; auto_y++) { for (int auto_y = cursor_y-1; auto_y <= cursor_y+1; auto_y++) {
for (int auto_x = cursor_x-1; auto_x <= cursor_x+1; auto_x++) { for (int auto_x = cursor_x-1; auto_x <= cursor_x+1; auto_x++) {
if ( auto_x == cursor_x && auto_y == cursor_y ) { if ( auto_x == cursor_x && auto_y == cursor_y ) {
@ -354,14 +354,56 @@ static bool play_move(Minesweeper* minesweeper_state, int cursor_x, int cursor_y
} }
if ( auto_x >= 0 && auto_x < PLAYFIELD_WIDTH && auto_y >= 0 && auto_y < PLAYFIELD_HEIGHT) { if ( auto_x >= 0 && auto_x < PLAYFIELD_WIDTH && auto_y >= 0 && auto_y < PLAYFIELD_HEIGHT) {
if (minesweeper_state->playfield[auto_x][auto_y] == TileTypeUncleared) { if (minesweeper_state->playfield[auto_x][auto_y] == TileTypeUncleared) {
play_move(minesweeper_state, auto_x, auto_y); if(!play_move(minesweeper_state, auto_x, auto_y)) {
// flags were wrong, we got a mine!
return false;
}
} }
} }
} }
} }
// we're done without hitting a mine - so return
return true;
}
}
// calculate number of surrounding mines.
int hint = 0;
for (int y = cursor_y-1; y <= cursor_y+1; y++) {
for (int x = cursor_x-1; x <= cursor_x+1; x++) {
if ( x == cursor_x && y == cursor_y ) {
// we're on the cell the user selected, so ignore.
continue;
}
// make sure we don't go OOB
if ( x >= 0 && x < PLAYFIELD_WIDTH && y >= 0 && y < PLAYFIELD_HEIGHT) {
if(minesweeper_state->minefield[x][y] == FieldMine) {
hint ++;
}
}
}
}
// 〜( ̄▽ ̄〜) don't judge me (〜 ̄▽ ̄)〜
minesweeper_state->playfield[cursor_x][cursor_y] = hint;
minesweeper_state->fields_cleared++;
FURI_LOG_D("Minesweeper", "Setting %d,%d to %d", cursor_x, cursor_y, hint);
if (hint == 0) {
// the field is "empty"
// auto open surrounding fields.
for (int auto_y = cursor_y-1; auto_y <= cursor_y+1; auto_y++) {
for (int auto_x = cursor_x-1; auto_x <= cursor_x+1; auto_x++) {
if ( auto_x == cursor_x && auto_y == cursor_y ) {
continue;
}
if ( auto_x >= 0 && auto_x < PLAYFIELD_WIDTH && auto_y >= 0 && auto_y < PLAYFIELD_HEIGHT) {
if (minesweeper_state->playfield[auto_x][auto_y] == TileTypeUncleared) {
play_move(minesweeper_state, auto_x, auto_y);
}
}
}
} }
return true;
} }
return true;
} }
static void minesweeper_state_init(Minesweeper* const minesweeper_state) { static void minesweeper_state_init(Minesweeper* const minesweeper_state) {

Loading…
Cancel
Save