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?

enter image description here


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.

| improve this answer | |
  • \$\begingroup\$ What is the floor() method doing? What is the squareResolution if my image is 54x54? \$\endgroup\$ – Enayat Muradi Apr 10 '14 at 18:44
  • \$\begingroup\$ 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. \$\endgroup\$ – PatrickB Apr 10 '14 at 21:00
  • \$\begingroup\$ What is the squareResolution and gridResolution? How do I calculate it? \$\endgroup\$ – Enayat Muradi Apr 11 '14 at 9:27
  • \$\begingroup\$ 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' \$\endgroup\$ – Herp Apr 14 '14 at 10:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.