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!