From c6bee2a55554b69f8a208b7277ad81c320f4905c Mon Sep 17 00:00:00 2001 From: Alexander Weber Date: Mon, 19 Feb 2018 14:47:02 +0100 Subject: [PATCH] TNTSweeper: Some rework to fix #90 / Auto-unmark on reveal --- apps/TNTsweeper_app.lua | 46 ++++++++++++++++++++++++++--------------- 1 file changed, 29 insertions(+), 17 deletions(-) diff --git a/apps/TNTsweeper_app.lua b/apps/TNTsweeper_app.lua index 4a388d1..9258f74 100644 --- a/apps/TNTsweeper_app.lua +++ b/apps/TNTsweeper_app.lua @@ -49,25 +49,37 @@ function sweeper_class:init(level) end end +function sweeper_class:get(sel_w, sel_h) + local board = self.data.board + if board[sel_w] then + return board[sel_w][sel_h] + end +end + function sweeper_class:reveal(sel_w, sel_h) local board = self.data.board - if board[sel_w] and board[sel_w][sel_h] then - local sel = board[sel_w][sel_h] - sel.is_revealed = true - if sel.is_bomb then - if not sel.bomb_marked then -- marked already counted - self.data.bomb_count = self.data.bomb_count + 1 - end - return true - end + local sel = self:get(sel_w, sel_h) + + -- unmark bomb + if sel.bomb_marked then + self:toggle_bomb_mark(sel_w, sel_h) + end + + sel.is_revealed = true + if sel.is_bomb then + -- Bomb found + self.data.bomb_count = self.data.bomb_count + 1 + else + -- No bomb, count empty fields self.data.open_count = self.data.open_count + 1 if sel.count == 0 then for w = sel_w - 1, sel_w + 1 do for h = sel_h - 1, sel_h + 1 do - if board[w] and board[w][h] then - if not board[w][h].is_revealed then - self:reveal(w,h) - end + local chk_sel = self:get(w,h) + if chk_sel and + not chk_sel.is_revealed and + not chk_sel.bomb_marked then + self:reveal(w,h) end end end @@ -76,13 +88,13 @@ function sweeper_class:reveal(sel_w, sel_h) end function sweeper_class:toggle_bomb_mark(sel_w, sel_h) - local field = self.data.board[sel_w][sel_h] - if field.bomb_marked then + local sel = self:get(sel_w, sel_h) + if sel.bomb_marked then self.data.bomb_count = self.data.bomb_count - 1 - field.bomb_marked = nil + sel.bomb_marked = nil else self.data.bomb_count = self.data.bomb_count + 1 - field.bomb_marked = true + sel.bomb_marked = true end end