Merge pull request #97 from bell07/pr_fix_tntsweeper

TNTSweeper: Some rework to fix #90 / Auto-unmark on reveal
This commit is contained in:
bell07 2018-02-21 16:53:19 +01:00 committed by GitHub
commit 7e46231c5f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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