Advertisement
guitarplayer616

Sudoku Gen w/ Backtracking

Jun 5th, 2016
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.59 KB | None | 0 0
  1. local called = 0
  2. local quadcoords = {}
  3. local grid = {}
  4.  
  5. shell.run("delete console")
  6.  
  7. for y=1,7,3 do
  8.   for x=1,7,3 do
  9.     quadcoords[#quadcoords+1]={x,y}
  10.   end
  11. end
  12.  
  13. function printTab(t)
  14.   for i,v in pairs(t) do
  15.     print("i = ",i," v = ",v)
  16.   end
  17. end
  18.  
  19. function log(t)
  20.   h = fs.open("console","a")
  21.   if type(t) == "table" then
  22.    
  23.     for i,v in pairs(t) do
  24.       h.write("i = ")
  25.       h.write(i)
  26.       h.write(" v = ")
  27.       h.write(v)
  28.       h.write("\n")
  29.     end
  30.    
  31.   else
  32.     h.write(t)
  33.     h.write("\n")
  34.   end
  35.   h.close()
  36. end
  37.  
  38. --[[function createGrid()
  39.   term.blit(str.rep(" ",19),"7":rep(19)," ":rep(19))
  40. end]]
  41.  
  42. --[[function sort(tab)
  43.   local sorted = {}
  44.   for _,v in pairs(tab) do
  45.     if v then
  46.         table.insert(sorted,v)
  47.     end
  48.   end
  49.   return sorted
  50. end]]
  51.  
  52. function findQuadrant(x,y)
  53.   local quadrant = nil
  54.   if y<=3 then
  55.     if x<=3 then
  56.       quadrant = 1
  57.     elseif x<=6 then
  58.       quadrant = 2
  59.     elseif x<=9 then
  60.       quadrant = 3
  61.     end
  62.   elseif y<=6 then
  63.     if x<=3 then
  64.       quadrant = 4
  65.     elseif x<=6 then
  66.       quadrant = 5
  67.     elseif x<=9 then
  68.       quadrant = 6
  69.     end
  70.   elseif y<=9 then
  71.     if x<= 3 then
  72.       quadrant = 7
  73.     elseif x<=6 then
  74.       quadrant = 8
  75.     elseif x<=9 then
  76.       quadrant = 9
  77.     end
  78.   end
  79.  
  80.   if quadrant then
  81.     return quadrant
  82.   else
  83.     error()
  84.   end
  85. end
  86.  
  87. bad = {}
  88. choice = {}
  89. for i = 1,9 do
  90.   bad[i] = {}
  91.   choice[i] = {}
  92.   for j = 1,9 do
  93.     bad[i][j] = {}
  94.     choice[i][j] = {}
  95.   end
  96. end
  97.  
  98. function filter(b)
  99.   local t = {}
  100.   for n=1,9 do
  101.     t[n] = 0
  102.   end
  103.   for _,k in pairs(b) do
  104.     if t[k] then
  105.         t[k] = 1
  106.     end
  107.   end
  108.   for x = 1,9 do
  109.     if t[x] == 0 then
  110.         t[x] = x
  111.     else
  112.         t[x] = 0
  113.     end
  114.   end
  115.   return t
  116. end
  117.  
  118. function initGrid()
  119.   grid = {}
  120.   for i=1,9 do
  121.     grid[i] = {}
  122.     for j = 1,9 do
  123.       grid[i][j] = 0
  124.     end
  125.   end
  126. end
  127.  
  128. function findChoices(x,y)
  129.   local options
  130.   quadrant = findQuadrant(x,y)
  131.   table.insert(bad[x][y],"q"..quadrant)
  132.      
  133.   for i=1,9 do
  134.     if grid[i][y] ~= 0 then
  135.       table.insert(bad[x][y],grid[i][y])
  136.     end
  137.   end
  138.  
  139.      
  140.   for i=1,9 do
  141.     if grid[x][i] ~= 0 then
  142.       table.insert(bad[x][y],grid[x][i])
  143.     end
  144.   end
  145.  
  146.      
  147.   for n = quadcoords[quadrant][1],quadcoords[quadrant][1]+2 do
  148.     for m = quadcoords[quadrant][2],quadcoords[quadrant][2]+2 do
  149.       if grid[n][m] ~= 0 then
  150.         table.insert(bad[x][y],grid[n][m])
  151.       end
  152.     end
  153.   end
  154.  
  155.      
  156.   options = filter(bad[x][y])
  157.   --options = sort(options)
  158.   return options
  159. end
  160.  
  161. function isFinished()
  162.     for i = 1,9 do
  163.         for j = 1,9 do
  164.             if grid[i][j] == 0 then
  165.                 return false
  166.             end
  167.         end
  168.     end
  169.     return true
  170. end
  171.  
  172. function findVacant()
  173.     i = 0
  174.     j = 0
  175.    
  176.     for x = 1,9 do
  177.         for y = 1,9 do
  178.             if grid[x][y] == 0 then
  179.                 i = x
  180.                 j = y
  181.                 return i,j
  182.             end
  183.         end
  184.     end
  185. end
  186.  
  187. function engine()
  188.   called = called + 1
  189.   i = 0
  190.   j = 0
  191.   options = {}
  192.   solved = isFinished()
  193.   if solved then
  194.     log("finished")
  195.     printGrid()
  196.     error()
  197.   else
  198.    
  199.     i,j = findVacant()
  200.    
  201.     options = findChoices(i,j)
  202.     log("OPTIONS")
  203.     log(i)
  204.     log(j)
  205.     log(options)
  206.     for _,v in pairs(options) do
  207.         if v~=0 then
  208.             grid[i][j] = v
  209.             log("DECISION")
  210.             log(v)
  211.             engine(grid)
  212.         end
  213.     end
  214.    
  215.     grid[i][j] = 0
  216.     log("backtrack")
  217.   end
  218. end
  219.    
  220.  
  221. function printGrid()
  222.   term.clear()
  223.   for x=1,9 do
  224.     for y=1,9 do
  225.       term.setCursorPos(x+30,y)
  226.       term.write(grid[x][y])
  227.     end
  228.   end
  229.   term.setCursorPos(1,1)
  230. end
  231.  
  232.  
  233. initGrid()
  234. engine()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement