Page 1 of 1
Convert old Basic dual FOR's to LC
Posted: Mon Aug 12, 2019 9:57 pm
by KRY_M
I have a problem trying to migrate an old app build by me long time ago.
I can't find any example of exiting 2 FOR loops (one for x and one for y) when i found my value inside of an Array
ex:
Code: Select all
For i = 1 To 6
For j = 1 To 5
If a(i, j) = 1 And a(i, j + 1) = 0 Then
om_c = 1
Beep
Call mut_calc
GoTo 102 //this is for exiting loops
End If
Next j
Next i
102: Next instruction
Re: Convert old Basic dual FOR's to LC
Posted: Mon Aug 12, 2019 10:18 pm
by bogs
I'd probably use repeat myself, since that is pretty much what the statement your using does in VB.
Re: Convert old Basic dual FOR's to LC
Posted: Mon Aug 12, 2019 11:40 pm
by bn
Hi Kry_M,
welcome to the forum.
In Livecode you can only exit a repeat loop of one level deep. You want to jump out of a 2 level repeat loop.
The trick is to use a flag.
Code: Select all
on mouseUp
local tExitRepeats -- declaring of local var optional
put false into tExitRepeats -- your flag
repeat with i = 1 to 6
repeat with j = 1 to 5
if j = 5 AND i = 3 then -- your condition here
put true into tExitRepeats -- your flag
exit repeat
end if
end repeat
if tExitRepeats then exit repeat -- leave outer repeat loop
end repeat
end mouseUp
Kind regards
Bernd
Re: Convert old Basic dual FOR's to LC
Posted: Tue Aug 13, 2019 10:57 pm
by KRY_M
Thank you, it did the trick ! my code now looks like a spagetti code but it is too complex to modify its structure...
Code: Select all
repeat with i = 1 to 6
repeat with j = 1 to 5
if a[i][j]=0 or a[i][j+1]= 1 then //or ! nu and
next repeat //next j
end if
repeat with k=j+1 to 6
put false into sw //adaugire
if a[i][k]=1 then
exit repeat //exit k only
end if
put 0 into a[i][j]
put 1 into a[i][k]
put 0 into dep
repeat with t= 2 to 6
repeat with s= 1 to 6
if a[t][s]=0 then
next repeat //next s
end if
repeat with v=t-1 down to 1
if a[v][s]=1 then
exit repeat //next s
end if
add 1 to dep
end repeat //v
end repeat //s
end repeat //t
put 1 into a[i][j]
put 0 into a[i][k]
if depo<= dep then
exit repeat // next j
end if
put dep into depo
put i into io
put j into jo
put k-j into po
if depo=0 then //exit k , j , i
put true into sw1 //am gasit o mutare o efectuez si ies din loop
exit repeat //exit k -> j,i
end if
end repeat //k
if sw1 then exit repeat -- leave outer repeat loop
end repeat //j
if sw1 then exit repeat -- leave outer repeat loop
end repeat //i
Re: Convert old Basic dual FOR's to LC
Posted: Wed Aug 14, 2019 8:18 am
by bogs
Speaking as someone who has mangled code on more than one occasion, I think there must certainly be a shorter and probably better way to write that, but having just woke up, it didn't come to me off the top of my head
Re: Convert old Basic dual FOR's to LC
Posted: Thu Aug 15, 2019 9:10 pm
by bn
Hi Cristian,
I just had a play with "Impas Flex" on LivecodeShare.
I did not get it to work at first then I looked at the code of "command afisez"
There you
Code: Select all
set the filename of image (l2) to xxx
That only works on files that are on disk. And without a fully referenced fileName including the path it only works if the image you reference is in the same folder as the default folder.
If you change the code to
Code: Select all
put image "gol.png" of card 2 into image (l2)
then it works as expected.
You would change the code for all instances where you want this to happen.
A syntactic variant would be
Code: Select all
set the text of image (l2) to the text of image "bulina2.png" of card 2
Whatever suits you.
Apart from that it seems to work unfortunately I don't know the rules of the game.
Kind regards
Bernd
Re: Convert old Basic dual FOR's to LC
Posted: Thu Aug 15, 2019 10:08 pm
by KRY_M
Thank you for support, I modified the code and now it works (i forgot to test it outside of the working folder)
The rules of the game are simple each player moves one piece at a time, the user can move up one or two squares an the PC moves horizontally only one or two squares. The user who can't move first looses ! Simple !
Re: Convert old Basic dual FOR's to LC
Posted: Thu Aug 15, 2019 11:07 pm
by bn
It works now and I played two rounds. I am not much into games but this is nice.
And it is a nice adaption to Livecode coding.
Kind regards
Bernd