0
votes

Please can you help me out I am new to as3 and I am trying to create a shuffled deck using the Fisher-Yates Algorithm. When I run the code with ctrl-enter it compiles with no errors but when I try to output it with trace(); it comes back with:

Scene 1, Layer 'actions', Frame 1, Line 6 1120: Access of undefined property shuffledArray.

Like I said I am new to this and it will be me doing something very stupid but all the same i'm stuck.

Here is the code

package src.CardDeck
{
    public class CardDeck
    {
        public var allCards:Array = [];
        public var cardNames:Array;
        public var cardValues:Array;
        public var gameType:String;
        public var drawnCards:uint = 0;

        public function CardDeck(game:String)
        {
            gameType = game;
            cardNames = ["Ace","Two","Three",
                         "Four","Five","Six",
                         "Seven","Eight","Nine",
                         "Ten","Jack","Queen","King"];
            if(gameType == "texasholdem")
            {
                cardValues = [1,2,3,4,5,6,7,8,9,10,10,10,10];
            }
            makeSuit("Spade");
            makeSuit("Heart");
            makeSuit("Diamond");
            makeSuit("Club");
        }
        function makeSuit(suitString:String):void
        {
            var card:Object;

            for(var i:uint = 0; i < cardNames.length; i++)
            {
                card = {};
                card.cardType = suitString;
                card.cardName = cardNames[i];
                card.cardValue = cardValues[i];
                card.isDrawn = false;
                allCards.push(card);
            }
        }

        public function shuffleFisherYates():Array 
        {
            var shuffledArray:Array = [];
            var randomCardIndex: int;
            do 
            {
                randomCardIndex = Math.floor(Math.random()* allCards.length);
                shuffledArray.push(allCards[randomCardIndex]); // add to mix
                allCards.splice(randomCardIndex,1); // remove from deck
            }while(allCards.length); // Meaning while allCards.length != 0
            return shuffledArray;


        }
    }
}

and here is the .fla actions layer

import src.CardDeck.CardDeck;

var deck:CardDeck = new CardDeck("texasholdem");
trace(shuffledArray);

I know its probably something silly but i'm struggling.

Thanks in advance!

Paul

2

2 Answers

1
votes
var deck:CardDeck = new CardDeck("texasholdem");
trace(shuffledArray);

This doesn't work because shuffledArray isn't defined there.

Try :

var deck:CardDeck = new CardDeck("texasholdem");
var array:Array = deck.shuffleFisherYates();

for(var i:int=0; i<array.length; i++)
{    
    trace(array[i].cardName);
    trace(array[i].cardType);
    trace(array[i].cardValue);
    trace(array[i].isDrawn);
}
0
votes

"shuffledArray" is a property inside of your CardDeck object. To access public methods and properties within it, you need to use the dot syntax:

trace(deck.shuffleFisherYates());

However, depending on what you are doing, you may not need to really be accessing the array directly, if your CardDeck object is meant to control the entire deck.