Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Newcomers AppGameKit Corner / Array Element Swap in AGK

Author
Message
yoda333
12
Years of Service
User Offline
Joined: 3rd Apr 2012
Location:
Posted: 18th May 2015 00:26
In a single dimentioned array with unique values I wish to do the following.

I have two values from the array called result - say in element position 5 (Lets say the value 36) and element position 7 (Lets say has the value 13)

After something happens within the program, I want element position 5 to contain the value 13 and the element in position 7 to contain the value 36.

How do I do this with one command. The order is unique in the array. (I don't want to mess up the order) The array is called result[]


No stupid answers please. Thanks
yoda333
12
Years of Service
User Offline
Joined: 3rd Apr 2012
Location:
Posted: 18th May 2015 01:04
Also - what if the two values, 36 and 13 are in a couple of variables and their element positions in the array is not known. So a search of the array for both values to find the elements is required before the elements are swapped over. Thanks
Kevin Picone
21
Years of Service
User Offline
Joined: 27th Aug 2002
Location: Australia
Posted: 18th May 2015 08:12 Edited at: 18th May 2015 08:14
if there's no build in swap command, then you just read both values into some temp variables and write them back.



depending upon how frequently you need to swap in your routines, then perhaps rolling the swap logic into a custom swap function might help reduce the code.

something like




alternatively you might want to use one less temp..



none the less, the principal is the same.. it's possible to swap without using temp variables, by various math operators which can be a fun little programming challenge, but generally overkill

yoda333
12
Years of Service
User Offline
Joined: 3rd Apr 2012
Location:
Posted: 18th May 2015 10:43
Thanks Kevin - Brilliant

//hd contains the spritenumber clicked
//sx is the sprite number of the blank tile

for fra = 1 to 16
if (result [fra-1]) = hd then posiea = fra-1
next fra

for frb = 1 to 16
if (result [frb-1]) = sx then posieb = frb-1
next frb

Temp1= result[posiea]
Temp2= result[posieb]

result[posiea] = Temp2
result[posieb] = Temp1

// then I redraw the 15 puzzle

Thanks for your greatness. Cheers
IronManhood
9
Years of Service
User Offline
Joined: 6th Feb 2015
Location: US
Posted: 18th May 2015 14:10
http://www.appgamekit.com/documentation/guides/12_array_changes.htm

There is a swap command mid page.

You use it like this, myArray.swap(index1, index2).
yoda333
12
Years of Service
User Offline
Joined: 3rd Apr 2012
Location:
Posted: 30th May 2015 04:47
Thanks very much for the answers from both forum members.
I'm using both examples provided in my program.

All the best
=PRoF=
21
Years of Service
User Offline
Joined: 17th Mar 2003
Location: Milton Keynes, UK
Posted: 3rd Jun 2015 14:29 Edited at: 3rd Jun 2015 14:30
Nice, I was almost completely unaware of the extensive changes to arrays and the way they work which have been made in AGK2.

Login to post a reply

Server time is: 2024-04-19 21:24:06
Your offset time is: 2024-04-19 21:24:06