Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- FilledField = {
- 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= "", B6= "", G7= "", G8= "", G9= "";
- H1= "", H2= "", H3= "", H4= "", H5= "", H6= "", H7= "", H8= "", H9= "";
- I1= "", I2= "", I3= "", I4= "", I5= "", I6= "", I7= "", I8= "", I9= ""
- }
- 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"}
- Rows = {"A","B","C","D","E","F","G","H","I"}
- c = 1 -- use this to count the square we are up to
- function inTable(tbl, item)
- for key, value in pairs(tbl) do
- if value == item then return true end
- end
- return false
- end
- function math.randomEx(slot)
- local bExcluded
- local Block = getblock(slot)
- local restricted = {}
- --add uncompatible spots to
- for i = 1,9 do
- table.insert(restricted,FilledField[Block[i]])
- table.insert(restricted,FilledField[string.sub(slot, 1, -2)..i])
- table.insert(restricted,FilledField[Rows[i]..string.sub(slot,2)])
- end
- 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
- return " "
- end
- local iNum
- repeat
- bExcluded = false;
- iNum = math.random(1,9)
- for _, iVal in ipairs( restricted ) do
- if iNum == tonumber(iVal) then
- bExcluded = true
- break
- end
- end
- until not bExcluded
- --print(iNum)
- return iNum
- end
- function getblock(slot)
- Block1 = {"A1","A2","A3","B1","B2","B3","C1","C2","C3"}
- Block2 = {"A4","A5","A6","B4","B5","B6","C4","C5","C6"}
- Block3 = {"A7","A8","A9","B7","B8","B9","C7","C8","C9"}
- Block4 = {"D1","D2","D3","E1","E2","E3","F1","F2","F3"}
- Block5 = {"D4","D5","D6","E4","E5","E6","F4","F5","F6"}
- Block6 = {"D7","D8","D9","E7","E8","E9","F7","F8","F9"}
- Block7 = {"G1","G2","G3","H1","H2","H3","I1","I2","I3"}
- Block8 = {"G4","G5","G6","H4","H5","H6","I4","I5","I6"}
- Block9 = {"G7","G8","G9","H7","H8","H9","I7","I8","I9"}
- for i=1,9 do if slot == Block1[i] then Block = Block1;break end end
- for i=1,9 do if slot == Block2[i] then Block = Block2;break end end
- for i=1,9 do if slot == Block3[i] then Block = Block3;break end end
- for i=1,9 do if slot == Block4[i] then Block = Block4;break end end
- for i=1,9 do if slot == Block5[i] then Block = Block5;break end end
- for i=1,9 do if slot == Block6[i] then Block = Block6;break end end
- for i=1,9 do if slot == Block7[i] then Block = Block7;break end end
- for i=1,9 do if slot == Block8[i] then Block = Block8;break end end
- for i=1,9 do if slot == Block9[i] then Block = Block9;break end end
- return Block
- end
- function CheckConflicts(number,slot)
- number = tostring(number)
- local Block = getblock(slot)
- slot = slot
- for i = 1,9 do if FilledField[Block[i]] == number then return true end end -- Check Block
- for i = 1,9 do if FilledField[string.sub(slot, 1, -2)..i] == number then return true end end -- Check Row
- for i = 1,9 do if FilledField[Rows[i]..string.sub(slot, 2)] == number then return true end end --Check Coloumn
- return false -- if nothing is found to conflict, produces a false result
- end
- function generate()
- repeat
- CurSlot = slots[c]
- FilledField[CurSlot] = tostring(math.randomEx(CurSlot))
- c=c+1
- until c == 82
- end
- function testprint()
- print(FilledField["A1"],FilledField["A2"],FilledField["A3"]," ",FilledField["A4"],FilledField["A5"],FilledField["A6"]," ",FilledField["A7"],FilledField["A8"],FilledField["A9"])
- print(FilledField["B1"],FilledField["B2"],FilledField["B3"]," ",FilledField["B4"],FilledField["B5"],FilledField["B6"]," ",FilledField["B7"],FilledField["B8"],FilledField["B9"])
- print(FilledField["C1"],FilledField["C2"],FilledField["C3"]," ",FilledField["C4"],FilledField["C5"],FilledField["C6"]," ",FilledField["C7"],FilledField["C8"],FilledField["C9"])
- print(" ")
- print(FilledField["D1"],FilledField["D2"],FilledField["D3"]," ",FilledField["D4"],FilledField["D5"],FilledField["D6"]," ",FilledField["D7"],FilledField["D8"],FilledField["D9"])
- print(FilledField["E1"],FilledField["E2"],FilledField["E3"]," ",FilledField["E4"],FilledField["E5"],FilledField["E6"]," ",FilledField["E7"],FilledField["E8"],FilledField["E9"])
- print(FilledField["F1"],FilledField["F2"],FilledField["F3"]," ",FilledField["F4"],FilledField["F5"],FilledField["F6"]," ",FilledField["F7"],FilledField["F8"],FilledField["F9"])
- print(" ")
- print(FilledField["G1"],FilledField["G2"],FilledField["G3"]," ",FilledField["G4"],FilledField["G5"],FilledField["G6"]," ",FilledField["G7"],FilledField["G8"],FilledField["G9"])
- print(FilledField["H1"],FilledField["H2"],FilledField["H3"]," ",FilledField["H4"],FilledField["H5"],FilledField["H6"]," ",FilledField["H7"],FilledField["H8"],FilledField["H9"])
- print(FilledField["I1"],FilledField["I2"],FilledField["I3"]," ",FilledField["I4"],FilledField["I5"],FilledField["I6"]," ",FilledField["I7"],FilledField["I8"],FilledField["I9"])
- end
- term.clear()
- term.setCursorPos(1,1)
- generate()
- --print(CheckConflicts(2,"E3"))
- testprint()
Advertisement
Add Comment
Please, Sign In to add comment