Skip to content

Commit 36c9971

Browse files
committed
Do not require all flags to be placed
1 parent cfce663 commit 36c9971

File tree

2 files changed

+36
-22
lines changed

2 files changed

+36
-22
lines changed

bot/handlers/callbacks.py

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from sqlalchemy.exc import IntegrityError
1111

1212
from bot.minesweeper.game import (get_fake_newgame_data, untouched_cells_count, all_flags_match_bombs,
13-
make_text_table, get_real_game_data)
13+
all_free_cells_are_open, make_text_table, get_real_game_data)
1414
from bot.minesweeper.states import ClickMode, CellMask
1515
from bot.keyboards.kb_minefield import make_keyboard_from_minefield
1616
from bot.cbdata import cb_newgame, cb_click, cb_switch_mode, cb_switch_flag, cb_ignore
@@ -111,28 +111,27 @@ async def callback_open_square(call: types.CallbackQuery, state: FSMContext,
111111
# This cell contained a number
112112
else:
113113
cells[x][y]["mask"] = CellMask.OPEN
114-
# If this is the last cell to open...
115-
if untouched_cells_count(cells) == 0:
116-
# ...and all flags stand on bombs
117-
if all_flags_match_bombs(cells):
118-
with suppress(MessageNotModified):
119-
await call.message.edit_text(
120-
call.message.html_text + f"\n\n{make_text_table(cells)}\n\n<b>You won!</b> 🎉",
121-
reply_markup=None
122-
)
123-
await log_game(session, fsm_data, call.from_user.id, "win")
124-
# ...or some flags stand on numbers
125-
else:
126-
await state.update_data(game_data=game_data)
127-
with suppress(MessageNotModified):
128-
await call.message.edit_reply_markup(
129-
make_keyboard_from_minefield(cells, game_id, game_data["current_mode"])
130-
)
131-
await call.answer(
132-
show_alert=True,
133-
text="Looks like you've placed more flags than there are bombs on field. Please check them again."
114+
if all_free_cells_are_open(cells):
115+
with suppress(MessageNotModified):
116+
await call.message.edit_text(
117+
call.message.html_text + f"\n\n{make_text_table(cells)}\n\n<b>You won!</b> 🎉",
118+
reply_markup=None
134119
)
135-
return
120+
await log_game(session, fsm_data, call.from_user.id, "win")
121+
await call.answer()
122+
return
123+
# There are more flags than there should be
124+
elif untouched_cells_count(cells) == 0 and not all_flags_match_bombs(cells):
125+
await state.update_data(game_data=game_data)
126+
with suppress(MessageNotModified):
127+
await call.message.edit_reply_markup(
128+
make_keyboard_from_minefield(cells, game_id, game_data["current_mode"])
129+
)
130+
await call.answer(
131+
show_alert=True,
132+
text="Looks like you've placed more flags than there are bombs on field. Please check them again."
133+
)
134+
return
136135
# If this is not the last cell to open
137136
else:
138137
await state.update_data(game_data=game_data)

bot/minesweeper/game.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,21 @@ def all_flags_match_bombs(cells: List[List[Dict]]) -> bool:
6161
return True
6262

6363

64+
def all_free_cells_are_open(cells: List[List[Dict]]) -> bool:
65+
"""
66+
Checks whether all non-bombs cells are open
67+
68+
:param cells: array of array of cells dicts
69+
:return: True if all non-bombs cells are in OPEN state
70+
"""
71+
hidden_cells_count = 0
72+
for row in cells:
73+
for cell in row:
74+
if cell["mask"] != CellMask.OPEN and cell["value"] != "*":
75+
hidden_cells_count += 1
76+
return hidden_cells_count == 0
77+
78+
6479
def make_text_table(cells: List[List[Dict]]) -> str:
6580
"""
6681
Makes a text representation of game field using texttable library

0 commit comments

Comments
 (0)