Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ----------------------------------------------MISC FUNCS
- if fs.exists("console") then
- shell.run("delete console")
- end
- local debug = false
- local h = fs.open("console","a")
- function log(t)
- if debug then
- if type(t) == "table" then
- for i,v in pairs(t) do
- h.write("i = ")
- h.write(i)
- h.write(" v = ")
- h.write(v)
- h.write("\n")
- end
- else
- h.write(t)
- h.write("\n")
- end
- h.flush()
- end
- end
- function logBoard(board, currentPos, futurePos)
- if debug then
- 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[1] + math.floor((currentPos[1]-1)/3), currentPos[2] + math.floor((currentPos[2]-1)/3))
- term.write(board[currentPos[2]][currentPos[1]])
- end
- if futurePos then
- term.setTextColor(colors.lime)
- term.setCursorPos(futurePos[1] + math.floor((futurePos[1]-1)/3), currentPos[2] + math.floor((futurePos[2]-1)/3))
- term.write("*")
- os.pullEvent("mouse_click")
- end
- print("")
- sleep(.1)
- end
- end
- function printBoard(board)
- 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
- print("")
- end
- ----------------------------------------------GENERATION
- function initializeBoard(board)
- for y = 1, 9 do
- board[y] = {}
- end
- end
- function generatePuzzle(board)
- for i = 1, 9 do --x --y
- log("-=-=-=-=-=-=-=-=-=-=-=-=-=-=-" .. i)
- local refTab = {{{1, 2, 3}, {1, 2, 3}, {1, 2, 3}}, {{1, 2, 3}, {1, 2, 3}, {1, 2, 3}}}
- for y = 1, 3 do
- for x = 1, 3 do
- local availableCells = getAvailableCells(x, y, refTab, board)
- local yOff = 0
- local xOff = 0
- for loop = 1, 4 do
- if #availableCells == 0 and loop%2 == 1 then
- log("No possible cells... reshuffling Y...")
- for recurY = y-1-yOff, 1, -1 do
- local iPosLocal, iPosGlobal = iLocate(i, x, recurY, board)
- local options = {}
- for n = 1 + (x-1)*3, 3 + (x-1)*3 do
- if not board[iPosGlobal[2]][n] and refTab[1][x][n - (x-1)*3] ~= 0 then --(recurY-1)*3 +
- table.insert(options, {n, iPosGlobal[2]})
- end
- end
- if #options > 0 then
- local choice = options[math.random(1, #options)]
- logBoard(board, iPosGlobal, choice)
- board[iPosGlobal[2]][iPosGlobal[1]] = nil
- board[choice[2]][choice[1]] = i
- log("Moving " .. iPosGlobal[1] .. ":" .. iPosGlobal[2] .. " to " .. choice[1] .. ":" .. choice[2])
- refTab[1][math.ceil(choice[1]/3)][iPosLocal[1]] = 1
- refTab[1][math.ceil(choice[1]/3)][(choice[1]-1)%3+1] = 0
- yOff = yOff + 1
- break;
- end
- end
- elseif #availableCells == 0 and loop%2 == 0 then
- log("No possible cells... reshuffling X...")
- for recurX = x-1-xOff, 1, -1 do
- local iPosLocal, iPosGlobal = iLocate(i, recurX, y, board)
- local options = {}
- for n = 1 + (y-1)*3, 3 + (y-1)*3 do
- if not board[n][iPosGlobal[1]] and refTab[2][y][n - (y-1)*3] ~= 0 then --(recurY-1)*3 +
- table.insert(options, {iPosGlobal[1], n}) ---not sure
- end
- end
- if #options > 0 then
- local choice = options[math.random(1, #options)]
- logBoard(board, iPosGlobal, choice)
- board[iPosGlobal[2]][iPosGlobal[1]] = nil
- board[choice[2]][choice[1]] = i
- log("Moving " .. iPosGlobal[1] .. ":" .. iPosGlobal[2] .. " to " .. choice[1] .. ":" .. choice[2])
- refTab[2][math.ceil(choice[2]/3)][iPosLocal[2]] = 1
- refTab[2][math.ceil(choice[2]/3)][(choice[2]-1)%3+1] = 0
- xOff = xOff + 1
- break;
- end
- end
- end
- end
- if #availableCells == 0 then
- availableCells = getAvailableCells(x, y, refTab, board)
- printBoard(board)
- end
- log(x .. "|" .. y .. "============ " .. #availableCells)
- for i, v in pairs (availableCells) do
- log(v[1] .. ":" .. v[2])
- end
- chosenCell = availableCells[math.random(1, #availableCells)]
- log(chosenCell[1] .. "," .. chosenCell[2] .. "\n")
- board[chosenCell[2] + (y-1)*3][chosenCell[1] + (x-1)*3] = i
- refTab[1][x][chosenCell[1]] = 0
- refTab[2][y][chosenCell[2]] = 0
- logBoard(board)
- end
- end
- end
- end
- function getAvailableCells(quadrantX, quadrantY, refTab, board)
- local result = {}
- for y, _ in pairs (refTab[2][quadrantY]) do
- for x, _ in pairs (refTab[1][quadrantX]) do
- if refTab[2][quadrantY][y] ~= 0 and refTab[1][quadrantX][x] ~= 0 and not board[y + (quadrantY-1)*3][x + (quadrantX-1)*3] then
- table.insert(result, {x, y})
- end
- end
- end
- return result
- end
- function iLocate(i, quadrantX, quadrantY, board)
- for y = 1, 3 do
- for x = 1, 3 do
- if board[y + (quadrantY-1)*3][x + (quadrantX-1)*3] == i then
- return {x, y}, {x + (quadrantX-1)*3, y + (quadrantY-1)*3}
- end
- end
- end
- printBoard(board)
- error("iLocate Failed")
- 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)
- local ok = pcall(generatePuzzle(board))
- local c = 1
- while not ok do
- initializeBoard(board)
- --printBoard(board)
- ok = pcall(generatePuzzle(board))
- sleep(.1)
- if c % 100 == 0 then
- print(c)
- sleep(.1)
- end
- c = c + 1
- end
- printBoard(board)
- elseif tArgs[1] == "solve" then
- end
Add Comment
Please, Sign In to add comment