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

@ -68,7 +68,7 @@ static void timer_callback(void* ctx) {
} }
static void input_callback(InputEvent* input_event, FuriMessageQueue* event_queue) { static void input_callback(InputEvent* input_event, FuriMessageQueue* event_queue) {
furi_assert(event_queue); furi_assert(event_queue);
PluginEvent event = {.type = EventTypeKey, .input = *input_event}; PluginEvent event = {.type = EventTypeKey, .input = *input_event};
furi_message_queue_put(event_queue, &event, FuriWaitForever); furi_message_queue_put(event_queue, &event, FuriWaitForever);
@ -111,7 +111,7 @@ static void render_callback(Canvas* const canvas, void* ctx) {
x*TILE_HEIGHT, // x x*TILE_HEIGHT, // x
8 + (y * TILE_WIDTH), // y 8 + (y * TILE_WIDTH), // y
TILE_WIDTH, TILE_WIDTH,
TILE_HEIGHT, TILE_HEIGHT,
tile_0_bits); tile_0_bits);
break; break;
case TileType1: case TileType1:
@ -120,7 +120,7 @@ static void render_callback(Canvas* const canvas, void* ctx) {
x*TILE_HEIGHT, // x x*TILE_HEIGHT, // x
8 + (y * TILE_WIDTH), // y 8 + (y * TILE_WIDTH), // y
TILE_WIDTH, TILE_WIDTH,
TILE_HEIGHT, TILE_HEIGHT,
tile_1_bits); tile_1_bits);
break; break;
case TileType2: case TileType2:
@ -129,7 +129,7 @@ static void render_callback(Canvas* const canvas, void* ctx) {
x*TILE_HEIGHT, // x x*TILE_HEIGHT, // x
8 + (y * TILE_WIDTH), // y 8 + (y * TILE_WIDTH), // y
TILE_WIDTH, TILE_WIDTH,
TILE_HEIGHT, TILE_HEIGHT,
tile_2_bits); tile_2_bits);
break; break;
case TileType3: case TileType3:
@ -138,7 +138,7 @@ static void render_callback(Canvas* const canvas, void* ctx) {
x*TILE_HEIGHT, // x x*TILE_HEIGHT, // x
8 + (y * TILE_WIDTH), // y 8 + (y * TILE_WIDTH), // y
TILE_WIDTH, TILE_WIDTH,
TILE_HEIGHT, TILE_HEIGHT,
tile_3_bits); tile_3_bits);
break; break;
case TileType4: case TileType4:
@ -147,7 +147,7 @@ static void render_callback(Canvas* const canvas, void* ctx) {
x*TILE_HEIGHT, // x x*TILE_HEIGHT, // x
8 + (y * TILE_WIDTH), // y 8 + (y * TILE_WIDTH), // y
TILE_WIDTH, TILE_WIDTH,
TILE_HEIGHT, TILE_HEIGHT,
tile_4_bits); tile_4_bits);
break; break;
case TileType5: case TileType5:
@ -156,7 +156,7 @@ static void render_callback(Canvas* const canvas, void* ctx) {
x*TILE_HEIGHT, // x x*TILE_HEIGHT, // x
8 + (y * TILE_WIDTH), // y 8 + (y * TILE_WIDTH), // y
TILE_WIDTH, TILE_WIDTH,
TILE_HEIGHT, TILE_HEIGHT,
tile_5_bits); tile_5_bits);
break; break;
case TileType6: case TileType6:
@ -165,7 +165,7 @@ static void render_callback(Canvas* const canvas, void* ctx) {
x*TILE_HEIGHT, // x x*TILE_HEIGHT, // x
8 + (y * TILE_WIDTH), // y 8 + (y * TILE_WIDTH), // y
TILE_WIDTH, TILE_WIDTH,
TILE_HEIGHT, TILE_HEIGHT,
tile_6_bits); tile_6_bits);
break; break;
case TileType7: case TileType7:
@ -174,7 +174,7 @@ static void render_callback(Canvas* const canvas, void* ctx) {
x*TILE_HEIGHT, // x x*TILE_HEIGHT, // x
8 + (y * TILE_WIDTH), // y 8 + (y * TILE_WIDTH), // y
TILE_WIDTH, TILE_WIDTH,
TILE_HEIGHT, TILE_HEIGHT,
tile_7_bits); tile_7_bits);
break; break;
case TileType8: case TileType8:
@ -183,7 +183,7 @@ static void render_callback(Canvas* const canvas, void* ctx) {
x*TILE_HEIGHT, // x x*TILE_HEIGHT, // x
8 + (y * TILE_WIDTH), // y 8 + (y * TILE_WIDTH), // y
TILE_WIDTH, TILE_WIDTH,
TILE_HEIGHT, TILE_HEIGHT,
tile_8_bits); tile_8_bits);
break; break;
case TileTypeFlag: case TileTypeFlag:
@ -192,7 +192,7 @@ static void render_callback(Canvas* const canvas, void* ctx) {
x*TILE_HEIGHT, // x x*TILE_HEIGHT, // x
8 + (y * TILE_WIDTH), // y 8 + (y * TILE_WIDTH), // y
TILE_WIDTH, TILE_WIDTH,
TILE_HEIGHT, TILE_HEIGHT,
tile_flag_bits); tile_flag_bits);
break; break;
case TileTypeUncleared: case TileTypeUncleared:
@ -201,7 +201,7 @@ static void render_callback(Canvas* const canvas, void* ctx) {
x*TILE_HEIGHT, // x x*TILE_HEIGHT, // x
8 + (y * TILE_WIDTH), // y 8 + (y * TILE_WIDTH), // y
TILE_WIDTH, TILE_WIDTH,
TILE_HEIGHT, TILE_HEIGHT,
tile_uncleared_bits); tile_uncleared_bits);
break; break;
case TileTypeMine: case TileTypeMine:
@ -210,7 +210,7 @@ static void render_callback(Canvas* const canvas, void* ctx) {
x*TILE_HEIGHT, // x x*TILE_HEIGHT, // x
8 + (y * TILE_WIDTH), // y 8 + (y * TILE_WIDTH), // y
TILE_WIDTH, TILE_WIDTH,
TILE_HEIGHT, TILE_HEIGHT,
tile_mine_bits); tile_mine_bits);
break; break;
} }
@ -238,7 +238,7 @@ static void setup_playfield(Minesweeper* minesweeper_state) {
int rand_y = rand() % PLAYFIELD_HEIGHT; int rand_y = rand() % PLAYFIELD_HEIGHT;
// make sure first guess isn't a mine // make sure first guess isn't a mine
if (minesweeper_state->minefield[rand_x][rand_y] == FieldEmpty && if (minesweeper_state->minefield[rand_x][rand_y] == FieldEmpty &&
(minesweeper_state->cursor_x != rand_x && minesweeper_state->cursor_y != rand_y )) { (minesweeper_state->cursor_x != rand_x && minesweeper_state->cursor_y != rand_y )) {
minesweeper_state->minefield[rand_x][rand_y] = FieldMine; minesweeper_state->minefield[rand_x][rand_y] = FieldMine;
mines_left--; mines_left--;
} }
@ -273,7 +273,7 @@ static bool game_lost(Minesweeper* minesweeper_state) {
dialog_message_set_buttons(message, NULL, "Play again", NULL); dialog_message_set_buttons(message, NULL, "Play again", NULL);
dialog_message_set_icon(message, NULL, 0, 10); dialog_message_set_icon(message, NULL, 0, 10);
NotificationApp* notifications = furi_record_open(RECORD_NOTIFICATION); NotificationApp* notifications = furi_record_open(RECORD_NOTIFICATION);
notification_message(notifications, &sequence_set_vibro_on); notification_message(notifications, &sequence_set_vibro_on);
furi_record_close(RECORD_NOTIFICATION); furi_record_close(RECORD_NOTIFICATION);
@ -293,7 +293,7 @@ static bool game_won(Minesweeper* minesweeper_state) {
tempStr = furi_string_alloc(); tempStr = furi_string_alloc();
int seconds = 0; int seconds = 0;
int minutes = 0; int minutes = 0;
uint32_t ticks_elapsed = furi_get_tick() - minesweeper_state->game_started_tick; uint32_t ticks_elapsed = furi_get_tick() - minesweeper_state->game_started_tick;
seconds = (int) ticks_elapsed / furi_kernel_get_tick_frequency(); seconds = (int) ticks_elapsed / furi_kernel_get_tick_frequency();
minutes = (int) seconds / 60; minutes = (int) seconds / 60;
@ -305,28 +305,31 @@ 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);
dialog_message_free(message); dialog_message_free(message);
furi_string_free(tempStr); furi_string_free(tempStr);
furi_record_close(RECORD_DIALOGS); furi_record_close(RECORD_DIALOGS);
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,18 +354,60 @@ 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;
} }
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;
} }
static void minesweeper_state_init(Minesweeper* const minesweeper_state) { static void minesweeper_state_init(Minesweeper* const minesweeper_state) {
minesweeper_state->cursor_x = minesweeper_state->cursor_y = 0; minesweeper_state->cursor_x = minesweeper_state->cursor_y = 0;
minesweeper_state->game_started = false; minesweeper_state->game_started = false;
for (int y = 0; y < PLAYFIELD_HEIGHT; y++) { for (int y = 0; y < PLAYFIELD_HEIGHT; y++) {
for (int x = 0; x < PLAYFIELD_WIDTH; x++){ for (int x = 0; x < PLAYFIELD_WIDTH; x++){
@ -393,11 +435,11 @@ int32_t minesweeper_app(void* p) {
furi_record_close(RECORD_DIALOGS); furi_record_close(RECORD_DIALOGS);
FuriMessageQueue* event_queue = furi_message_queue_alloc(8, sizeof(PluginEvent)); FuriMessageQueue* event_queue = furi_message_queue_alloc(8, sizeof(PluginEvent));
Minesweeper* minesweeper_state = malloc(sizeof(Minesweeper)); Minesweeper* minesweeper_state = malloc(sizeof(Minesweeper));
// setup // setup
minesweeper_state_init(minesweeper_state); minesweeper_state_init(minesweeper_state);
ValueMutex state_mutex; ValueMutex state_mutex;
if (!init_mutex(&state_mutex, minesweeper_state, sizeof(minesweeper_state))) { if (!init_mutex(&state_mutex, minesweeper_state, sizeof(minesweeper_state))) {
FURI_LOG_E("Minesweeper", "cannot create mutex\r\n"); FURI_LOG_E("Minesweeper", "cannot create mutex\r\n");
@ -407,13 +449,13 @@ int32_t minesweeper_app(void* p) {
// BEGIN IMPLEMENTATION // BEGIN IMPLEMENTATION
// Set system callbacks // Set system callbacks
ViewPort* view_port = view_port_alloc(); ViewPort* view_port = view_port_alloc();
view_port_draw_callback_set(view_port, render_callback, &state_mutex); view_port_draw_callback_set(view_port, render_callback, &state_mutex);
view_port_input_callback_set(view_port, input_callback, event_queue); view_port_input_callback_set(view_port, input_callback, event_queue);
minesweeper_state->timer = furi_timer_alloc(timer_callback, FuriTimerTypeOnce, &state_mutex); minesweeper_state->timer = furi_timer_alloc(timer_callback, FuriTimerTypeOnce, &state_mutex);
// Open GUI and register view_port // Open GUI and register view_port
Gui* gui = furi_record_open("gui"); Gui* gui = furi_record_open("gui");
gui_add_view_port(gui, view_port, GuiLayerFullscreen); gui_add_view_port(gui, view_port, GuiLayerFullscreen);
PluginEvent event; PluginEvent event;
@ -423,7 +465,7 @@ int32_t minesweeper_app(void* p) {
if(event_status == FuriStatusOk) { if(event_status == FuriStatusOk) {
// press events // press events
if(event.type == EventTypeKey) { if(event.type == EventTypeKey) {
if(event.input.type == InputTypeShort) { if(event.input.type == InputTypeShort) {
switch(event.input.key) { switch(event.input.key) {
case InputKeyUp: case InputKeyUp:
minesweeper_state->cursor_y--; minesweeper_state->cursor_y--;
@ -461,7 +503,7 @@ int32_t minesweeper_app(void* p) {
setup_playfield(minesweeper_state); setup_playfield(minesweeper_state);
} else { } else {
// player wants to exit :( // player wants to exit :(
processing = false; processing = false;
} }
} else { } else {
// check win condition. // check win condition.
@ -469,7 +511,7 @@ int32_t minesweeper_app(void* p) {
if (game_won(minesweeper_state)) { if (game_won(minesweeper_state)) {
//player wants to restart //player wants to restart
setup_playfield(minesweeper_state); setup_playfield(minesweeper_state);
} else { } else {
processing = false; processing = false;
} }
} }
@ -496,9 +538,9 @@ int32_t minesweeper_app(void* p) {
case InputKeyBack: case InputKeyBack:
processing = false; processing = false;
break; break;
} }
} }
} }
} else { } else {
// event timeout // event timeout
; ;

Loading…
Cancel
Save