Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- board = {}
- shell.run("delete console")
- local h = fs.open("console","a")
- function createEmptyBoard()
- for i = 1,9 do
- board[i] = {}
- for j = 1,9 do
- board[i][j] = {}
- board[i][j].num = 0
- board[i][j].exceptions = {}
- board[i][j].options = {}
- end
- end
- end
- function printBoard(xoff,yoff)
- xoff = xoff or 0
- yoff = yoff or 0
- for i=1,9 do
- for j=1,9 do
- term.setCursorPos(i + xoff,j + yoff)
- term.write(board[i][j].num)
- end
- end
- end
- --==Sudoku Functions==--
- function findEmpty()
- for y = 1,9 do
- for x = 1,9 do
- if board[x][y].num == 0 then
- return true,x,y
- end
- end
- end
- end
- local function findQuadrant(x,y)
- quadrant = {}
- quadrant.num = 0
- quadrant.x = 0
- quadrant.y = 0
- if y <= 3 then
- if x<=3 then
- quadrant.num = 1
- quadrant.x = 1
- quadrant.y = 1
- elseif x<=6 then
- quadrant.num = 2
- quadrant.x = 4
- quadrant.y = 1
- elseif x<=9 then
- quadrant.num = 3
- quadrant.x = 7
- quadrant.y = 1
- end
- elseif y<=6 then
- if x<=3 then
- quadrant.num = 4
- quadrant.x = 1
- quadrant.y = 1
- elseif x<=6 then
- quadrant.num = 5
- quadrant.x = 4
- quadrant.y = 1
- elseif x<=9 then
- quadrant.num = 6
- quadrant.x = 7
- quadrant.y = 1
- end
- elseif y<=9 then
- if x<=3 then
- quadrant.num = 7
- quadrant.x = 1
- quadrant.y = 1
- elseif x<=6 then
- quadrant.num = 8
- quadrant.x = 4
- quadrant.y = 1
- elseif x<=9 then
- quadrant.num = 9
- quadrant.x = 7
- quadrant.y = 1
- end
- end
- return quadrant
- end
- local function organizeOptions(x,y)
- for i=1,9 do
- board[x][y].options[i] = i
- end
- for i = 1,9 do
- for _,k in pairs(board[x][y].exceptions) do
- if i == k then
- board[x][y].options[i] = 0
- end
- end
- end
- end
- function findOptions(x,y)
- for i = 1,9 do
- if board[i][y].num ~= 0 then
- table.insert(board[x][y].exceptions,board[i][y].num)
- end
- if board[x][i].num ~= 0 then
- table.insert(board[x][y].exceptions,board[x][i].num)
- end
- end
- quadrant = findQuadrant(x,y)
- for i = quadrant.x,quadrant.x+2 do
- for j = quadrant.y,quadrant.y+2 do
- if board[i][j].num ~= 0 then
- table.insert(board[x][y].exceptions,board[i][j].num)
- end
- end
- end
- organizeOptions(x,y)
- end
- function recursive()
- sleep(.000000001)
- log("recall")
- unsolved,x,y = findEmpty()
- if unsolved then
- findOptions(x,y)
- for _,v in pairs(board[x][y].options) do
- if v~= 0 then
- log("x: "..x)
- log("y: "..y)
- log("Decision: "..v)
- board[x][y].num = v
- recursive()
- end
- end
- board[x][y].num = 0
- log("backtrack")
- else
- term.clear()
- printBoard()
- print("FINISHED :D")
- error()
- end
- end
- --==Debug Functions==--
- function fillRand()
- for i=1,4 do
- for j=1,4 do
- board[i][j].num = math.random(1,9)
- end
- end
- end
- function tab(t)
- for i,v in pairs(t) do
- if type(v) == "table" then
- tab(v)
- else
- print("i: ",i," v: ",v)
- end
- end
- end
- function log(s)
- if type(s) == "string" then
- h.writeLine(s)
- --print(s)
- else
- for i,v in pairs(s) do
- h.write("i: ")
- h.write(i)
- h.write(" v: ")
- h.writeLine(v)
- --print("i: ",i," v: ",v)
- end
- end
- h.flush()
- end
- createEmptyBoard()
- term.clear()
- board[1][1].num = 5
- board[2][1].num = 3
- board[5][1].num = 7
- board[1][2].num = 6
- board[4][2].num = 1
- board[5][2].num = 9
- board[6][2].num = 5
- board[2][3].num = 9
- board[3][3].num = 8
- board[8][3].num = 6
- board[1][4].num = 8
- board[5][4].num = 6
- board[9][4].num = 3
- board[1][5].num = 4
- board[4][5].num = 8
- board[6][5].num = 3
- board[9][5].num = 1
- board[1][6].num = 7
- board[5][6].num = 2
- board[9][6].num = 6
- board[2][7].num = 6
- board[7][7].num = 2
- board[8][7].num = 8
- board[4][8].num = 4
- board[5][8].num = 1
- board[6][8].num = 9
- board[9][8].num = 5
- board[5][9].num = 8
- board[8][9].num = 7
- board[9][9].num = 9
- printBoard(30,0)
- --fillRand()
- recursive()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement