Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ----------------------------------------------MISC FUNCS
- function logBoard(board, currentPos)
- for y = 1, 9 do
- term.setCursorPos(1, y + math.ceil(y/3) - 1)
- for x = 1, 9 do
- if tonumber(board[y][x]) then
- term.setTextColor(colors.red)
- write(board[y][x])
- else
- term.setTextColor(colors.white)
- write("0")
- end
- if x == 3 or x == 6 then
- write(" ")
- end
- end
- end
- if currentPos then
- term.setTextColor(colors.orange)
- term.setCursorPos(currentPos[2] + math.floor((currentPos[2]-1)/3), currentPos[1] + math.floor((currentPos[1]-1)/3))
- term.write("*")
- end
- print("")
- end
- function printBoard(board)
- local c = 0
- for y = 1, 13 do
- term.setCursorPos(1, y)
- if y == 5 or y == 9 then
- term.write("|---+---+---|")
- c = c + 1
- elseif y == 1 or y == 13 then
- term.write("=============")
- c = c + 1
- else
- term.write("|")
- for x = 1, 9 do
- if tonumber(board[y-c][x]) then
- term.setTextColor(colors.red)
- write(board[y-c][x])
- else
- term.setTextColor(colors.white)
- write("0")
- end
- if x == 3 or x == 6 then
- write("|")
- end
- end
- term.write("|")
- end
- end
- term.setCursorPos(1, 13)
- term.write(interString)
- print("")
- end
- ----------------------------------------------GENERATION
- function initializeBoard(board)
- for y = 1, 9 do
- board[y] = {}
- for x = 1, 9 do
- board[y][x] = {1, 2, 3, 4, 5, 6, 7, 8, 9}
- end
- end
- end
- function generatePuzzle(board)
- cellsLeft = {}
- for i = 1, 81 do
- cellsLeft[i] = i
- end
- while #cellsLeft > 0 do
- currentCell = table.remove(cellsLeft, math.random(1, #cellsLeft))
- currentCell = {math.ceil(currentCell/9), (currentCell-1)%9 + 1} --y, x
- chooseCellNum(currentCell, board)
- end
- end
- function chooseCellNum(cell, board)
- --print(cell[1] .. " " .. cell[2])
- printBoard(board, cell)
- local numsDetected = {}
- for i = 1, 9 do
- if tonumber(board[cell[1]][i]) then
- table.insert(numsDetected, board[cell[1]][i])
- end --scan rows/columns for pre-existing numbers
- if tonumber(board[i][cell[2]]) then
- table.insert(numsDetected, board[i][cell[2]])
- end
- end
- for y = 1 + (math.ceil(cell[1]/3)-1)*3, 3 + (math.ceil(cell[1]/3)-1)*3 do --scan the surrounding box
- for x = 1 + (math.ceil(cell[2]/3)-1)*3, 3 + (math.ceil(cell[2]/3)-1)*3 do
- --print (y .. " " .. x)
- if tonumber(board[y][x]) then
- table.insert(numsDetected, board[y][x])
- end
- end
- end
- table.sort(numsDetected)
- for i = #numsDetected, 2, -1 do
- if numsDetected[i] == numsDetected[i-1] then
- table.remove(numsDetected, i) --ensure each element occurs only once
- end
- end
- local possibleNums = {}
- local index = 1
- for i = 1, 9 do
- if numsDetected[index] ~= i then
- table.insert(possibleNums, i)
- else
- index = index + 1
- end
- end
- sleep(.2)
- if #possibleNums == 0 then
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- printBoard(board, cell)
- --print(cell[1] .. ":" .. cell[2])
- term.setCursorPos(1, 12)
- end
- local result = possibleNums[math.random(1, #possibleNums)]
- board[cell[1]][cell[2]] = result --choose one of the renaiming possible nums
- end
- ----------------------------------------------SOLUTION
- ----------------------------------------------ENGINE
- local tArgs = {...}
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- term.clear()
- if tArgs[1] == "generate" or tArgs[1] == "gen" then
- local board = {}
- initializeBoard(board)
- printBoard(board)
- generatePuzzle(board)
- printBoard(board)
- elseif tArgs[1] == "solve" then
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement