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
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]
sel.is_revealed = true -- unmark bomb
if sel.is_bomb then if sel.bomb_marked then
if not sel.bomb_marked then -- marked already counted self:toggle_bomb_mark(sel_w, sel_h)
self.data.bomb_count = self.data.bomb_count + 1 end
end
return true sel.is_revealed = true
end 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 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
self:reveal(w,h) not chk_sel.is_revealed and
end not chk_sel.bomb_marked then
self:reveal(w,h)
end end
end end
end end
@ -76,13 +88,13 @@ function sweeper_class:reveal(sel_w, sel_h)
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