# How can I determine which square in my memory game was pressed?

I want to develop an simple memory game on Android.

The memory game have squares like the picture below. How should I determine which of the square is pressed? Should I use image buttons? Personally I don't think it is an good idea to make the game with image buttons. Could you suggest me an solution on how I should determine which of the square is pressed? Here is an idea:

you probably know of the dimensions of the screen, you know of the resolution of the squares, and at any time when a point on the screen is touched, you know this points x and y coordinates.

int xSquare = floor(x/squareResolution)
int ySquare = floor(y/squareResolution)


If you store all your squares in a one dimensional array, the square you want is

squares = new Square[gridResolution*gridResolution];
theSquareYouWant = squares[ySquare *gridResolution + xSquare]


If you store it in a 2d array you want square

squares = new Square[gridResolution][gridResolution];
theSquareYouWant = squares[xSquare][ySquare]


You should not use image buttons. If you want to animate a square being pressed, create an animated sprite or whatever at the space occupied by the square and then remove it once the animation has finished.

• What is the floor() method doing? What is the squareResolution if my image is 54x54? Apr 10, 2014 at 18:44
• floor() is a mathematical function. For positive values, it's basically truncation, i.e. floor(2.1) = 2.0, floor(2.5) = 2.0, floor(2.9) = 2.0. Apr 10, 2014 at 21:00
• What is the squareResolution and gridResolution? How do I calculate it? Apr 11, 2014 at 9:27
• squareResolution and gridResolution is arbitrary, it's whatever you feel like. In your image, the squares looks to be 25*25 pixels and the gridResolution looks like 6*6'
– Herp
Apr 14, 2014 at 10:56