Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local called = 0
- local quadcoords = {}
- local grid = {}
- shell.run("delete console")
- for y=1,7,3 do
- for x=1,7,3 do
- quadcoords[#quadcoords+1]={x,y}
- end
- end
- function printTab(t)
- for i,v in pairs(t) do
- print("i = ",i," v = ",v)
- end
- end
- function log(t)
- h = fs.open("console","a")
- if type(t) == "table" then
- for i,v in pairs(t) do
- h.write("i = ")
- h.write(i)
- h.write(" v = ")
- h.write(v)
- h.write("\n")
- end
- else
- h.write(t)
- h.write("\n")
- end
- h.close()
- end
- --[[function createGrid()
- term.blit(str.rep(" ",19),"7":rep(19)," ":rep(19))
- end]]
- --[[function sort(tab)
- local sorted = {}
- for _,v in pairs(tab) do
- if v then
- table.insert(sorted,v)
- end
- end
- return sorted
- end]]
- function findQuadrant(x,y)
- local quadrant = nil
- if y<=3 then
- if x<=3 then
- quadrant = 1
- elseif x<=6 then
- quadrant = 2
- elseif x<=9 then
- quadrant = 3
- end
- elseif y<=6 then
- if x<=3 then
- quadrant = 4
- elseif x<=6 then
- quadrant = 5
- elseif x<=9 then
- quadrant = 6
- end
- elseif y<=9 then
- if x<= 3 then
- quadrant = 7
- elseif x<=6 then
- quadrant = 8
- elseif x<=9 then
- quadrant = 9
- end
- end
- if quadrant then
- return quadrant
- else
- error()
- end
- end
- bad = {}
- choice = {}
- for i = 1,9 do
- bad[i] = {}
- choice[i] = {}
- for j = 1,9 do
- bad[i][j] = {}
- choice[i][j] = {}
- end
- end
- function filter(b)
- local t = {}
- for n=1,9 do
- t[n] = 0
- end
- for _,k in pairs(b) do
- if t[k] then
- t[k] = 1
- end
- end
- for x = 1,9 do
- if t[x] == 0 then
- t[x] = x
- else
- t[x] = 0
- end
- end
- return t
- end
- function initGrid()
- grid = {}
- for i=1,9 do
- grid[i] = {}
- for j = 1,9 do
- grid[i][j] = 0
- end
- end
- end
- function findChoices(x,y)
- local options
- quadrant = findQuadrant(x,y)
- table.insert(bad[x][y],"q"..quadrant)
- for i=1,9 do
- if grid[i][y] ~= 0 then
- table.insert(bad[x][y],grid[i][y])
- end
- end
- for i=1,9 do
- if grid[x][i] ~= 0 then
- table.insert(bad[x][y],grid[x][i])
- end
- end
- for n = quadcoords[quadrant][1],quadcoords[quadrant][1]+2 do
- for m = quadcoords[quadrant][2],quadcoords[quadrant][2]+2 do
- if grid[n][m] ~= 0 then
- table.insert(bad[x][y],grid[n][m])
- end
- end
- end
- options = filter(bad[x][y])
- --options = sort(options)
- return options
- end
- function isFinished()
- for i = 1,9 do
- for j = 1,9 do
- if grid[i][j] == 0 then
- return false
- end
- end
- end
- return true
- end
- function findVacant()
- i = 0
- j = 0
- for x = 1,9 do
- for y = 1,9 do
- if grid[x][y] == 0 then
- i = x
- j = y
- return i,j
- end
- end
- end
- end
- function engine()
- called = called + 1
- i = 0
- j = 0
- options = {}
- solved = isFinished()
- if solved then
- log("finished")
- printGrid()
- error()
- else
- i,j = findVacant()
- options = findChoices(i,j)
- log("OPTIONS")
- log(i)
- log(j)
- log(options)
- for _,v in pairs(options) do
- if v~=0 then
- grid[i][j] = v
- log("DECISION")
- log(v)
- engine(grid)
- end
- end
- grid[i][j] = 0
- log("backtrack")
- end
- end
- function printGrid()
- term.clear()
- for x=1,9 do
- for y=1,9 do
- term.setCursorPos(x+30,y)
- term.write(grid[x][y])
- end
- end
- term.setCursorPos(1,1)
- end
- initGrid()
- engine()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement