Hello. I stumbled upon such a problem: I have 3 buttons (ImageButton), it is necessary after the 1st click on one of the buttons, to track the subsequent click (OnClick) on the next button (say-2). The 3rd will not work. How to track the first click, I understand. But how to track the second to the desired position, no PS I create chess, I try to implement the movement of the figure by changing the ImageButton on the click

  • More precisely how to track exactly the 2nd click on the screen - Andrew

2 answers 2

@Andr asked how to programmatically implement the movement of figures? I decided to write an algorithm. Let's start!

  1. When you click on the cell, we turn to the array of the field and determine which figure stands there. We write the value in int1 (pawn - 1 , knight - 2 , ...) and the position of the piece in int2 and int3 (better in the format of two numbers - for example 4 and 2 ).
  2. If the pressed cell is empty, we do nothing; otherwise, we are waiting for another cell to be pressed - the turn cell.
  3. When you click on the turn cell, we build the switch - see if a figure can walk there. This is where the created int comes in handy. If everything is correct, then we carry out the second check - is that cell occupied? We do this through an array. We make the final decision and make (or do not make) the movement of the figure. Change the array of shapes. It is worth considering whose course !!!

It is worth noting - initially the array is created according to the rules of chess - the arrangement of figures takes place. We give an example of the algorithm at the beginning of the game.

  1. The user clicked on the pawn with the position D2. Write the value 1 to int2 value 4 to int3 value 2 to int3 .
  2. We skip it.
  3. Check in switch(int1) value. Since it is equal to 1 , then we look at possible moves for the pawn. It meets the requirements. There is a move and write a new position of the figure in the array.

    This can be done by creating an additional variable; You can create a class that will store it, or you can do it right in the activity class. Here is the sequence of actions:

    • Create a button variable with a value of false .
    • When you click the first button, we change its value to true .
    • When you click on the second button:
      • If the button is false , do nothing.
      • If the button has a value of true , perform the action you need and set the variable to false .
    • This is understandable, thanks. I have a shah box on my screen consisting of an ImageButton. After pressing a button with a figure (for example, a pawn), how can I track the subsequent pressing of a button above, and the other buttons are not counted. I have 64 of them all on the screen. Most interested in this problem - Andrew
    • Make an array field. In it, track their current positions, depending on them, build an algorithm. It will also be very convenient to track cells that are inaccessible for a turn. And let your buttons turn to a specific element of the array corresponding to the pressed button. Do not forget to put a tick next to the answer! - Daniel Chizhevsky
    • It will also be very convenient to track cells that are inaccessible for a turn. This is the problem that I can not implement it programmatically)) Everything that you wrote above I have already done)) Can you tell me how to implement the software to track the right cell clickAndrew
    • Now I will write another answer. - Daniel Chizhevsky