Android Question array of views problem

aeropic

Active Member
Licensed User
Longtime User
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

  • knight.zip
    6.7 KB · Views: 275

LucaMs

Expert
Licensed User
Longtime User
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)
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
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
 
Upvote 0

derez

Expert
Licensed User
Longtime User
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.
upload_2014-3-30_10-45-29.png


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...
 
Upvote 0

aeropic

Active Member
Licensed User
Longtime User
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

  • knight.zip
    6.7 KB · Views: 268
Upvote 0

aeropic

Active Member
Licensed User
Longtime User
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 !!!!!
 
Upvote 0

aeropic

Active Member
Licensed User
Longtime User
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
 
Upvote 0

aeropic

Active Member
Licensed User
Longtime User
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.


I too coded a solver algorithm. It was so fast under B4A that I introduced a step by step loop with a "doevent" in order to see the knight moving in real time !
Here it is : http://www.b4x.com/android/forum/threads/knights-ride.39645/

thanks for having teased me with this solver idea !
Alain
 
Upvote 0
Top