Advertisement
CaptainSpaceCat

Sudoku Puzzle Generator/Sovler

Jun 3rd, 2016
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.59 KB | None | 0 0
  1. ----------------------------------------------MISC FUNCS
  2. function logBoard(board, currentPos)
  3.     for y = 1, 9 do
  4.     term.setCursorPos(1, y + math.ceil(y/3) - 1)
  5.         for x = 1, 9 do
  6.             if tonumber(board[y][x]) then
  7.                 term.setTextColor(colors.red)
  8.                 write(board[y][x])
  9.             else
  10.                 term.setTextColor(colors.white)
  11.                 write("0")
  12.             end
  13.             if x == 3 or x == 6 then
  14.                 write(" ")
  15.             end
  16.         end
  17.     end
  18.     if currentPos then
  19.         term.setTextColor(colors.orange)
  20.         term.setCursorPos(currentPos[2] + math.floor((currentPos[2]-1)/3), currentPos[1] + math.floor((currentPos[1]-1)/3))
  21.         term.write("*")
  22.     end
  23.     print("")
  24. end
  25.  
  26. function printBoard(board)
  27.     local c = 0
  28.     for y = 1, 13 do
  29.         term.setCursorPos(1, y)
  30.         if y == 5 or y == 9 then
  31.             term.write("|---+---+---|")
  32.             c = c + 1
  33.         elseif y == 1 or y == 13 then
  34.             term.write("=============")
  35.             c = c + 1
  36.         else
  37.             term.write("|")
  38.             for x = 1, 9 do
  39.                 if tonumber(board[y-c][x]) then
  40.                     term.setTextColor(colors.red)
  41.                     write(board[y-c][x])
  42.                 else
  43.                     term.setTextColor(colors.white)
  44.                     write("0")
  45.                 end
  46.                 if x == 3 or x == 6 then
  47.                     write("|")
  48.                 end
  49.             end
  50.             term.write("|")
  51.         end
  52.     end
  53.     term.setCursorPos(1, 13)
  54.     term.write(interString)
  55.     print("")
  56. end
  57.  
  58. ----------------------------------------------GENERATION
  59. function initializeBoard(board)
  60.     for y = 1, 9 do
  61.         board[y] = {}
  62.         for x = 1, 9 do
  63.             board[y][x] = {1, 2, 3, 4, 5, 6, 7, 8, 9}
  64.         end
  65.     end
  66. end
  67.  
  68. function generatePuzzle(board)
  69.     cellsLeft = {}
  70.     for i = 1, 81 do
  71.         cellsLeft[i] = i
  72.     end
  73.     while #cellsLeft > 0 do
  74.         currentCell = table.remove(cellsLeft, math.random(1, #cellsLeft))
  75.         currentCell = {math.ceil(currentCell/9), (currentCell-1)%9 + 1}    --y, x
  76.         chooseCellNum(currentCell, board)
  77.     end
  78. end
  79.  
  80. function chooseCellNum(cell, board)
  81.     --print(cell[1] .. " " .. cell[2])
  82.     printBoard(board, cell)
  83.     local numsDetected = {}
  84.     for i = 1, 9 do
  85.         if tonumber(board[cell[1]][i]) then
  86.             table.insert(numsDetected, board[cell[1]][i])
  87.         end                                     --scan rows/columns for pre-existing numbers
  88.         if tonumber(board[i][cell[2]]) then
  89.             table.insert(numsDetected, board[i][cell[2]])
  90.         end
  91.     end
  92.     for y = 1 + (math.ceil(cell[1]/3)-1)*3, 3 + (math.ceil(cell[1]/3)-1)*3 do  --scan the surrounding box
  93.         for x = 1 + (math.ceil(cell[2]/3)-1)*3, 3 + (math.ceil(cell[2]/3)-1)*3 do
  94.             --print (y .. " " .. x)
  95.             if tonumber(board[y][x]) then
  96.                 table.insert(numsDetected, board[y][x])
  97.             end
  98.         end
  99.     end
  100.     table.sort(numsDetected)
  101.     for i = #numsDetected, 2, -1 do
  102.         if numsDetected[i] == numsDetected[i-1] then
  103.             table.remove(numsDetected, i)   --ensure each element occurs only once
  104.         end
  105.     end
  106.     local possibleNums = {}
  107.     local index = 1
  108.     for i = 1, 9 do
  109.         if numsDetected[index] ~= i then
  110.             table.insert(possibleNums, i)
  111.         else
  112.             index = index + 1
  113.         end
  114.     end
  115.     sleep(.2)
  116.     if #possibleNums == 0 then
  117.         term.setBackgroundColor(colors.black)
  118.         term.setTextColor(colors.white)
  119.         printBoard(board, cell)
  120.         --print(cell[1] .. ":" .. cell[2])
  121.         term.setCursorPos(1, 12)
  122.     end
  123.     local result = possibleNums[math.random(1, #possibleNums)]
  124.     board[cell[1]][cell[2]] = result  --choose one of the renaiming possible nums
  125. end
  126.  
  127. ----------------------------------------------SOLUTION
  128.  
  129. ----------------------------------------------ENGINE
  130. local tArgs = {...}
  131. term.setBackgroundColor(colors.black)
  132. term.setTextColor(colors.white)
  133. term.clear()
  134.  
  135. if tArgs[1] == "generate" or tArgs[1] == "gen" then
  136.     local board = {}
  137.     initializeBoard(board)
  138.     printBoard(board)
  139.     generatePuzzle(board)
  140.     printBoard(board)
  141. elseif tArgs[1] == "solve" then
  142.    
  143. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement