This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Kryptanyte's profile picture

[Sudoku Game]Checking vert tables for double data

Started by Kryptanyte, 14 March 2013 - 07:17 PM
Kryptanyte #1
Posted 14 March 2013 - 08:17 PM
Hey guys.

Just been working on this off and on, this has got me stumped. I keep seemingly getting duplicate numbers on the vertical lines.

Spoiler

function clear()
    term.clear()
    term.setCursorPos(1,1)
end



function checkNum(checkNum, usedTable)

    for i = 1, #usedTable do
    
        if checkNum == usedTable[i] then
        
            return "used"
            
        end
        
    end
    
    return "valid"
    
end

function checkNumV(checkNum, colTable, line)

    if checkNum == colTable[line] then
        
        return "used"    
        
    end
    
    return "valid"
    
end


function genNumber()

    number = math.floor(math.random(1,9))
    return number

end

function genBox(line1, line2, line3)
    
    usedTable = {}
    usedTable[0] = {}
    usedTable[1] = {}
    usedTable[2] = {}
    usedTable[3] = {}

    for i = 1, 3 do
    
        repeat
        col1 = genNumber()
        validity = checkNum(col1, usedTable[0])
        until validity == "valid"

        if validity == "valid" then
        table.insert(usedTable[i], col1)
        table.insert(usedTable[0], col1)
        end

        repeat
        col2 = genNumber()
        validity = checkNum(col2, usedTable[0])
        until validity == "valid"
        
        if validity == "valid" then
        table.insert(usedTable[i], col2)
        table.insert(usedTable[0], col2)
        end
        
        repeat
        col3 = genNumber()
        validity = checkNum(col3, usedTable[0])
        until validity == "valid"        
        
        if validity == "valid" then
        table.insert(usedTable[i], col3)
        table.insert(usedTable[0], col3)
        end
        
    end

    return usedTable[1], usedTable[2], usedTable[3]
    
end

function genLine()


    usedLine = {}
    usedLine[1] = {}
    usedLine[2] = {}
    usedLine[3] = {}
    usedLine[4] = {}
    usedLine[5] = {}
    usedLine[6] = {}
    usedLine[7] = {}
    usedLine[8] = {}
    usedLine[9] = {}
    usedCol = {}
    usedCol[1] = {}
    usedCol[2] = {}
    usedCol[3] = {}
    usedCol[4] = {}
    usedCol[5] = {}
    usedCol[6] = {}
    usedCol[7] = {}
    usedCol[8] = {}
    usedCol[9] = {}
    
    for i = 1, 9 do
    
        usedTable = {}
    
        repeat
        line = genNumber()
        validity = checkNum(line, usedTable)
        until validity == "valid"
        
        
        if validity == "valid" then
        table.insert(usedLine[i], line)
        table.insert(usedTable, line)
        table.insert(usedCol[i], line)
        end
        
        for v = 2, 9 do
            
            repeat
            line = genNumber()
            validity = checkNumV(line, usedCol[v], i)
            until validity == "valid"
            
            if validity == "valid" then
            table.insert(usedLine[i], line)
            table.insert(usedCol[v], line)
            table.insert(usedTable, line)
            end
        
        end
        
    end
    
    return usedLine, usedCol
    
end

table1, table2 = genLine()

clear()


for i = 1, 9 do

    printthis = table.concat(table1[i], ",")
    print(printthis)

end



I know it checks the y col, but it seems to return true, I might be getting tired and missing something, idk?

Also, for those of you prefer it Pastebin: http://pastebin.com/S7DzjG5H
Kryptanyte #2
Posted 14 March 2013 - 11:59 PM
Help anyone?
theoriginalbit #3
Posted 15 March 2013 - 12:24 AM
I'll take a look in a minute. when I get back to my computer.