Tic Tac Toe Game help

I;ve been trying to learn Lua and decided to start with something i thought was simple, a tic tac toe game. Unfortunately its proving harder than i thought. i am counting the number of turns so when it gets to 9(all squares filled) you lose the game and im doing that by addind 1 to a counter every time i change the image on a square. everything ready , time to test and after the first click it tells me game over. i looked around a bit and figured out its adding a lot more than 1 every time the image changes even tho i used clicked = clicked + 1; any help would be greatly appreciated as i try to learn this language.

[code]_H = display.contentHeight
_W = display.contentWidth
clicked = 0

–[middle row]–
local button22= display.newImage( “blank.png” )
button22.x = _W / 2
button22.y = _H /2
button22.state = “blank”
local button21 = display.newImage( “blank.png” )
button21.x = button22.x -100
button21.y = button22.y
button21.state = “blank”
local button23 = display.newImage( “blank.png” )
button23.x = button22.x +100
button23.y = button22.y
button23.state = “blank”

–[top row]–
local button11 = display.newImage( “blank.png” )
button11.x = button21.x
button11.y = button21.y - 100
button11.state = “blank”
local button12 = display.newImage( “blank.png” )
button12.x = button22.x
button12.y = button22.y -100
button12.state = “blank”
local button13 = display.newImage( “blank.png” )
button13.x = button23.x
button13.y = button23.y -100
button13.state = “blank”

–[bottom row]–
local button31 = display.newImage( “blank.png” )
button31.x = button21.x
button31.y = button21.y +100
button31.state = “blank”
local button32 = display.newImage( “blank.png” )
button32.x = button22.x
button32.y = button22.y +100
button32.state = “blank”
local button33 = display.newImage( “blank.png” )
button33.x = button23.x
button33.y = button23.y +100
button33.state = “blank”

local cross = display.newImage(“cross.png”)
cross.x = (display.contentWidth / 2) -50
cross.y = 100
local circle = display.newImage(“circle.png”)
circle.x = (display.contentWidth/2) +50
circle.y = cross.y

function endGame()
clicked = 0;
local box = display.newRect(_W, _H, 0, 0);
box:setFillColor(255,255,255);
local endtxt = display.newText( “END GAME”, _W/2, _H/2, native.systemFont, 24 );
endtxt:setTextColor( 255,0,0 );
end

function changeToCross( event )
local button = event.target
if (button.state == “blank”) then
clicked = clicked + 1;
local endtxt = display.newText( clicked, 0, 0, native.systemFont, 24 );
button = display.newImage(“cross.png”);
button.x = event.target.x;
button.y = event.target.y;
end
if (clicked == 9) then
local endtxt = display.newText( clicked, 0, 0, native.systemFont, 24 );
end
end

function changeToCircle( event )
local button = event.target
if (button.state == “blank”) then
clicked = clicked + 1
button = display.newImage(“circle.png”)
button.x = event.target.x
button.y = event.target.y
end
if (clicked == 9) then
local endtxt = display.newText( “Circl Problem”, 0, 0, native.systemFont, 24 );
end
end

function changeCross (event)
local button = event.target

button11:addEventListener(“touch”, changeToCross)
button12:addEventListener(“touch”, changeToCross)
button13:addEventListener(“touch”, changeToCross)
button21:addEventListener(“touch”, changeToCross)
button22:addEventListener(“touch”, changeToCross)
button23:addEventListener(“touch”, changeToCross)
button31:addEventListener(“touch”, changeToCross)
button32:addEventListener(“touch”, changeToCross)
button33:addEventListener(“touch”, changeToCross)
end

function changeCircle (event)
local button = event.target

button11:addEventListener(“touch”, changeToCircle)
button12:addEventListener(“touch”, changeToCircle)
button13:addEventListener(“touch”, changeToCircle)
button21:addEventListener(“touch”, changeToCircle)
button22:addEventListener(“touch”, changeToCircle)
button23:addEventListener(“touch”, changeToCircle)
button31:addEventListener(“touch”, changeToCircle)
button32:addEventListener(“touch”, changeToCircle)
button33:addEventListener(“touch”, changeToCircle)
end

cross:addEventListener(“touch”, changeCross)
circle:addEventListener(“touch”, changeCircle)[/code] [import]uid: 133512 topic_id: 23331 reply_id: 323331[/import]

Touch events have phases. If you don’t limit your code to a phase, you get multiple calls.

To limit the up count to only when the player picks up his finger (completes the touch), try:

if event.phase == “ended” then

count = count + 1

end

other phases include “began”, “moved”, and “cancelled” (though I’ve had bad luck with that last one). [import]uid: 64596 topic_id: 23331 reply_id: 93439[/import]

Try this page, under the section Touch Events, for more details:

http://developer.anscamobile.com/content/events-and-listeners [import]uid: 64596 topic_id: 23331 reply_id: 93440[/import]

i just added the if event.phase == “ended” then but i still get multiple additions =/ [import]uid: 133512 topic_id: 23331 reply_id: 93442[/import]

@bia, you check for button.state==“blank” but never set it to NOT blank at any point.

instead of managing 9 aquares, read up on using arrays and functions, you can manage that with just one variable and easily.

some reading links for you
http://howto.oz-apps.com/2011/09/creating-dynamic-variables.html

http://howto.oz-apps.com/2011/09/if-i-were-carpenter-would-you-buy-my.html

http://howto.oz-apps.com/2011/09/tables-part-2.html

[import]uid: 3826 topic_id: 23331 reply_id: 93656[/import]