guitarplayer616

Sudoku Generator [WIP]

Jun 4th, 2016
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.89 KB | None | 0 0
  1. local xmax,ymax = term.getSize()
  2. local quadcoords = {}
  3.  
  4. for y=1,7,3 do
  5.   for x=1,7,3 do
  6.     quadcoords[#quadcoords+1]={x,y}
  7.   end
  8. end
  9.  
  10. function printTab(t)
  11.   for i,v in pairs(t) do
  12.     print("i = ",i," v = ",v)
  13.   end
  14. end
  15.  
  16. function log(t)
  17.   local h = fs.open("console","a")
  18.   if type(t) == "table" then
  19.     for i,v in pairs(t) do
  20.       h.write("i = ",i," v = ",v)
  21.       h.write("\n")
  22.     end
  23.   else
  24.     h.write(t)
  25.     h.write("\n")
  26.   end
  27.   h.close()
  28. end
  29.  
  30. --[[function createGrid()
  31.   term.blit(str.rep(" ",19),"7":rep(19)," ":rep(19))
  32. end]]
  33. function sort(tab)
  34.   local sorted = {}
  35.   for _,v in pairs(tab) do
  36.     if v then
  37.         table.insert(sorted,v)
  38.     end
  39.   end
  40.   return sorted
  41. end
  42.  
  43. function findQuadrant(x,y)
  44.   local quadrant = nil
  45.   if y<=3 then
  46.     if x<=3 then
  47.       quadrant = 1
  48.     elseif x<=6 then
  49.       quadrant = 2
  50.     elseif x<=9 then
  51.       quadrant = 3
  52.     end
  53.   elseif y<=6 then
  54.     if x<=3 then
  55.       quadrant = 4
  56.     elseif x<=6 then
  57.       quadrant = 5
  58.     elseif x<=9 then
  59.       quadrant = 6
  60.     end
  61.   elseif y<=9 then
  62.     if x<= 3 then
  63.       quadrant = 7
  64.     elseif x<=6 then
  65.       quadrant = 8
  66.     elseif x<=9 then
  67.       quadrant = 9
  68.     end
  69.   end
  70.  
  71.   if quadrant then
  72.     return quadrant
  73.   else
  74.     error()
  75.   end
  76. end
  77.  
  78. bad = {}
  79. choice = {}
  80. for i = 1,9 do
  81.   bad[i] = {}
  82.   choice[i] = {}
  83.   for j = 1,9 do
  84.     bad[i][j] = {}
  85.     choice[i][j] = {}
  86.   end
  87. end
  88.  
  89. function filter(n)
  90.   local t = {1,2,3,4,5,6,7,8,9}
  91.   for i,v in pairs(t) do
  92.     for _,k in pairs(n) do
  93.       if v == k then
  94.         t[i] = nil
  95.       end
  96.     end
  97.   end
  98.   return t
  99. end
  100.  
  101. function initGrid()
  102.   grid = {}
  103.   for i=1,9 do
  104.     grid[i] = {}
  105.     for j = 1,9 do
  106.       grid[i][j] = 0
  107.     end
  108.   end
  109. end
  110.  
  111. function findChoices(x,y)
  112.   local options
  113.   quadrant = findQuadrant(x,y)
  114.   table.insert(bad[x][y],"q"..quadrant)
  115.      
  116.   for i=1,9 do
  117.     if grid[i][y] ~= 0 then
  118.       table.insert(bad[x][y],grid[i][y])
  119.     end
  120.   end
  121.  
  122.      
  123.   for i=1,9 do
  124.     if grid[x][i] ~= 0 then
  125.       table.insert(bad[x][y],grid[x][i])
  126.     end
  127.   end
  128.  
  129.      
  130.   for n = quadcoords[quadrant][1],quadcoords[quadrant][1]+2 do
  131.     for m = quadcoords[quadrant][2],quadcoords[quadrant][2]+2 do
  132.       if grid[n][m] ~= 0 then
  133.         table.insert(bad[x][y],grid[n][m])
  134.       end
  135.     end
  136.   end
  137.  
  138.      
  139.   options = filter(bad[x][y])
  140.   options = sort(options)
  141.   return options
  142. end
  143.  
  144. function engine()
  145.  
  146.   for x = 1,9 do
  147.     for y = 1,9 do
  148.      
  149.       choice[x][y] = findChoices(x,y)
  150.       if #choice[x][y] ~= 0 then
  151.         grid[x][y] = choice[x][y][math.random(#choice[x][y])]
  152.       else
  153.         grid[x][y] = 'x'
  154.       end
  155.     end
  156.  
  157.   end
  158. end
  159.  
  160. function printGrid()
  161.   term.clear()
  162.   for x=1,9 do
  163.     for y=1,9 do
  164.       term.setCursorPos(x+30,y)
  165.       term.write(grid[x][y])
  166.     end
  167.   end
  168.   term.setCursorPos(1,1)
  169. end
  170.  
  171. initGrid()
  172. engine()
  173. printGrid()
Add Comment
Please, Sign In to add comment