Jump to content

Recommended Posts

Hello :)

I recently started learning Actionscript, and I am sort of stuck understanding the logic behind something, so if someone can please explain it to me clearly and dumb it down to a beginners logic, it'd be awesome.

My code is simple. I am making a concentration game, and I used arrays to generate tiles (max 20 tiles, so two of each cards from 0-9):

00
11
22
33
44 etc...

Anyways, like all games of concentration, you need to add randomness, so I I added a for-loop (btw, I'm following a tutorial, I didn't actually write this :P), and it basically swaps the numbers around so you get different combinations everytime. This is the part that confuses me...
 

 

package {
    // importing classes
    import flash.display.Sprite;
    // end of importing classes
    public class Main extends Sprite {
        public function Main() {
            // variables and constants
            /* Different types of constants
            - int represents an integer that can be negative or positive, called signed integer
            - uint represents an integer that represents ONLY positibe numbers, called unsigned integer
            - Number (uppercase "N") is used to represent whole and fractional numbers, regardless of their sign (i.e: positive or negative)
            ** Important: you can use Number to define constants if you are unsure of int/unit
            */
            const NUMBER_OF_TILES:uint=20;
            var tiles:Array=new Array();
            // end of variables and constants
            // tiles creation loop
            for (var i:uint=0; i<NUMBER_OF_TILES; i++) {
                // push() method adds an element to the end of the array
                // Math.floor() method returns the number that is less than or equal to the parameter inside (i.e. rounds down)
                tiles.push(Math.floor(i/2));
            }
            trace("My tiles: " +tiles);
            // end of tiles
            // shuffling loop in order to create randomness (it's very easy to guess a game of concentration without it)
            var swap, tmp:uint;
            for (i=NUMBER_OF_TILES-1; i>0; i--) {
                swap=Math.floor(Math.random()*i);
                tmp=tiles[i];
                tiles[i]=tiles[swap];
                tiles[swap]=tmp;
            }
            trace("My new shuffled tiles: "+tiles);
            // end of shuffling loop
        }
    }
    }
 

 



For the second for loop, the person is using the condition i = NUMBER_OF_TILES-1, so if we go with the defined variable above which is 20, 20-1 is 19.

19 is added into swap, so a number between 0 and 19 (19 not included), let's say for simplicity sake it's 4 (rounded down with Math.floor()). Can someone tell me what happens now?

With the above code, i get the following results:

My tiles: 0,0,1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9
My new shuffled tiles: 9,3,9,2,5,4,1,3,7,0,8,4,7,2,6,1,5,6,0,8



However, if I remove the tiles[swap]=tmp;, the output is:

My tiles: 0,0,1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9
My new shuffled tiles: 0,0,0,0,0,0,0,1,1,4,2,3,0,5,5,1,6,5,0,3



Can someone tell me the significance of the tmp? And explain me how the swap is actually occurring, and the logic behind it.  

In my understanding, if swap generates 4, then it's

tmp= tiles[19]
tiles[19] = tiles[4]

What does that mean exactly.. Thank you

By the way, if it helps, the tutorial is using Fisher-Yates shuffle algorithm.

Edit: ignore my little comments. They are just notes I made for myself.
Link to comment
https://forums.phpfreaks.com/topic/275954-arrays-help-in-actionscript/
Share on other sites

tmp=tiles;

tiles=tiles[swap];

tiles[swap]=tmp;

 

You have two variables you want to swap. You need a third to hold one while you move the other.

If I have two bowls filled with two soups, how do I get each soup into the other bowl?

Soup A into bowl C

Soup B into bowl A

Soup C into bowl B

tmp=tiles;

tiles=tiles[swap];

tiles[swap]=tmp;

 

You have two variables you want to swap. You need a third to hold one while you move the other.

If I have two bowls filled with two soups, how do I get each soup into the other bowl?

Soup A into bowl C

Soup B into bowl A

Soup C into bowl B

Thank you :)

 

I just got it! :D That was so simple

 

Just one more question:

 

Since it's number of tiles -1, what would happen to the 20th tile? Since the first run would be with 19, then 18, etc.

Edited by zerotolerance
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.