pico8-0hh1/board.lua

399 lines
10 KiB
Lua
Raw Normal View History

2022-05-29 18:38:07 +02:00
local Board = {}
function Board.new()
local debug = false
2022-06-01 23:14:22 +02:00
2022-05-29 18:38:07 +02:00
local width = 10
2022-06-01 23:14:22 +02:00
local tile_width = 10
local margin = 4
local padding = 1
2022-05-29 18:38:07 +02:00
local t = [[
0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 2, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 2, 2, 0, 1, 0, 2, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 2, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 2, 0, 0
]]
a = {foo = 3}
local tiles = split(t)
return {
reset = function(self)
local t = [[
0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0
]]
tiles = split(t)
end,
2022-06-02 20:18:02 +02:00
get_tile = function(self, idx)
2022-05-29 18:38:07 +02:00
return tiles[idx]
end,
-- returns a COPY of the tiles array
2022-06-02 20:18:02 +02:00
get_tiles_copy = function(self)
2022-05-29 18:38:07 +02:00
return copy(tiles)
end,
-- overwrites the whole tiles array
2022-06-02 20:18:02 +02:00
set_tiles = function(self, newtiles)
2022-05-29 18:38:07 +02:00
assert(#newtiles == #tiles, "New tiles array must have a length of " .. #tiles)
tiles = copy(newtiles)
end,
idx_xy = function(self, idx)
return idx_xy(idx, width)
end,
xy_idx = function(self, x, y)
return xy_idx(x, y, width)
end,
2022-06-01 23:14:22 +02:00
draw_coords = function(self, x, y)
if not y then x, y = self:idx_xy(x) end
return margin + (x-1)*tile_width + (x-1)*(padding+1),
margin + (y-1)*tile_width + (y-1)*(padding+1)
end,
2022-06-02 20:18:02 +02:00
get_size = function(self)
2022-06-01 23:14:22 +02:00
return width
end,
2022-06-02 20:18:02 +02:00
get_tile_width = function(self)
2022-06-01 23:14:22 +02:00
return tile_width
end,
2022-05-29 18:38:07 +02:00
fill = function(self, idx, color, invert)
2022-06-02 20:08:20 +02:00
if idx > width*width then return end
2022-05-29 18:38:07 +02:00
if invert then
color = color == YELLOW and BLUE or YELLOW
end
tiles[idx] = color
end,
2022-06-02 20:18:02 +02:00
get_rows = function(self)
2022-05-29 18:38:07 +02:00
local ret = {}
for i = 1, width do
add(ret, slice(tiles, ((i - 1) * width) + 1, i * width))
end
return ret
end,
2022-06-02 20:18:02 +02:00
get_cols = function(self)
2022-05-29 18:38:07 +02:00
local ret = {}
2022-06-02 20:18:02 +02:00
local rows = self.get_rows(self)
2022-05-29 18:38:07 +02:00
for i = 1, width do
add(ret, map(rows, function(v) return v[i] end))
end
return ret
end,
2022-06-02 20:18:02 +02:00
is_complete = function(self)
2022-05-29 18:38:07 +02:00
return count(tiles, 0) == 0
end,
2022-06-02 20:18:02 +02:00
is_valid = function(self)
local rows = self:get_rows()
2022-05-29 18:38:07 +02:00
for y,row in ipairs(rows) do
-- check count
if count(row, BLUE) ~= count(row, YELLOW) then
if (debug) printh("uneven count on row "..y)
return false
end
-- check identical lines
for k,other in ipairs(rows) do
if equal(other, row) and other ~= row then
if (debug) printh("equal rows "..k)
return false
end
end
-- check triples
2022-06-02 20:18:02 +02:00
if self:count_consecutives(row) > 2 then
2022-05-29 18:38:07 +02:00
if (debug) printh("triples")
return false
end
end
2022-06-02 20:18:02 +02:00
local cols = self:get_cols()
2022-05-29 18:38:07 +02:00
for col in all(cols) do
-- check count
if count(col, BLUE) ~= count(col, YELLOW) then
if (debug) printh("uneven count")
return false
end
-- check identical lines
for other in all(cols) do
if equal(other, col) and other ~= col then
if (debug) printh("equal cols")
return false
end
end
-- check triples
2022-06-02 20:18:02 +02:00
if self:count_consecutives(col) > 2 then
2022-05-29 18:38:07 +02:00
if (debug) printh("triples")
return false
end
end
return true
end,
2022-06-02 20:18:02 +02:00
count_consecutives = function(self, line)
2022-05-29 18:38:07 +02:00
local count = 0
local last = 0
for v in all(line) do
if v ~= last then
last = 0
else
last = v
count += 1
end
end
return count
end,
--
-- Returns the index of a random zero tile
--
2022-06-02 20:18:02 +02:00
get_random_zero = function(self)
2022-05-29 18:38:07 +02:00
assert(count(tiles, 0) > 0, "No zero left")
local zeroes = filter(tiles, function(v) return v == 0 end, true)
local z = {}
for k,v in pairs(zeroes) do
add(z, k)
end
return rnd(z)
end,
--
-- Returns the index of a random non-zero tile
--
2022-06-02 20:18:02 +02:00
get_random_non_zero = function(self)
2022-05-29 18:38:07 +02:00
assert(count(tiles, 0) < #tiles, "All zeroes")
local numbers = filter(tiles, function(v) return v ~= 0 end, true)
local z = {}
for k,v in pairs(numbers) do
add(z, k)
end
return rnd(z)
end,
tostring = function(self)
local str = ''
for v in all(tiles) do
str ..= ", " .. v
end
return str
end,
-- Solves 1 step of the board
-- Returns "valid" if it solved it without guessing
-- Returns "invalid" if the board cannot be solved
2022-06-02 20:18:02 +02:00
solve_step = function(self, random)
2022-05-29 18:38:07 +02:00
local zeroes = count(tiles, 0)
2022-06-02 20:18:02 +02:00
self:surround_doubles()
self:split_triples()
self:fill_lines()
self:no_identical_lines()
2022-05-29 18:38:07 +02:00
local changed = zeroes ~= count(tiles, 0)
2022-06-02 20:18:02 +02:00
if not changed and random and not self:is_complete() then
2022-05-29 18:38:07 +02:00
-- Set a random color
2022-06-02 20:18:02 +02:00
local z = self:get_random_zero()
2022-05-29 18:38:07 +02:00
self:fill(z, rnd({BLUE, YELLOW}))
if (debug) printh("!!!!!!!!!!!!!!!!! RANDOM FILL AT " .. z)
return "invalid"
end
2022-06-02 20:18:02 +02:00
return (changed or self:is_complete()) and "valid" or "invalid"
2022-05-29 18:38:07 +02:00
end,
2022-06-02 20:18:02 +02:00
surround_doubles = function(self)
2022-05-29 18:38:07 +02:00
for idx,v in ipairs(tiles) do
local x,y = self:idx_xy(idx)
if v == 0 then
local neighbors = {}
-- 2 tiles on the left
if x >= 3 then
add(neighbors, {idx, idx-1, idx-2})
end
-- 2 tiles on the right
if x <= width-2 then
add(neighbors, {idx, idx+1, idx+2})
end
-- 2 tiles on top
if y >= 3 then
add(neighbors, {idx, idx-width, idx - width*2})
end
-- 2 tiles under
if y <= width-2 then
add(neighbors, {idx, idx+width, idx + width*2})
end
-- only keep pairs that are identical (and not 0)
neighbors = filter(neighbors, function (o) return tiles[o[2]] == tiles[o[3]] and tiles[o[2]] ~= 0 end)
-- do the surrounding
for item in all(neighbors) do
if item[1] then
if (debug) printh("Surrounding at " .. item[1])
self:fill(item[1], tiles[item[2]], true)
end
end
end
end
end,
2022-06-02 20:18:02 +02:00
split_triples = function(self)
2022-05-29 18:38:07 +02:00
for idx, col in ipairs(tiles) do
local x,y = self:idx_xy(idx)
if col == 0 then
if x > 1 and x < width then
-- check horizontal
local prev = tiles[idx-1]
local next = tiles[idx+1]
if prev ~= 0 and prev == next then
if (debug) printh("Splitting at " .. idx)
self:fill(idx, prev, true)
end
end
if y>1 and y < width then
-- check vertical
local prev = tiles[idx-width]
local next = tiles[idx+width]
if prev ~= 0 and prev == next then
if (debug) printh("Splitting at " .. idx)
self:fill(idx, prev, true)
end
end
end
end
end,
2022-06-02 20:18:02 +02:00
fill_lines = function(self)
local rows = self:get_rows()
local cols = self:get_cols()
2022-05-29 18:38:07 +02:00
-- rows
for y,row in ipairs(rows) do
local a = count(row, BLUE)
local b = count(row, YELLOW)
if a ~= b then
2022-06-02 20:18:02 +02:00
if a == width/2 then self:fill_row(y, YELLOW) end
if b == width/2 then self:fill_row(y, BLUE) end
2022-05-29 18:38:07 +02:00
end
end
-- columns
for x,col in ipairs(cols) do
local a = count(col, BLUE)
local b = count(col, YELLOW)
if a ~= b then
2022-06-02 20:18:02 +02:00
if a == width/2 then self:fill_col(x, YELLOW) end
if b == width/2 then self:fill_col(x, BLUE) end
2022-05-29 18:38:07 +02:00
end
end
end,
2022-06-02 20:18:02 +02:00
fill_row = function(self, y, color)
2022-05-29 18:38:07 +02:00
if (debug) printh("Filling line " .. y .. " in " .. (color == BLUE and "blue" or "yellow"))
local idx = self:xy_idx(1, y)
for i = idx, (idx+width-1) do
2022-06-02 20:18:02 +02:00
if self:get_tile(i) == 0 then
2022-05-29 18:38:07 +02:00
self:fill(i, color)
end
end
end,
2022-06-02 20:18:02 +02:00
fill_col = function(self, x, color)
2022-05-29 18:38:07 +02:00
if (debug) printh("Filling column " .. x .. " in " .. (color == BLUE and "blue" or "yellow"))
local idx = self:xy_idx(x, 1)
for i = idx, #tiles, width do
2022-06-02 20:18:02 +02:00
if self:get_tile(i) == 0 then
2022-05-29 18:38:07 +02:00
self:fill(i, color)
end
end
end,
2022-06-02 20:18:02 +02:00
no_identical_lines = function(self)
2022-05-29 18:38:07 +02:00
-- columns
2022-06-02 20:18:02 +02:00
local cols = self:get_cols()
2022-05-29 18:38:07 +02:00
for x,col in ipairs(cols) do
if count(col, 0) == 2 and count(col, BLUE) == count(col, YELLOW) then
local y1, y2 = unpack(find(col, 0))
local ab = copy(col) ab[y1] = BLUE ab[y2] = YELLOW
local ba = copy(col) ba[y1] = YELLOW ba[y2] = BLUE
-- Check if a dupe exists
for x2,col in ipairs(cols) do
if equal(col, ab) then
if debug then
printh("No-dupe at col " .. x .. " from col " .. x2)
printh(" " .. self:xy_idx(x,y1) .. " in yellow")
printh(" " .. self:xy_idx(x,y2) .. " in blue")
end
self:fill(self:xy_idx(x,y1), YELLOW)
self:fill(self:xy_idx(x,y2), BLUE)
goto continue
elseif equal(col, ba) then
if debug then
printh("No-dupe at col " .. x .. " from col " .. x2)
printh(" " .. self:xy_idx(x,y1) .. " in blue")
printh(" " .. self:xy_idx(x,y2) .. " in yellow")
end
self:fill(self:xy_idx(x,y1), BLUE)
self:fill(self:xy_idx(x,y2), YELLOW)
goto continue
end
end
end
::continue::
end
-- rows
2022-06-02 20:18:02 +02:00
local rows = self:get_rows()
2022-05-29 18:38:07 +02:00
for y,row in ipairs(rows) do
if count(row, 0) == 2 and count(row, BLUE) == count(row, YELLOW) then
local x1, x2 = unpack(find(row, 0))
local ab = copy(row) ab[x1] = BLUE ab[x2] = YELLOW
local ba = copy(row) ba[x1] = YELLOW ba[x2] = BLUE
-- Check if a dupe exists
for y2,row in ipairs(rows) do
if equal(row, ab) then
if debug then
printh("No-dupe at row " .. y .. " from row " .. y2)
printh(" " .. self:xy_idx(x1,y) .. " in yellow")
printh(" " .. self:xy_idx(x2,y) .. " in blue")
end
self:fill(self:xy_idx(x1,y), YELLOW)
self:fill(self:xy_idx(x2,y), BLUE)
goto continue
elseif equal(row, ba) then
if debug then
printh("No-dupe at row " .. y .. " from row " .. y2)
printh(" " .. self:xy_idx(x1,y) .. " in blue")
printh(" " .. self:xy_idx(x2,y) .. " in yellow")
end
self:fill(self:xy_idx(x1,y), BLUE)
self:fill(self:xy_idx(x2,y), YELLOW)
goto continue
end
end
end
::continue::
end
end,
draw = function(self)
2022-06-01 23:14:22 +02:00
local w = width
2022-05-29 18:38:07 +02:00
for k,v in ipairs(tiles) do
2022-06-01 23:14:22 +02:00
local x,y = self:draw_coords(k)
2022-05-29 22:55:43 +02:00
local color = v == BLUE and 12 or v == YELLOW and 8 or 1
if color == 1 then fillp() else fillp() end
2022-06-01 23:14:22 +02:00
rectfill2(x, y, w, w, color)
2022-05-29 18:38:07 +02:00
end
end
}
end