Procedural generation ok

This commit is contained in:
Simon Cambier 2022-05-28 17:32:43 +02:00
parent d7ac47ce24
commit f47ed840cf

162
main.lua
View File

@ -1,3 +1,5 @@
poke(0x5F2D, 3)
--
-- constants
--
@ -91,21 +93,49 @@ end
local Board = {}
function Board.new()
local width = 10
-- local t = [[
-- 2,0,2,0,0,2,1,0,1,2,
-- 0,0,0,0,0,0,0,0,0,1,
-- 0,1,0,0,0,0,0,0,0,0,
-- 0,2,0,0,0,0,0,1,0,2,
-- 0,0,1,0,0,2,0,1,0,1,
-- 0,0,0,0,1,1,0,0,0,0,
-- 0,2,2,0,0,0,0,0,0,2,
-- 0,0,0,0,2,0,0,0,0,0,
-- 0,0,2,0,0,1,0,0,0,0,
-- 0,2,0,0,2,0,2,0,0,0
-- ]]
local t = [[
2,0,2,0,0,2,1,0,1,2,
0,0,0,0,0,0,0,0,0,1,
0,1,0,0,0,0,0,0,0,0,
0,2,0,0,0,0,0,1,0,2,
0,0,1,0,0,2,0,1,0,1,
0,0,0,0,1,1,0,0,0,0,
0,2,2,0,0,0,0,0,0,2,
0,0,0,0,2,0,0,0,0,0,
0,0,2,0,0,1,0,0,0,0,
0,2,0,0,2,0,2,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,0,0,0
]]
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,
getTile = function(self, idx)
return tiles[idx]
end,
@ -142,11 +172,101 @@ function Board.new()
return ret
end,
solve = function(self)
isComplete = function(self)
return count(tiles, 0) == 0
end,
isValid = function(self)
local rows = self:getRows()
for row in all(rows) do
-- check count
if count(row, BLUE) ~= count(row, YELLOW) then
printh("uneven count")
return false
end
-- check identical lines
for k,other in ipairs(rows) do
if equal(other, row) and other ~= row then
printh("equal rows "..k)
return false
end
end
-- check triples
if self:countConsecutives(row) > 2 then
printh("triples")
return false
end
end
local cols = self:getCols()
for col in all(cols) do
-- check count
if count(col, BLUE) ~= count(col, YELLOW) then
printh("uneven count")
return false
end
-- check identical lines
for other in all(cols) do
if equal(other, col) and other ~= col then
printh("equal cols")
return false
end
end
-- check triples
if self:countConsecutives(col) > 2 then
printh("triples")
return false
end
end
return true
end,
countConsecutives = function(self, line)
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
getRandomZero = function(self, v)
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,
tostring = function(self)
local str = ''
for v in all(tiles) do
str ..= ", " .. v
end
return str
end,
solve = function(self, random)
local old = self:tostring()
self:surroundDoubles()
self:splitTriples()
self:fillLines()
self:noIdenticalLines()
local changed = old ~= self:tostring()
if not changed and random and not self:isComplete() then
-- Set a random color
local z = self:getRandomZero()
self:fill(z, rnd({BLUE, YELLOW}))
printh("!!!!!!!!!!!!!!!!! RANDOM FILL AT " .. z)
end
end,
surroundDoubles = function(self)
@ -321,7 +441,7 @@ function Board.new()
draw = function(self)
for k,v in ipairs(tiles) do
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 9 or v == YELLOW and 3 or 1
local w = 10
local p = 1
rectfill((x-1)*w + (x-1)*(p+1), (y-1)*w + (y-1)*(p+1), w, w, color)
@ -340,15 +460,21 @@ local board = Board.new()
function _init()
end
function _update()
function _update60()
board:solve(true)
-- if time() % .01 == 0 then
-- end
if board:isComplete() then
if not board:isValid() then
printh("NOT VALID")
board:reset()
end
end
end
function _draw()
cls()
if time() % .5 == 0 then
printh("==")
printh("==")
board:solve()
end
board:draw()
circfill(stat(32), stat(33), 0, 7)
circ(stat(32), stat(33), 1, 0)
end