Advertisement
BigSHinyToys

failed lua sudoku generator

Mar 10th, 2013
256
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.75 KB | None | 0 0
  1. --[[
  2. Attempt at sudoku
  3. by BigShinyToys
  4. ]]--
  5. local testMode = false
  6.  
  7. local function dPrint(...)
  8. print(...)
  9. sleep(0.5)
  10. end
  11.  
  12. if not testMode then
  13. dPrint = function()
  14. end
  15. end
  16.  
  17. local function generate()
  18. local valed = true
  19. local loop = true
  20. local line = {}
  21. local column = {}
  22. local game = {}
  23. local box = {
  24. {{},{},{},},
  25. {{},{},{},},
  26. {{},{},{},},
  27. }
  28. for x = 1,3 do
  29. for y = 1,3 do
  30. for pos = 1,9 do
  31. box[x][y][pos] = true
  32. end
  33. end
  34. end
  35. for x = 1,9 do
  36. line[x] = {}
  37. column[x] = {}
  38. for y = 1,9 do
  39. line[x][y] = true
  40. column[x][y] = true
  41. end
  42. end
  43.  
  44. for x = 1,9 do
  45. game[x] = {}
  46. for y = 1,9 do
  47. local possible = {}
  48. for slot = 1,9 do
  49. dPrint(tostring(x).." "..tostring(math.ceil(x/3)).." "..tostring(y).." "..tostring(math.ceil(y/3)))
  50. if line[x][slot] and column[y][slot] and box[math.ceil(x/3)][math.ceil(y/3)][slot] then
  51. table.insert(possible,slot)
  52. end
  53. end
  54. --print(#possible)
  55. if #possible ~= 0 then
  56. local slot = possible[math.random(1,#possible)]
  57. game[x][y] = slot
  58. line[x][slot] = false
  59. column[y][slot] = false
  60. box[math.ceil(x/3)][math.ceil(y/3)][slot] = false
  61. else
  62. valed = false
  63. end
  64. end
  65. end
  66.  
  67. return game,valed
  68. end
  69.  
  70. local function drawGame(game,offX,offY)
  71. local setX = offX or 0
  72. local setY = offY or 0
  73. for x = 1,9 do
  74. for y = 1,9 do
  75. term.setCursorPos((x*3) + setX,(y*2) + setY)
  76. term.write(tostring(game[x][y]))
  77. sleep(0.1)
  78. end
  79. end
  80. end
  81.  
  82. while true do
  83. term.clear()
  84. local loop = 1
  85. local myGame,valed = generate()
  86. while not valed do
  87. loop = loop + 1
  88. myGame,valed = generate()
  89. end
  90. print(loop)
  91. drawGame(myGame)
  92. local _,n = os.pullEvent("key")
  93. if n == 200 then
  94. break
  95. end
  96. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement