Advertisement
guitarplayer616

Sudoku Solver 9x9 [WIP]

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