HyperQ

Sudoku

Feb 9th, 2013
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.82 KB | None | 0 0
  1. FilledField = {
  2.     A1= "", A2= "", A3= "", A4= "", A5= "", A6= "", A7= "", A8= "", A9= "";
  3.     B1= "", B2= "", B3= "", B4= "", B5= "", B6= "", B7= "", B8= "", B9= "";
  4.     C1= "", C2= "" ,C3= "", C4= "", C5= "", C6= "", C7= "", C8= "", C9= "";
  5.     D1= "", D2= "", D3= "", D4= "", D5= "", D6= "", D7= "", D8= "", D9= "";
  6.     E1= "", E2= "", E3= "", E4= "", E5= "", E6= "", E7= "", E8= "", E9= "";
  7.     F1= "", F2= "", F3= "", F4= "", F5= "", F6= "", F7= "", F8= "", F9= "";
  8.     G1= "", G2= "", G3= "", G4= "", G5= "", B6= "", G7= "", G8= "", G9= "";
  9.     H1= "", H2= "", H3= "", H4= "", H5= "", H6= "", H7= "", H8= "", H9= "";
  10.     I1= "", I2= "", I3= "", I4= "", I5= "", I6= "", I7= "", I8= "", I9= ""
  11. }
  12.  
  13. slots = {"A1","A2","A3","A4","A5","A6","A7","A8","A9","B1","B2","B3","B4","B5","B6","B7","B8","B9","C1","C2","C3","C4","C5","C6","C7","C8","C9","D1","D2","D3","D4","D5","D6","D7","D8","D9","E1","E2","E3","E4","E5","E6","E7","E8","E9","F1","F2","F3","F4","F5","F6","F7","F8","F9","G1","G2","G3","G4","G5","G6","G7","G8","G9","H1","H2","H3","H4","H5","H6","H7","H8","H9","I1","I2","I3","I4","I5","I6","I7","I8","I9"}
  14. Rows = {"A","B","C","D","E","F","G","H","I"}
  15. c = 1 -- use this to count the square we are up to
  16. function inTable(tbl, item)
  17.     for key, value in pairs(tbl) do
  18.         if value == item then return true end
  19.     end
  20.     return false
  21. end
  22. function math.randomEx(slot)
  23.     local bExcluded
  24.  
  25.     local Block = getblock(slot)
  26.     local restricted = {}
  27.     --add uncompatible spots to
  28.     for i = 1,9 do
  29.         table.insert(restricted,FilledField[Block[i]])
  30.         table.insert(restricted,FilledField[string.sub(slot, 1, -2)..i])
  31.         table.insert(restricted,FilledField[Rows[i]..string.sub(slot,2)])
  32.     end
  33.     if inTable(restricted,"1") and inTable(restricted,"2") and inTable(restricted,"3") and inTable(restricted,"4") and inTable(restricted,"5")and inTable(restricted,"6") and inTable(restricted,"7") and inTable(restricted,"8") and inTable(restricted,"9") then
  34.         return " "
  35.     end
  36.     local iNum
  37.     repeat
  38.         bExcluded = false;
  39.         iNum = math.random(1,9)
  40.         for _, iVal in ipairs( restricted ) do
  41.             if iNum == tonumber(iVal) then
  42.                 bExcluded = true
  43.                 break
  44.             end
  45.         end
  46.     until not bExcluded
  47.     --print(iNum)
  48.     return iNum
  49. end
  50.  
  51. function getblock(slot)
  52.     Block1 = {"A1","A2","A3","B1","B2","B3","C1","C2","C3"}
  53.     Block2 = {"A4","A5","A6","B4","B5","B6","C4","C5","C6"}
  54.     Block3 = {"A7","A8","A9","B7","B8","B9","C7","C8","C9"}
  55.     Block4 = {"D1","D2","D3","E1","E2","E3","F1","F2","F3"}
  56.     Block5 = {"D4","D5","D6","E4","E5","E6","F4","F5","F6"}
  57.     Block6 = {"D7","D8","D9","E7","E8","E9","F7","F8","F9"}
  58.     Block7 = {"G1","G2","G3","H1","H2","H3","I1","I2","I3"}
  59.     Block8 = {"G4","G5","G6","H4","H5","H6","I4","I5","I6"}
  60.     Block9 = {"G7","G8","G9","H7","H8","H9","I7","I8","I9"}
  61.     for i=1,9 do if slot == Block1[i] then Block = Block1;break end end
  62.     for i=1,9 do if slot == Block2[i] then Block = Block2;break end end
  63.     for i=1,9 do if slot == Block3[i] then Block = Block3;break end end
  64.     for i=1,9 do if slot == Block4[i] then Block = Block4;break end end
  65.     for i=1,9 do if slot == Block5[i] then Block = Block5;break end end
  66.     for i=1,9 do if slot == Block6[i] then Block = Block6;break end end
  67.     for i=1,9 do if slot == Block7[i] then Block = Block7;break end end
  68.     for i=1,9 do if slot == Block8[i] then Block = Block8;break end end
  69.     for i=1,9 do if slot == Block9[i] then Block = Block9;break end end
  70.     return Block
  71. end
  72.  
  73. function CheckConflicts(number,slot)
  74.     number = tostring(number)
  75.     local Block = getblock(slot)
  76.     slot = slot
  77.     for i = 1,9 do if FilledField[Block[i]] == number then return true end end -- Check Block
  78.     for i = 1,9 do if FilledField[string.sub(slot, 1, -2)..i] == number then return true end end -- Check Row
  79.     for i = 1,9 do if FilledField[Rows[i]..string.sub(slot, 2)] == number then return true end end --Check Coloumn
  80.    
  81.     return false -- if nothing is found to conflict, produces a false result
  82. end
  83.  
  84. function generate()
  85.  
  86.     repeat
  87.         CurSlot = slots[c]
  88.         FilledField[CurSlot] = tostring(math.randomEx(CurSlot))
  89.         c=c+1
  90.     until c == 82
  91.    
  92. end
  93.  
  94. function testprint()
  95.     print(FilledField["A1"],FilledField["A2"],FilledField["A3"],"  ",FilledField["A4"],FilledField["A5"],FilledField["A6"],"  ",FilledField["A7"],FilledField["A8"],FilledField["A9"])
  96.     print(FilledField["B1"],FilledField["B2"],FilledField["B3"],"  ",FilledField["B4"],FilledField["B5"],FilledField["B6"],"  ",FilledField["B7"],FilledField["B8"],FilledField["B9"])
  97.     print(FilledField["C1"],FilledField["C2"],FilledField["C3"],"  ",FilledField["C4"],FilledField["C5"],FilledField["C6"],"  ",FilledField["C7"],FilledField["C8"],FilledField["C9"])
  98.     print(" ")
  99.     print(FilledField["D1"],FilledField["D2"],FilledField["D3"],"  ",FilledField["D4"],FilledField["D5"],FilledField["D6"],"  ",FilledField["D7"],FilledField["D8"],FilledField["D9"])
  100.     print(FilledField["E1"],FilledField["E2"],FilledField["E3"],"  ",FilledField["E4"],FilledField["E5"],FilledField["E6"],"  ",FilledField["E7"],FilledField["E8"],FilledField["E9"])
  101.     print(FilledField["F1"],FilledField["F2"],FilledField["F3"],"  ",FilledField["F4"],FilledField["F5"],FilledField["F6"],"  ",FilledField["F7"],FilledField["F8"],FilledField["F9"])
  102.     print(" ")
  103.     print(FilledField["G1"],FilledField["G2"],FilledField["G3"],"  ",FilledField["G4"],FilledField["G5"],FilledField["G6"],"  ",FilledField["G7"],FilledField["G8"],FilledField["G9"])
  104.     print(FilledField["H1"],FilledField["H2"],FilledField["H3"],"  ",FilledField["H4"],FilledField["H5"],FilledField["H6"],"  ",FilledField["H7"],FilledField["H8"],FilledField["H9"])
  105.     print(FilledField["I1"],FilledField["I2"],FilledField["I3"],"  ",FilledField["I4"],FilledField["I5"],FilledField["I6"],"  ",FilledField["I7"],FilledField["I8"],FilledField["I9"])
  106.  
  107. end
  108. term.clear()
  109. term.setCursorPos(1,1)
  110. generate()
  111. --print(CheckConflicts(2,"E3"))
  112. testprint()
Advertisement
Add Comment
Please, Sign In to add comment