Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- Attempt at sudoku
- by BigShinyToys
- ]]--
- local testMode = false
- local function dPrint(...)
- print(...)
- sleep(0.5)
- end
- if not testMode then
- dPrint = function()
- end
- end
- local function generate()
- local valed = true
- local loop = true
- local line = {}
- local column = {}
- local game = {}
- local box = {
- {{},{},{},},
- {{},{},{},},
- {{},{},{},},
- }
- for x = 1,3 do
- for y = 1,3 do
- for pos = 1,9 do
- box[x][y][pos] = true
- end
- end
- end
- for x = 1,9 do
- line[x] = {}
- column[x] = {}
- for y = 1,9 do
- line[x][y] = true
- column[x][y] = true
- end
- end
- for x = 1,9 do
- game[x] = {}
- for y = 1,9 do
- local possible = {}
- for slot = 1,9 do
- dPrint(tostring(x).." "..tostring(math.ceil(x/3)).." "..tostring(y).." "..tostring(math.ceil(y/3)))
- if line[x][slot] and column[y][slot] and box[math.ceil(x/3)][math.ceil(y/3)][slot] then
- table.insert(possible,slot)
- end
- end
- --print(#possible)
- if #possible ~= 0 then
- local slot = possible[math.random(1,#possible)]
- game[x][y] = slot
- line[x][slot] = false
- column[y][slot] = false
- box[math.ceil(x/3)][math.ceil(y/3)][slot] = false
- else
- valed = false
- end
- end
- end
- return game,valed
- end
- local function drawGame(game,offX,offY)
- local setX = offX or 0
- local setY = offY or 0
- for x = 1,9 do
- for y = 1,9 do
- term.setCursorPos((x*3) + setX,(y*2) + setY)
- term.write(tostring(game[x][y]))
- sleep(0.1)
- end
- end
- end
- while true do
- term.clear()
- local loop = 1
- local myGame,valed = generate()
- while not valed do
- loop = loop + 1
- myGame,valed = generate()
- end
- print(loop)
- drawGame(myGame)
- local _,n = os.pullEvent("key")
- if n == 200 then
- break
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement