mirror of
https://github.com/DarkFlippers/unleashed-firmware.git
synced 2025-12-13 13:09:49 +04:00
@@ -11,6 +11,7 @@ This is a Minesweeper implementation for the Flipper Zero device.
|
||||
- Arrow buttons to move
|
||||
- Push center button to open field
|
||||
- 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
|
||||
|
||||
|
||||
@@ -1,144 +1,48 @@
|
||||
#define tile_0_width 8
|
||||
#define tile_0_height 8
|
||||
static uint8_t tile_0_bits[] = {
|
||||
0x00,
|
||||
0x00,
|
||||
0x00,
|
||||
0x00,
|
||||
0x00,
|
||||
0x00,
|
||||
0x00,
|
||||
0x00,
|
||||
};
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, };
|
||||
#define tile_1_width 8
|
||||
#define tile_1_height 8
|
||||
static uint8_t tile_1_bits[] = {
|
||||
0x00,
|
||||
0x10,
|
||||
0x18,
|
||||
0x10,
|
||||
0x10,
|
||||
0x10,
|
||||
0x10,
|
||||
0x00,
|
||||
};
|
||||
0x00, 0x10, 0x18, 0x10, 0x10, 0x10, 0x10, 0x00, };
|
||||
#define tile_2_width 8
|
||||
#define tile_2_height 8
|
||||
static uint8_t tile_2_bits[] = {
|
||||
0x00,
|
||||
0x1C,
|
||||
0x20,
|
||||
0x20,
|
||||
0x18,
|
||||
0x04,
|
||||
0x3C,
|
||||
0x00,
|
||||
};
|
||||
0x00, 0x1C, 0x20, 0x20, 0x18, 0x04, 0x3C, 0x00, };
|
||||
#define tile_3_width 8
|
||||
#define tile_3_height 8
|
||||
static uint8_t tile_3_bits[] = {
|
||||
0x00,
|
||||
0x1C,
|
||||
0x20,
|
||||
0x20,
|
||||
0x18,
|
||||
0x20,
|
||||
0x1C,
|
||||
0x00,
|
||||
};
|
||||
0x00, 0x1C, 0x20, 0x20, 0x18, 0x20, 0x1C, 0x00, };
|
||||
#define tile_4_width 8
|
||||
#define tile_4_height 8
|
||||
static uint8_t tile_4_bits[] = {
|
||||
0x00,
|
||||
0x04,
|
||||
0x14,
|
||||
0x14,
|
||||
0x3C,
|
||||
0x10,
|
||||
0x10,
|
||||
0x00,
|
||||
};
|
||||
0x00, 0x04, 0x14, 0x14, 0x3C, 0x10, 0x10, 0x00, };
|
||||
#define tile_5_width 8
|
||||
#define tile_5_height 8
|
||||
static uint8_t tile_5_bits[] = {
|
||||
0x00,
|
||||
0x3C,
|
||||
0x04,
|
||||
0x1C,
|
||||
0x20,
|
||||
0x20,
|
||||
0x1C,
|
||||
0x00,
|
||||
};
|
||||
0x00, 0x3C, 0x04, 0x1C, 0x20, 0x20, 0x1C, 0x00, };
|
||||
#define tile_6_width 8
|
||||
#define tile_6_height 8
|
||||
static uint8_t tile_6_bits[] = {
|
||||
0x00,
|
||||
0x18,
|
||||
0x24,
|
||||
0x04,
|
||||
0x1C,
|
||||
0x24,
|
||||
0x18,
|
||||
0x00,
|
||||
};
|
||||
0x00, 0x18, 0x24, 0x04, 0x1C, 0x24, 0x18, 0x00, };
|
||||
#define tile_7_width 8
|
||||
#define tile_7_height 8
|
||||
static uint8_t tile_7_bits[] = {
|
||||
0x00,
|
||||
0x3C,
|
||||
0x20,
|
||||
0x20,
|
||||
0x10,
|
||||
0x08,
|
||||
0x08,
|
||||
0x00,
|
||||
};
|
||||
0x00, 0x3C, 0x20, 0x20, 0x10, 0x08, 0x08, 0x00, };
|
||||
#define tile_8_width 8
|
||||
#define tile_8_height 8
|
||||
static uint8_t tile_8_bits[] = {
|
||||
0x00,
|
||||
0x18,
|
||||
0x24,
|
||||
0x18,
|
||||
0x24,
|
||||
0x24,
|
||||
0x18,
|
||||
0x00,
|
||||
};
|
||||
0x00, 0x18, 0x24, 0x18, 0x24, 0x24, 0x18, 0x00, };
|
||||
#define tile_flag_width 8
|
||||
#define tile_flag_height 8
|
||||
static uint8_t tile_flag_bits[] = {
|
||||
0xFF,
|
||||
0x81,
|
||||
0xB9,
|
||||
0x89,
|
||||
0x89,
|
||||
0x9D,
|
||||
0x81,
|
||||
0xFF,
|
||||
};
|
||||
0xFF, 0x81, 0xB9, 0x89, 0x89, 0x9D, 0x81, 0xFF, };
|
||||
#define tile_mine_width 8
|
||||
#define tile_mine_height 8
|
||||
static uint8_t tile_mine_bits[] = {
|
||||
0x55,
|
||||
0xAA,
|
||||
0x55,
|
||||
0xAA,
|
||||
0x55,
|
||||
0xAA,
|
||||
0x55,
|
||||
0xAA,
|
||||
};
|
||||
0x55, 0xAA, 0x55, 0xAA, 0x55, 0xAA, 0x55, 0xAA, };
|
||||
#define tile_uncleared_width 8
|
||||
#define tile_uncleared_height 8
|
||||
static uint8_t tile_uncleared_bits[] = {
|
||||
0xFF,
|
||||
0x81,
|
||||
0x81,
|
||||
0x81,
|
||||
0x81,
|
||||
0x81,
|
||||
0x81,
|
||||
0xFF,
|
||||
};
|
||||
0xFF, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0xFF, };
|
||||
|
||||
@@ -251,16 +251,11 @@ static void setup_playfield(Minesweeper* minesweeper_state) {
|
||||
}
|
||||
|
||||
static void place_flag(Minesweeper* minesweeper_state) {
|
||||
if(minesweeper_state->playfield[minesweeper_state->cursor_x][minesweeper_state->cursor_y] ==
|
||||
TileTypeUncleared) {
|
||||
minesweeper_state->playfield[minesweeper_state->cursor_x][minesweeper_state->cursor_y] =
|
||||
TileTypeFlag;
|
||||
if (minesweeper_state->playfield[minesweeper_state->cursor_x][minesweeper_state->cursor_y] == TileTypeUncleared) {
|
||||
minesweeper_state->playfield[minesweeper_state->cursor_x][minesweeper_state->cursor_y] = TileTypeFlag;
|
||||
minesweeper_state->flags_set++;
|
||||
} else if(
|
||||
minesweeper_state->playfield[minesweeper_state->cursor_x][minesweeper_state->cursor_y] ==
|
||||
TileTypeFlag) {
|
||||
minesweeper_state->playfield[minesweeper_state->cursor_x][minesweeper_state->cursor_y] =
|
||||
TileTypeUncleared;
|
||||
} else if (minesweeper_state->playfield[minesweeper_state->cursor_x][minesweeper_state->cursor_y] == TileTypeFlag) {
|
||||
minesweeper_state->playfield[minesweeper_state->cursor_x][minesweeper_state->cursor_y] = TileTypeUncleared;
|
||||
minesweeper_state->flags_set--;
|
||||
}
|
||||
}
|
||||
@@ -308,10 +303,8 @@ static bool game_won(Minesweeper* minesweeper_state) {
|
||||
const char* header_text = "Game won!";
|
||||
furi_string_cat_printf(tempStr, "Minefield cleared in %01d:%02d", minutes, seconds);
|
||||
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);
|
||||
// TODO: create icon
|
||||
dialog_message_set_icon(message, NULL, 72, 17);
|
||||
|
||||
DialogMessageButton choice = dialog_message_show(dialogs, message);
|
||||
@@ -321,17 +314,60 @@ static bool game_won(Minesweeper* minesweeper_state) {
|
||||
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) {
|
||||
if(minesweeper_state->playfield[cursor_x][cursor_y] != TileTypeUncleared) {
|
||||
// we're on an already uncovered field
|
||||
if (minesweeper_state->playfield[cursor_x][cursor_y] == TileTypeFlag) {
|
||||
// we're on a flagged field, do nothing
|
||||
return true;
|
||||
}
|
||||
if (minesweeper_state->minefield[cursor_x][cursor_y] == FieldMine) {
|
||||
// TODO: player loses!
|
||||
// player loses - draw mine
|
||||
minesweeper_state->playfield[cursor_x][cursor_y] = TileTypeMine;
|
||||
return false;
|
||||
} else {
|
||||
// get number of surrounding mines.
|
||||
}
|
||||
|
||||
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 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->playfield[x][y] == TileTypeFlag) {
|
||||
flags ++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
int mines = minesweeper_state->playfield[cursor_x][cursor_y]; // ¯\_(ツ)_/¯
|
||||
if (flags == mines) {
|
||||
// auto uncover all non-flags around (to win faster ;)
|
||||
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) {
|
||||
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++) {
|
||||
@@ -352,14 +388,14 @@ static bool play_move(Minesweeper* minesweeper_state, int cursor_x, int cursor_y
|
||||
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 ( 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);
|
||||
}
|
||||
@@ -369,7 +405,6 @@ static bool play_move(Minesweeper* minesweeper_state, int cursor_x, int cursor_y
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
static void minesweeper_state_init(Minesweeper* const minesweeper_state) {
|
||||
minesweeper_state->cursor_x = minesweeper_state->cursor_y = 0;
|
||||
@@ -461,10 +496,7 @@ int32_t minesweeper_app(void* p) {
|
||||
setup_playfield(minesweeper_state);
|
||||
minesweeper_state->game_started = true;
|
||||
}
|
||||
if(!play_move(
|
||||
minesweeper_state,
|
||||
minesweeper_state->cursor_x,
|
||||
minesweeper_state->cursor_y)) {
|
||||
if (!play_move(minesweeper_state, minesweeper_state->cursor_x, minesweeper_state->cursor_y)) {
|
||||
// ooops. looks like we hit a mine!
|
||||
if (game_lost(minesweeper_state)) {
|
||||
// player wants to restart.
|
||||
@@ -475,8 +507,7 @@ int32_t minesweeper_app(void* p) {
|
||||
}
|
||||
} else {
|
||||
// check win condition.
|
||||
if(minesweeper_state->fields_cleared ==
|
||||
(PLAYFIELD_HEIGHT * PLAYFIELD_WIDTH) - MINECOUNT) {
|
||||
if (minesweeper_state->fields_cleared == (PLAYFIELD_HEIGHT*PLAYFIELD_WIDTH) - MINECOUNT){
|
||||
if (game_won(minesweeper_state)) {
|
||||
//player wants to restart
|
||||
setup_playfield(minesweeper_state);
|
||||
@@ -510,6 +541,9 @@ int32_t minesweeper_app(void* p) {
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// event timeout
|
||||
;
|
||||
}
|
||||
view_port_update(view_port);
|
||||
release_mutex(&state_mutex, minesweeper_state);
|
||||
|
||||
Reference in New Issue
Block a user