TNTSweeper: Some rework to fix #90 / Auto-unmark on reveal

This commit is contained in:
Alexander Weber 2018-02-19 14:47:02 +01:00
parent 1baadce0b7
commit c6bee2a555

View File

@ -49,40 +49,52 @@ function sweeper_class:init(level)
end end
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) function sweeper_class:reveal(sel_w, sel_h)
local board = self.data.board local board = self.data.board
if board[sel_w] and board[sel_w][sel_h] then local sel = self:get(sel_w, sel_h)
local sel = board[sel_w][sel_h]
-- unmark bomb
if sel.bomb_marked then
self:toggle_bomb_mark(sel_w, sel_h)
end
sel.is_revealed = true sel.is_revealed = true
if sel.is_bomb then if sel.is_bomb then
if not sel.bomb_marked then -- marked already counted -- Bomb found
self.data.bomb_count = self.data.bomb_count + 1 self.data.bomb_count = self.data.bomb_count + 1
end else
return true -- No bomb, count empty fields
end
self.data.open_count = self.data.open_count + 1 self.data.open_count = self.data.open_count + 1
if sel.count == 0 then if sel.count == 0 then
for w = sel_w - 1, sel_w + 1 do for w = sel_w - 1, sel_w + 1 do
for h = sel_h - 1, sel_h + 1 do for h = sel_h - 1, sel_h + 1 do
if board[w] and board[w][h] then local chk_sel = self:get(w,h)
if not board[w][h].is_revealed then if chk_sel and
not chk_sel.is_revealed and
not chk_sel.bomb_marked then
self:reveal(w,h) self:reveal(w,h)
end end
end end
end end
end end
end end
end
end end
function sweeper_class:toggle_bomb_mark(sel_w, sel_h) function sweeper_class:toggle_bomb_mark(sel_w, sel_h)
local field = self.data.board[sel_w][sel_h] local sel = self:get(sel_w, sel_h)
if field.bomb_marked then if sel.bomb_marked then
self.data.bomb_count = self.data.bomb_count - 1 self.data.bomb_count = self.data.bomb_count - 1
field.bomb_marked = nil sel.bomb_marked = nil
else else
self.data.bomb_count = self.data.bomb_count + 1 self.data.bomb_count = self.data.bomb_count + 1
field.bomb_marked = true sel.bomb_marked = true
end end
end end