# Android Questionarray of views problem

#### aeropic

##### Active Member
Hi all,

I'm starting a new program based upon tick tack toe example.
The idea is to make a knight ride game on a chess board. You shall move the knight from its current position to a next "chess move valid" position and fill the 64 positions.
This part works very fine but I get a weird behavior when I try to allow a move back.

For instance, assume the current step is 3 (0, 1, 2 and 3 are displayed on the screen). When clicking on the "3" button, the knight goes back (button "3" text is cancelled and the previous position is stored as current one.)

This part too works fine.
I get into trouble at that step... If you click on the old "3" position, the one that has been cancelled, you get a message saying that a back move is done.

Under the debbugger, the sender button seems to be the button on which the "2" is displayed...
I'm fully stuck ....
Any help appreciated.
Alain

#### Attachments

• 6.7 KB Views: 101

#### LucaMs

##### Expert
All this is complex (I have to press a button randomly to guess the next step?).

I can give you a hint (on the fly) that can help you find the defect: add data into the type indices, for example State:

Type indices(x As Int, y As Int, state as int) (or Boolean)

#### Erel

Staff member
The problem is here:
B4X:
``LastMove = i``
Now both the button tag and LastMove point to the same variable. Later when you change LastMove xx and yy you are also affecting the button tag values (same object).

Solution:
B4X:
``````If ButtonsText(xx,yy) = CurrentStep Then
Dim LastMove As indices '<--- create a new object.
LastMove.x = xx
LastMove.y = yy``````

#### derez

##### Expert
I played with recursive algorithms some time ago and wrote the application that finds the solution to this riddle. This is one of the solutions.

Please note that the order of magnitude of the cycles is 10 *10^6 !
If you plan this as a challenge for users you should also plan a mid-life upgrade to the cover glass and buttons machinery...

#### aeropic

##### Active Member
The problem is here:
B4X:
``LastMove = i``
Now both the button tag and LastMove point to the same variable. Later when you change LastMove xx and yy you are also affecting the button tag values (same object).
Brilliant finding, Erel, as usual !
As I wanted to get the LastMove global variable correctly initialized after a back move I end with this piece of code which is working :
B4X:
``````If ButtonsText(xx,yy) = CurrentStep Then
LastMove.x = xx
LastMove.y = yy
Log(xx & " back move done " & yy)
Return   '<=================================
End If``````
and
B4X:
``````CurrentStep = CurrentStep+1
LastMove.x = i.x
LastMove.y = i.y``````
However, I must admit I have not fully understood how this solves my mistake:

What is the difference between
LastMove = i
and
LastMove.x = i.x
LastMove.y = i.y

In the first case I understand that LastMove "follows" i sharing the same pointer. In the second case, there is a physical change of the memory values of the indices .x and .y, meaning that when i will change, LastMove is not affected ?

#### Attachments

• 6.7 KB Views: 104

#### Erel

Staff member
LastMove should not point to the same indices object that the button tag points to. Otherwise when you set LastMove.X you are actually changing the field of the button tag.

#### aeropic

##### Active Member
Please note that the order of magnitude of the cycles is 10 *10^6 !
If you plan this as a challenge for users you should also plan a mid-life upgrade to the cover glass and buttons machinery...
Several years ago, when I got a macintosh, I too coded a solver based upon a randomize algorithm with 100% CPU allocated to solve the problem.
It worked fine and found plenty of solutions.

I aggree that the use of "last move" option may lead to premature wear of the front glass . however basic user will stop before 64 presses !!!!!

#### aeropic

##### Active Member
LastMove should not point to the same indices object that the button tag points to. Otherwise when you set LastMove.X you are actually changing the field of the button tag.
OK I understand this pointer problem: Lastmove and button tag shall point to different variables.

But is it the case when writing ?
LastMove.x = i.x
LastMove.y = i.y

#### Erel

Staff member
No. The above code is fine.