TNTSweeper: Some rework to fix #90 / Auto-unmark on reveal
This commit is contained in:
parent
1baadce0b7
commit
c6bee2a555
@ -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
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user