Indent with tabs
This commit is contained in:
parent
c0870b4831
commit
9ca9766799
314
main.lua
314
main.lua
|
@ -12,55 +12,55 @@ local YELLOW = 2
|
||||||
local a = BLUE
|
local a = BLUE
|
||||||
|
|
||||||
function idx_xy(idx, width)
|
function idx_xy(idx, width)
|
||||||
return (idx - 1) % width + 1, (idx - 1) \ width + 1
|
return (idx - 1) % width + 1, (idx - 1) \ width + 1
|
||||||
end
|
end
|
||||||
|
|
||||||
function xy_idx(x, y, width)
|
function xy_idx(x, y, width)
|
||||||
return ((y - 1) * width) + x
|
return ((y - 1) * width) + x
|
||||||
end
|
end
|
||||||
|
|
||||||
function map(tbl, f)
|
function map(tbl, f)
|
||||||
local t = {}
|
local t = {}
|
||||||
for k, v in pairs(tbl) do
|
for k, v in pairs(tbl) do
|
||||||
t[k] = f(v)
|
t[k] = f(v)
|
||||||
end
|
end
|
||||||
return t
|
return t
|
||||||
end
|
end
|
||||||
|
|
||||||
function filter(tbl, f, keepindex)
|
function filter(tbl, f, keepindex)
|
||||||
local ret = {}
|
local ret = {}
|
||||||
for k, v in pairs(tbl) do
|
for k, v in pairs(tbl) do
|
||||||
if f(v) then
|
if f(v) then
|
||||||
if keepindex
|
if keepindex
|
||||||
then ret[k] = v
|
then ret[k] = v
|
||||||
else add(ret, v)
|
else add(ret, v)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
return ret
|
return ret
|
||||||
end
|
end
|
||||||
|
|
||||||
function slice(tbl, first, last, step)
|
function slice(tbl, first, last, step)
|
||||||
local sliced = {}
|
local sliced = {}
|
||||||
for i = (first or 1), (last or #tbl), (step or 1) do
|
for i = (first or 1), (last or #tbl), (step or 1) do
|
||||||
sliced[(#sliced + 1)] = tbl[i]
|
sliced[(#sliced + 1)] = tbl[i]
|
||||||
end
|
end
|
||||||
return sliced
|
return sliced
|
||||||
end
|
end
|
||||||
|
|
||||||
local _rectfill = rectfill
|
local _rectfill = rectfill
|
||||||
function rectfill(x, y, w, h, col)
|
function rectfill(x, y, w, h, col)
|
||||||
_rectfill(x, y, x+w, y+h, col)
|
_rectfill(x, y, x+w, y+h, col)
|
||||||
end
|
end
|
||||||
|
|
||||||
local _count = count
|
local _count = count
|
||||||
function count(tbl, p)
|
function count(tbl, p)
|
||||||
if type(p) != "function" then return _count(tbl, p) end
|
if type(p) != "function" then return _count(tbl, p) end
|
||||||
local c = 0
|
local c = 0
|
||||||
for v in all(tbl) do
|
for v in all(tbl) do
|
||||||
if p(v) then c+=1 end
|
if p(v) then c+=1 end
|
||||||
end
|
end
|
||||||
return c
|
return c
|
||||||
end
|
end
|
||||||
|
|
||||||
--
|
--
|
||||||
|
@ -93,29 +93,29 @@ function Board.new()
|
||||||
return xy_idx(x, y, width)
|
return xy_idx(x, y, width)
|
||||||
end,
|
end,
|
||||||
|
|
||||||
fill = function(self, idx, color, invert)
|
fill = function(self, idx, color, invert)
|
||||||
if invert then
|
if invert then
|
||||||
color = color == YELLOW and BLUE or YELLOW
|
color = color == YELLOW and BLUE or YELLOW
|
||||||
end
|
end
|
||||||
tiles[idx] = color
|
tiles[idx] = color
|
||||||
end,
|
end,
|
||||||
|
|
||||||
getRows = function(self)
|
getRows = function(self)
|
||||||
local ret = {}
|
local ret = {}
|
||||||
for i = 1, width do
|
for i = 1, width do
|
||||||
add(ret, slice(tiles, ((i - 1) * width) + 1, i * width))
|
add(ret, slice(tiles, ((i - 1) * width) + 1, i * width))
|
||||||
end
|
end
|
||||||
return ret
|
return ret
|
||||||
end,
|
end,
|
||||||
|
|
||||||
getCols = function(self)
|
getCols = function(self)
|
||||||
local ret = {}
|
local ret = {}
|
||||||
local rows = self.getRows(self)
|
local rows = self.getRows(self)
|
||||||
for i = 1, width do
|
for i = 1, width do
|
||||||
add(ret, map(rows, function(v) return v[i] end))
|
add(ret, map(rows, function(v) return v[i] end))
|
||||||
end
|
end
|
||||||
return ret
|
return ret
|
||||||
end,
|
end,
|
||||||
|
|
||||||
solve = function(self)
|
solve = function(self)
|
||||||
self:surroundDoubles()
|
self:surroundDoubles()
|
||||||
|
@ -124,123 +124,123 @@ function Board.new()
|
||||||
self:noIdenticalLines()
|
self:noIdenticalLines()
|
||||||
end,
|
end,
|
||||||
|
|
||||||
surroundDoubles = function(self)
|
surroundDoubles = function(self)
|
||||||
for idx,v in ipairs(tiles) do
|
for idx,v in ipairs(tiles) do
|
||||||
local x,y = self:idx_xy(idx)
|
local x,y = self:idx_xy(idx)
|
||||||
if v == 0 then
|
if v == 0 then
|
||||||
local neighbors = {}
|
local neighbors = {}
|
||||||
-- 2 tiles on the left
|
-- 2 tiles on the left
|
||||||
if x >= 3 then
|
if x >= 3 then
|
||||||
add(neighbors, {idx, idx-1, idx-2})
|
add(neighbors, {idx, idx-1, idx-2})
|
||||||
end
|
end
|
||||||
-- 2 tiles on the right
|
-- 2 tiles on the right
|
||||||
if x <= width-2 then
|
if x <= width-2 then
|
||||||
add(neighbors, {idx, idx+1, idx+2})
|
add(neighbors, {idx, idx+1, idx+2})
|
||||||
end
|
end
|
||||||
-- 2 tiles on top
|
-- 2 tiles on top
|
||||||
if y >= 3 then
|
if y >= 3 then
|
||||||
add(neighbors, {idx, idx-width, idx - width*2})
|
add(neighbors, {idx, idx-width, idx - width*2})
|
||||||
end
|
end
|
||||||
-- 2 tiles under
|
-- 2 tiles under
|
||||||
if y <= width-2 then
|
if y <= width-2 then
|
||||||
add(neighbors, {idx, idx+width, idx + width*2})
|
add(neighbors, {idx, idx+width, idx + width*2})
|
||||||
end
|
end
|
||||||
|
|
||||||
-- only keep pairs that are identical (and not 0)
|
-- 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)
|
neighbors = filter(neighbors, function (o) return tiles[o[2]] == tiles[o[3]] and tiles[o[2]] ~= 0 end)
|
||||||
|
|
||||||
-- do the surrounding
|
-- do the surrounding
|
||||||
for item in all(neighbors) do
|
for item in all(neighbors) do
|
||||||
if item[1] then
|
if item[1] then
|
||||||
printh("Surrounding at " .. item[1])
|
printh("Surrounding at " .. item[1])
|
||||||
self:fill(item[1], tiles[item[2]], true)
|
self:fill(item[1], tiles[item[2]], true)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end,
|
end,
|
||||||
|
|
||||||
splitTriples = function(self)
|
splitTriples = function(self)
|
||||||
for idx, col in ipairs(tiles) do
|
for idx, col in ipairs(tiles) do
|
||||||
local x,y = self:idx_xy(idx)
|
local x,y = self:idx_xy(idx)
|
||||||
if col == 0 then
|
if col == 0 then
|
||||||
|
|
||||||
if x > 1 and x < width then
|
if x > 1 and x < width then
|
||||||
-- check horizontal
|
-- check horizontal
|
||||||
local prev = tiles[idx-1]
|
local prev = tiles[idx-1]
|
||||||
local next = tiles[idx+1]
|
local next = tiles[idx+1]
|
||||||
if prev ~= 0 and prev == next then
|
if prev ~= 0 and prev == next then
|
||||||
printh("Splitting at " .. idx)
|
printh("Splitting at " .. idx)
|
||||||
self:fill(idx, prev, true)
|
self:fill(idx, prev, true)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
if y>1 and y < width then
|
if y>1 and y < width then
|
||||||
-- check vertical
|
-- check vertical
|
||||||
local prev = tiles[idx-width]
|
local prev = tiles[idx-width]
|
||||||
local next = tiles[idx+width]
|
local next = tiles[idx+width]
|
||||||
if prev ~= 0 and prev == next then
|
if prev ~= 0 and prev == next then
|
||||||
printh("Splitting at " .. idx)
|
printh("Splitting at " .. idx)
|
||||||
self:fill(idx, prev, true)
|
self:fill(idx, prev, true)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end,
|
end,
|
||||||
|
|
||||||
fillLines = function(self)
|
fillLines = function(self)
|
||||||
local rows = self:getRows()
|
local rows = self:getRows()
|
||||||
local cols = self:getCols()
|
local cols = self:getCols()
|
||||||
|
|
||||||
-- rows
|
-- rows
|
||||||
for y,row in ipairs(rows) do
|
for y,row in ipairs(rows) do
|
||||||
local a = count(row, function (a) return a == BLUE end)
|
local a = count(row, BLUE)
|
||||||
local b = count(row, function (a) return a == YELLOW end)
|
local b = count(row, YELLOW)
|
||||||
if a ~= b then
|
if a ~= b then
|
||||||
if a == width/2 then self:fillRow(y, YELLOW) end
|
if a == width/2 then self:fillRow(y, YELLOW) end
|
||||||
if b == width/2 then self:fillRow(y, BLUE) end
|
if b == width/2 then self:fillRow(y, BLUE) end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
-- columns
|
-- columns
|
||||||
for x,col in ipairs(cols) do
|
for x,col in ipairs(cols) do
|
||||||
local a = count(col, function (a) return a == BLUE end)
|
local a = count(col, BLUE)
|
||||||
local b = count(col, function (a) return a == YELLOW end)
|
local b = count(col, YELLOW)
|
||||||
if a ~= b then
|
if a ~= b then
|
||||||
if a == width/2 then self:fillCol(x, YELLOW) end
|
if a == width/2 then self:fillCol(x, YELLOW) end
|
||||||
if b == width/2 then self:fillCol(x, BLUE) end
|
if b == width/2 then self:fillCol(x, BLUE) end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end,
|
end,
|
||||||
|
|
||||||
fillRow = function(self, y, color)
|
fillRow = function(self, y, color)
|
||||||
printh("Filling line " .. y .. " in " .. color == BLUE and "blue" or "yellow")
|
printh("Filling line " .. y .. " in " .. color == BLUE and "blue" or "yellow")
|
||||||
local idx = self:xy_idx(1, y)
|
local idx = self:xy_idx(1, y)
|
||||||
for i = idx, (idx+width-1) do
|
for i = idx, (idx+width-1) do
|
||||||
if self:getTile(i) == 0 then
|
if self:getTile(i) == 0 then
|
||||||
self:fill(i, color)
|
self:fill(i, color)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end,
|
end,
|
||||||
|
|
||||||
fillCol = function(self, x, color)
|
fillCol = function(self, x, color)
|
||||||
printh("Filling column " .. x .. " in " .. color == BLUE and "blue" or "yellow")
|
printh("Filling column " .. x .. " in " .. color == BLUE and "blue" or "yellow")
|
||||||
local idx = self:xy_idx(x, 1)
|
local idx = self:xy_idx(x, 1)
|
||||||
for i = idx, #tiles, width do
|
for i = idx, #tiles, width do
|
||||||
if self:getTile(i) == 0 then
|
if self:getTile(i) == 0 then
|
||||||
self:fill(i, color)
|
self:fill(i, color)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end,
|
end,
|
||||||
|
|
||||||
noIdenticalLines = function(self)
|
noIdenticalLines = function(self)
|
||||||
end,
|
end,
|
||||||
|
|
||||||
draw = function(self)
|
draw = function(self)
|
||||||
for k,v in ipairs(tiles) do
|
for k,v in ipairs(tiles) do
|
||||||
local x,y = self:idx_xy(k)
|
local x,y = self:idx_xy(k)
|
||||||
local color = v == BLUE and 12 or v == YELLOW and 10 or 1
|
local color = v == BLUE and 12 or v == YELLOW and 10 or 1
|
||||||
rectfill((x-1)*10 + x*8 + 4, (y-1)*10 + y*8 + 4, 12, 12, color)
|
rectfill((x-1)*10 + x*8 + 4, (y-1)*10 + y*8 + 4, 12, 12, color)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
}
|
}
|
||||||
|
@ -259,9 +259,9 @@ function _update()
|
||||||
end
|
end
|
||||||
|
|
||||||
function _draw()
|
function _draw()
|
||||||
cls()
|
cls()
|
||||||
if time() % 1 == 0 then
|
if time() % 1 == 0 then
|
||||||
board:solve()
|
board:solve()
|
||||||
end
|
end
|
||||||
board:draw()
|
board:draw()
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue
Block a user