Memory Game

I want to make a memory game with different difficulties. It will be a grid with certain squares selected. it will show the user the selected squares, then the user has to select those squares again. Here is what I have so far.

-- Memory

-- Use this function to perform your initial setup
function setup()
    parameter.integer("Difficulty", 3, 10, 4)
    rectMode(CENTER)
    displayMode(FULLSCREEN)
    sqr=vec2(0,0)   -- x,y value of touched square
    displaying = false
    lock = false
    input = {}
    squareColor = color(0, 255, 0, 255)
end


function reset()
    if Difficulty ~= nil then 
        input = {}
        answer = randomTable(Difficulty)
        lock = false
        squareColor = color(0, 255, 0, 255)
        displaying = false
    end
end
function randomTable(Diff)
    print("Generating answer")
    newTable = {}
    totalTiles = 0
    cap = Diff^2
    for x=1, cap do
        newTable[x] = math.random(0, 1)
        totalTiles = totalTiles + newTable[x]
        if debugMode == true then
            print(newTable[x])
        end
    end
end

function isIn(value, Table)
    if Table == nil then return false end
    for t=1, #Table do
         if Table[t] == value then
            return true
        end
    end
    return false
end

function draw()
    background(40, 40, 50) 
    size=-10*(Difficulty^.5)+100     -- size of squares
    for y=1,Difficulty do    -- loop from 1 to ySize
        for x=1,Difficulty do    -- loop from 1 to xSize
            fill(255)   -- set color to white
                tableNumber = ((sqr.x-1)*Difficulty)+sqr.y
            if isIn(tableNumber, input) then
                fill(squareColor)   -- make it green
            end
            rect(x*size,y*size,size,size)   -- draw white or green square
        end
    end
end

function touched(t)
    if displaying == false and lock == false then
        if t.state==0 then
            for y=1,Difficulty do
                for x=1,Difficulty do
                    if t.x>x*size-size/2 and t.x<x*size+size/2 and t.y>y*size-size/2 and t.y<y*size+size/2 then
                        sqr=vec2(x,y)
                        tableValue = ((sqr.x-1)*Difficulty)+sqr.y
                        table.insert(input, tableValue)
                        print(vec2(x,y))
                        print(tableValue)
                        if input[-1] ~= answer[tableValue] and input[-1] ~= nil then
                            squareColor = color(255, 0, 0, 255)
                            lock = true
                            print(input[-1])
                            print(answer[tableValue])
                        end
                    end
                end
            end
        end
    end
end 


I haven’t implemented a way to show the user which squares are selected, and everything is buggy. Help!

See this link for a memory game I wrote back in January. Maybe there’s something in there you could use also.

http://codea.io/talk/discussion/4573/how-good-is-your-memory#Item_13

@HydoVitalityProtago

Table = {1, 2, 3}
print(table[-1])

This prints 3, the last value in the table.
And @dave1707 , Thanks!

Hello !

What’s the signification of input[-1] for you ?

It is customary in Lua to start arrays with index 1 (the Lua libraries adhere to this convention; so, if your arrays also start with 1, you will be able to use their functions directly).

@CNCode In some cases I find it easier to write the code from scratch then to try and fix code. Here is some code I think does some of what you want. You can look thru this and see what I did and then modify yours. I didn’t put a whole lot of time into this, so it’s not that good.


displayMode(FULLSCREEN)

function setup()
    nbr=4
    rectMode(CENTER)
    size=50     -- size of squares
    setup1()
end

function setup1()
    done=false
    matched=false
    str=""
    z=0
    tab={}
    -- random starting squares
    while z~=nbr do
        x=math.random(nbr)
        y=math.random(nbr)
        found=false
        for a=1,#tab do
            if tab[a].x==x and tab[a].y==y then
                found=true
            end
        end
        if not found then
            table.insert(tab,vec2(x,y))
            z=z+1
        end
        if #tab==0 then
            table.insert(tab,vec2(x,y))
        end
    end
    start=true
    cnt=0
    tab1={}
end

function draw()
    background(40, 40, 50)
    if start then   -- show starting squares
        if cnt<3 then
            cnt=cnt+DeltaTime
        else
            start=false
        end
    end  
    -- draw all squares. touched are green 
    for y=1,nbr do    -- loop from 1 to ySize
        for x=1,nbr do    -- loop from 1 to xSize
            fill(255)   -- set color to white
            for z=1,#tab1 do
                if tab1[z].x==x and tab1[z].y==y then   -- square that was touched
                    fill(0,255,0)   -- make it green
                end
            end
            if start then
                for z=1,#tab do
                    if tab[z].x==x and tab[z].y==y then
                        fill(0,255,0)
                    end
                end
            end
            rect(x*size,y*size,size,size)   -- draw white or green square
        end
    end
    
    if matched then
        text("matched",WIDTH/2,HEIGHT-100)
        text("tap screen for next square",WIDTH/2,HEIGHT-200)
        done=true
    end
    if str~="" then
        text(str,WIDTH/2,HEIGHT-50)
        done=true
    end
end

function touched(t)
    if t.state==BEGAN and done then
        if matched then
            nbr=nbr+1
        end
        setup1()
        return
    end
    if t.state==BEGAN and not start then -- screen touched
        for y=1,nbr do    -- loop y from 1 to ySize
            for x=1,nbr do    -- loop x from 1 to xSize
                -- determine which square is touched
                if t.x>x*size-size/2 and t.x<x*size+size/2 and
                        t.y>y*size-size/2 and t.y<y*size+size/2 then
                    table.insert(tab1,vec2(x,y))
                end
            end
        end
    end
    if t.state==ENDED then
        str=""
        if #tab==#tab1 then
            matched=true
            for z=1,#tab do
                found=false
                for y=1,#tab1 do
                    if tab[z].x==tab1[y].x and tab[z].y==tab1[y].y then
                        found=true
                    end
                    
                end
                if not found then
                    matched=false
                end
            end
        end
        if #tab1>#tab then
            str="too many squares touched"
        end
    end
end