Procedural generation ok
This commit is contained in:
parent
d7ac47ce24
commit
f47ed840cf
164
main.lua
164
main.lua
|
@ -1,3 +1,5 @@
|
||||||
|
poke(0x5F2D, 3)
|
||||||
|
|
||||||
--
|
--
|
||||||
-- constants
|
-- constants
|
||||||
--
|
--
|
||||||
|
@ -91,21 +93,49 @@ end
|
||||||
local Board = {}
|
local Board = {}
|
||||||
function Board.new()
|
function Board.new()
|
||||||
local width = 10
|
local width = 10
|
||||||
local t = [[
|
-- local t = [[
|
||||||
2,0,2,0,0,2,1,0,1,2,
|
-- 2,0,2,0,0,2,1,0,1,2,
|
||||||
0,0,0,0,0,0,0,0,0,1,
|
-- 0,0,0,0,0,0,0,0,0,1,
|
||||||
0,1,0,0,0,0,0,0,0,0,
|
-- 0,1,0,0,0,0,0,0,0,0,
|
||||||
0,2,0,0,0,0,0,1,0,2,
|
-- 0,2,0,0,0,0,0,1,0,2,
|
||||||
0,0,1,0,0,2,0,1,0,1,
|
-- 0,0,1,0,0,2,0,1,0,1,
|
||||||
0,0,0,0,1,1,0,0,0,0,
|
-- 0,0,0,0,1,1,0,0,0,0,
|
||||||
0,2,2,0,0,0,0,0,0,2,
|
-- 0,2,2,0,0,0,0,0,0,2,
|
||||||
0,0,0,0,2,0,0,0,0,0,
|
-- 0,0,0,0,2,0,0,0,0,0,
|
||||||
0,0,2,0,0,1,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,2,0,0,2,0,2,0,0,0
|
||||||
|
-- ]]
|
||||||
|
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
|
||||||
]]
|
]]
|
||||||
local tiles = split(t)
|
local tiles = split(t)
|
||||||
|
|
||||||
return {
|
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)
|
getTile = function(self, idx)
|
||||||
return tiles[idx]
|
return tiles[idx]
|
||||||
end,
|
end,
|
||||||
|
@ -142,11 +172,101 @@ function Board.new()
|
||||||
return ret
|
return ret
|
||||||
end,
|
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:surroundDoubles()
|
||||||
self:splitTriples()
|
self:splitTriples()
|
||||||
self:fillLines()
|
self:fillLines()
|
||||||
self:noIdenticalLines()
|
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,
|
end,
|
||||||
|
|
||||||
surroundDoubles = function(self)
|
surroundDoubles = function(self)
|
||||||
|
@ -321,7 +441,7 @@ function Board.new()
|
||||||
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 9 or v == YELLOW and 3 or 1
|
||||||
local w = 10
|
local w = 10
|
||||||
local p = 1
|
local p = 1
|
||||||
rectfill((x-1)*w + (x-1)*(p+1), (y-1)*w + (y-1)*(p+1), w, w, color)
|
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()
|
function _init()
|
||||||
end
|
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
|
end
|
||||||
|
|
||||||
function _draw()
|
function _draw()
|
||||||
cls()
|
cls()
|
||||||
if time() % .5 == 0 then
|
|
||||||
printh("==")
|
|
||||||
printh("==")
|
|
||||||
board:solve()
|
|
||||||
end
|
|
||||||
board:draw()
|
board:draw()
|
||||||
|
circfill(stat(32), stat(33), 0, 7)
|
||||||
|
circ(stat(32), stat(33), 1, 0)
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue
Block a user