fix cases when mine is got when auto uncovering and some others

flagsbased-uncover
Denis Nelubin 2 years ago
parent 94aaa1ec6e
commit eab0d7121d

@ -313,6 +313,15 @@ static bool game_won(Minesweeper* minesweeper_state) {
} }
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] == TileTypeFlag) {
// we're on an flagged field, do nothing
return true;
}
if (minesweeper_state->minefield[cursor_x][cursor_y] == FieldMine) {
// TODO: player loses!
minesweeper_state->playfield[cursor_x][cursor_y] = TileTypeMine;
return false;
}
if (minesweeper_state->playfield[cursor_x][cursor_y] >= TileType1 && minesweeper_state->playfield[cursor_x][cursor_y] <= TileType8) { if (minesweeper_state->playfield[cursor_x][cursor_y] >= TileType1 && minesweeper_state->playfield[cursor_x][cursor_y] <= TileType8) {
// click on an cleared cell with a number // click on an cleared cell with a number
// count the flags around // count the flags around
@ -341,7 +350,10 @@ 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;
}
} }
} }
} }
@ -352,48 +364,42 @@ static bool play_move(Minesweeper* minesweeper_state, int cursor_x, int cursor_y
// we're on an already uncovered field // we're on an already uncovered field
return true; return true;
} }
if (minesweeper_state->minefield[cursor_x][cursor_y] == FieldMine) { // get number of surrounding mines.
// TODO: player loses! int hint = 0;
minesweeper_state->playfield[cursor_x][cursor_y] = TileTypeMine; for (int y = cursor_y-1; y <= cursor_y+1; y++) {
return false; for (int x = cursor_x-1; x <= cursor_x+1; x++) {
} else { if ( x == cursor_x && y == cursor_y ) {
// get number of surrounding mines. // we're on the cell the user selected, so ignore.
int hint = 0; continue;
for (int y = cursor_y-1; y <= cursor_y+1; y++) { }
for (int x = cursor_x-1; x <= cursor_x+1; x++) { // make sure we don't go OOB
if ( x == cursor_x && y == cursor_y ) { if ( x >= 0 && x < PLAYFIELD_WIDTH && y >= 0 && y < PLAYFIELD_HEIGHT) {
// we're on the cell the user selected, so ignore. if(minesweeper_state->minefield[x][y] == FieldMine) {
continue; hint ++;
}
// 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; // 〜( ̄▽ ̄〜) don't judge me (〜 ̄▽ ̄)〜
minesweeper_state->fields_cleared++; minesweeper_state->playfield[cursor_x][cursor_y] = hint;
FURI_LOG_D("Minesweeper", "Setting %d,%d to %d", cursor_x, cursor_y, hint); minesweeper_state->fields_cleared++;
if (hint == 0) { FURI_LOG_D("Minesweeper", "Setting %d,%d to %d", cursor_x, cursor_y, hint);
// auto open surrounding fields. if (hint == 0) {
for (int auto_y = cursor_y-1; auto_y <= cursor_y+1; auto_y++) { // auto open surrounding fields.
for (int auto_x = cursor_x-1; auto_x <= cursor_x+1; auto_x++) { for (int auto_y = cursor_y-1; auto_y <= cursor_y+1; auto_y++) {
if ( auto_x == cursor_x && auto_y == cursor_y ) { for (int auto_x = cursor_x-1; auto_x <= cursor_x+1; auto_x++) {
continue; 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 ( auto_x >= 0 && auto_x < PLAYFIELD_WIDTH && auto_y >= 0 && auto_y < PLAYFIELD_HEIGHT) {
play_move(minesweeper_state, auto_x, auto_y); 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