Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

How to check if each number in two tables match?

edited March 2016 in Questions Posts: 11

This is what I currently have now, and yes, it doesn't work like I wanted it to.
What I am trying to code is a game that works like an android unlock pattern, where you can draw in a type of a pattern, and do other functions if the pattern(that one puts in) matches which what exists in the code.

sn={"Spell number one", 50, 4, 5, 6, 9}. This is basically theSpellThatIsCasted, enemyAttackPoint, #1of code, #2of code....hope you get the idea.

What currently happens now, though, is the pattern(that one puts in) is considered as a match for s1 or s2 or s3, even when just the first number(so the first position on the pattern) matched with what you drew in.

This is a small portion of my code:

 for i=1,touchCount do
             if castASpell[i]== s1[i+2] then
                    theSpellThatIsCasted= s1[1]
                    enemyAttackPoint= s1[2]
            elseif castASpell[i]== s2[i+2] then
                    theSpellThatIsCasted= s2[1]
                    enemyAttackPoint= s2[2]
            elseif castASpell[i]== s3[i+2] then
                    theSpellThatIsCasted= s3[1]
                    enemyAttackPoint= s3[2]
            else
                theSpellThatIsCasted=0
                enemyAttackPoint=0
            end
        end

I would be so happy if anyone helped me out... To fix the code, make it better, make in shorter, or other seggestions or ideas... Thank you very much!

Tagged:

Comments

  • IgnatzIgnatz Mod
    edited March 2016 Posts: 5,396

    @hohohohoho

    Try this

    --put all the spells in one table
    --put each touch sequence in a comma separated string << NOTE
    Spells={
         {"Spell number one", 50, "4, 5, 6, 9"},
         {"Spell number two", 50, "3, 6, 4, 1"},
         {"Spell number three", 50, "4, 2, 8, 7"},
        }
    
    --then to do the test...
    --turn your table of touches into a comma separated string
    --so {1,2,3,4} becomes "1,2,3,4"
    local c = table.concat(castASpell,",")
    --test each spell
    theSpellThatIsCasted,enemyAttackPoint=0,0 --initialise
    for s in pairs(Spells) do
        if c == s[3] then
            theSpellThatIsCasted = s[1]
            enemyAttackPoint = s[2]
            break
        end
    end
    
    
    
    
    
  • IgnatzIgnatz Mod
    edited March 2016 Posts: 5,396

    @hohohohoho - here's a faster approach (it won't make a difference for a small table of spells, but as your programs get bigger, you should know how to do this)

    Spells={
         {"Spell number one", 50, "4, 5, 6, 9"},
         {"Spell number two", 50, "3, 6, 4, 1"},
         {"Spell number three", 50, "4, 2, 8, 7"},
        }
    --this is new
    --make an extra table where the lookup key is the touch sequence
    --so touchSeq["4, 2, 8, 7"]=3
    touchSeq={}
    for i=1, #Spells do
        touchSeq[Spells[i][3]] = i
    end
    
    --then to do the test...
    --turn your table of touches into a comma separated string
    --so {1,2,3,4} becomes "1,2,3,4"
    local c = table.concat(castASpell,",")
    --look it up in our new table, if it isn't found, it returns nil
    pattern = touchSeq[c]  --just one lookup is needed!
    
    --and when we want to use it later
    if pattern then --if a pattern has been chosen
        enemyAttackPoint=Spells[pattern][2]
    end
    --this is good is if your Spells table has lots of items
    --your code above would require you to copy all of them into variables
    --whereas this approach just stores one number (pattern) for the chosen 
    --spell, and when you want to use one of the items, you just look it up
    
  • Thank you so much! now I can complete my game hopefully!

Sign In or Register to comment.