A Flash Developer Resource Site

Results 1 to 6 of 6

Thread: swapping array elements

  1. #1
    KoolMoves Moderator blanius's Avatar
    Join Date
    Jul 2001
    Location
    Atlanta GA
    Posts
    5,244

    swapping array elements

    Decided not to continue this in the bitmap thread.....Still working occasionally on the video poker thing . Decided that having another array for the hand complicated some things and wasn't the way to go.....

    I have a shuffled array of 52 movie clips each with a card graphic on it, a suit value and a value. I'm going to treat the first 5 cards as always being the "hand" so what I'm now trying to do is make a swap function but I'm not doing something right and cannot seem to make it work.....
    code:

    swap=function(i,j){

    i=j
    j=temp
    }
    //swap(test[0],test[1])
    test=new Array(1,2,3,4)
    txt1.text=test[0]
    txt2.text=test[1]



    didn't work nor did
    code:

    Array.prototype.swap=function(i,j){
    var temp=this[i]
    this[i]=this[j]
    this[j]=temp
    }
    deck.swap(x,cardsDrawn)

    Last edited by blanius; 12-13-2006 at 12:30 AM.

  2. #2
    Senior Member
    Join Date
    Dec 2002
    Location
    Netherlands
    Posts
    1,632
    That your first attempt doesn't work is logical since changing i and j variables within the function doesn't affect the parameters that were used to set these variables.

    Your second attempt looks fine to me.
    What exactly isn't working ?

  3. #3
    KoolMoves Moderator blanius's Avatar
    Join Date
    Jul 2001
    Location
    Atlanta GA
    Posts
    5,244
    The first one was me trying things. The second one is the one I was actually using. It does turn out to work... Here's the problem it turns out.

    the assignment of the movieclips changed but not the way I had expected. for example a 2 of clubs is in deck[0] and I swap it with deck[5] now deck[5] is 2 of clubs and in the has swapped x,y position as well with deck[0] so now I've got to get the x and y and such to swap back...... I wonder if I should make it part of my swap function to somehow have them NOT swap paramaters.

  4. #4
    Senior Member
    Join Date
    Dec 2002
    Location
    Netherlands
    Posts
    1,632
    The coordinates also being swapped is logical since the only thing swapped are the movieclip references when it's an array of movieclips.

    If you only want to swap some things, you could try something like this ...
    Code:
    Array.prototype.objectSwap = function(i0, i1, props){
     var i,p,t;
     props = props.split(',');
     t = new Object();
     for (i = 0; i < this.length; i++){
      p = props[i];
      t[p] = this[i0][p];
      this[i0][p] = this[i1][p];
     }
     for (i = 0; i < this.length; i++){
      p = props[i];
      this[i1][p] = t[p];
     }
    }  
      
    a = [{x:1,y:2,z:7},{x:3,y:4,z:8},{x:5,y:6,z:9}];
    
    // swap properties x and z of item 0 with x and z of item 2
    a.objectSwap(0,2,'x,z');
    
    txt1.text = 'x:' + a[0].x + ' y:' + a[0].y + ' z:' + a[0].z;
    Another option of course is to change the function to swap everything except the properties you specify.

  5. #5
    KoolMoves Moderator blanius's Avatar
    Join Date
    Jul 2001
    Location
    Atlanta GA
    Posts
    5,244
    kept it simple as I needed to move the new card to the drawpile to animate it anyway. It just confused me as it didn't LOOK like the cards were swapped. Think I have all the basic animation and card handling done now, all thats left to do is the hand evaluation and the clean up for the next hand. I have that pretty well mapped out so that could go quickly then I've got the basic game ready. I have a few ideas for extras to add but we'll see I'm growing weary of this already....

    game so far: http://bretlanius.com/flash/videopoker.html

  6. #6
    KoolMoves Moderator blanius's Avatar
    Join Date
    Jul 2001
    Location
    Atlanta GA
    Posts
    5,244
    in case anyone is interested in looking over my shoulder here's what I have so far.

    Code:
    outline=function(m,x,y){
    	w=x+71
    	h=y+99
    	//lineStyle(thickness:Number, rgb:Number, alpha:Number, pixelHinting:Boolean, noScale:String, capsStyle:String, jointStyle:String, miterLimit:Number)
    	m.lineStyle(2,0x9FFFCF,50,true,"normal","round","round")
    	m.beginFill(0x9FFFCF,60)
    	m.moveTo(x,y)
    	m.lineTo(w,y)
    	m.lineTo(w,h)
    	m.lineTo(x,h)
    	m.lineTo(x,y)
    	m.endFill()
    	
    	}
    	
    
    
    _global.cards = flash.display.BitmapData.loadBitmap('deck');
    //DropShadowFilter([distance], [angle], [color], [alpha], [blurX], [blurY], [strength], [quality], [inner], [knockout], [hideObject])
    myDropShadow = new flash.filters.DropShadowFilter(4,45,0x00000000,10,10,10);
    MovieClip.prototype.createNewCard = function(name,depth,deck,col,row){
     
     // create the movieclips to hold the card
     
     var m = this.createEmptyMovieClip(name,depth);
     var m1 = m.createEmptyMovieClip('m',1); m1._x = 36; m1._y = 50;
     var m2 = m1.createEmptyMovieClip('m',1);
     m2.filters = [myDropShadow];
     // copy the passed parameters to the movieclip
     
     m.deck = deck;
     m.col = col;
     m.row = row;
     m.$s = true; // flip state
     
     // add some functions
     
     m.destroy = function(){
      this.bmp.dispose();
      this.swapDepths(this.getNextHighestDepth());
      this.removeMovieClip();
     }
     
     m.$copy = function(col,row){
      this.bmp.dispose();
      this.bmp = new flash.display.BitmapData(72,100,true);
      
      this.bmp.applyFilter(this.bmp,this.flash.geom.rectangle, new flash.geon.Point(0,0),myDropShadow)
      this.bmp.copyPixels(_global.cards,new flash.geom.Rectangle(col * 72,row * 100,72,100),new flash.geom.Point(0,0));
      this.m.m.attachBitmap(this.bmp,1,'auto',true);
      this.m.m._x = -38; this.m.m._y = -50;
     }
    
     m.flipCard = function(){
      this.$s = !this.$s;
      if (this.$s) this.$copy(this.col,this.row) else this.$copy(this.deck,4);
     }
    
     m.flipCardAnimated = function(){
      this._x-=8
      this.$f = 0;
      this.onEnterFrame = function(){
       this.$f++;
       this.m._xscale = Math.abs(Math.cos(Math.PI * this.$f * .1)) * 100;
       if (this.$f == 5){
        this.flipCard();
       } else if (this.$f == 10){
        this.onEnterFrame = null;
        this.onAnimationCompleted();
        this._x+=8
       }
      }
     }
    
     m.moveCardAnimated = function(dx,dy){
      this.$f = 0;
      this.sx = this._x;
      this.sy = this._y;
      this.dx = dx;
      this.dy = dy;
      this.onEnterFrame = function(){
       this.$f++;
       this._x = this.sx * (10-this.$f) * .1 + this.dx * this.$f * .1;
       this._y = this.sy * (10-this.$f) * .1 + this.dy * this.$f * .1;
       if (this.$f == 10){
        this.onEnterFrame = null;
        this.onAnimationCompleted();
       }
      }
     }
    
     // show card
     
     m.flipCard();
     
     // return refrence
      
     return m;
    
    }
    
    
    Array.prototype.shuffle = function() {
    	len = this.length;
    	for (var i=0; i < len; i++) {
            rand = Math.floor(Math.random()*len);
    		//swap current index with a random one
    		temp = this[i];
    		this[i] = this[rand];
    		this[rand] = temp;
    	}
    }
    Frame 3 code
    Code:
    mc7.gotoAndStop(3)
    //starting values
    reset=function(){
    	cash=500
    	cash_txt.text=cash
    	bet=0
    	cr=100//card row
    	cardStart=50
    	handOver=false
    	}
    	
    //function for array.sort	
    compareHands =function(a,b){
    	if (a.value<b.value) return(-1);
    	if (a.value==b.value) return(0)
    	if (a.value>b.value) return(1)
    }		
    
    //Get Value of hand
    function handVal(){
    	hand=deck.slice()//get copy of hand
    	hand.sort(compareHands)//sort it in numerical order to test for straight
    	//check for staights
    	straight=true
    	for (i=0;i<4;i++){
    		if(hand[i].value+1!=hand[i+1].value) straight=false;
    	}//end for	
    	//check for 10-Ace straight
    	if(hand[0]==1 && hand[1]==10 && hand[2]==11 && hand[3]==12 && hand[4]==13) straight=true
    	
    	//check for flush
    	flush=true
    	for (i=1;i<5;i++){
    		if (hand[i].suit!=hand[0].suit) flush=false
    	}//end for
    	
    	//count pairs.
    	counts=new Array()//make clean array of 0s
    	for (i=0;i<4;i++){
    		counts.push(0);
    	}
    	for (i=1;i<5;i++){
    		counts[hand[i].value]++
    	}
    	pair=twoPair=threeOfAKind=fourOfAKind=false
    	for(i=1;i<4;i++){
    		if (counts[i]==2){
    			if (pair){
    				twoPair=true;
    			}else{
    				pair=true;
    			}
    		}else if (counts[i]==3){
    			threeOfAKind=true
    		}else if (counts[i]==4){
    			fourOfAKind=true
    			}
    	}//end for
    	jackOrHigher=false
    	for(i=1;i<14;i++){
    		if (((i==1) | (i>10)) && (counts[i]>=2)){
    			jackOrHigher=true;
    		}
    	}
    	//******************working here**********************/
    	
    	
    	//***************************************************
    	
    }//end handVal	
    	
    reset()
    
    
    createNewCard('drawCard',getNextHighestDepth(),3,0,1);
    
    drawCard._x = 400; drawCard._y = cr;
    Array.prototype.shuffle = function() {//adds the method shuffe() to all Arrays to shuffle it's elements
       var len = this.length;
       for (var i=0; i < len; i++) {
            var rand = Math.floor(Math.random()*len);
          //swap current index with a random one
          var temp = this[i];
          this[i] = this[rand];
          this[rand] = temp;
       }
    }
    Array.prototype.swap=function(i,j){
    	var temp=this[i]
    	this[i]=this[j]
    	this[j]=temp
    	}
    function createDeck(){
    var deck=new Array()
    for(var s=0;s<4;s++){
       for(var n=0;n<13;n++){
           	deck.push(createNewCard('Card'+s.toString()+n.toString(),getNextHighestDepth(),3,n,s));
          	//deck[deck.length-1]._visible=false
          	deck[deck.length-1]._x=-100
          	deck[deck.length-1]._y=cr
          	deck[deck.length-1].value=n+1
          	deck[deck.length-1].suit=s
          	deck[deck.length-1].onRelease = drawCard.onReleaseOutside = function(){
          	if(!handOver){
          		this.flipCardAnimated();
          		this.onAnimationCompleted = function(){
          			this.onAnimationCompleted = function(){}
          		}
          	}
          	}
          }
       }
    return deck;
    }
    
     var m = this.createEmptyMovieClip("layout",getNextHighestDepth());
     for (x=cardStart;x<320;x+=80){
     	outline(m,x,100);
     }
    
    deck = new Array()
    
    deck=createDeck()
    deck.shuffle()
    deck.shuffle()
    
    //lightrn logo
    bgLogo._alpha=35
    
    
    chip.onRelease=function(){
    status.text=""
    	if (!handDealt){
    		bet+=5
    		txt3.text=bet
    		cash-=bet
    		cash_txt.text=cash
    		}else{
    		status.text="No more bets until next Hand"
    		}
    	}
    
    cardsDrawn=0
    cp=cardStart
    handDealt=false
    drawCard.onRelease = drawCard.onReleaseOutside = function(){
    mc7.gotoAndStop(2);
    	if (bet!=0){
    		if (!handDealt){
    		
    		var hand=new Array()
    			status.text="Dealing initial cards..."
    			for (x=0;x<4;x++){
    			status.text=deck[0].value
    				deck[x]._x=this._x
     				deck[x]._y=cr
     				deck[x]._visible=true
     				deck[x].moveCardAnimated(cp,cr)
    				cardsDrawn++
     				cp+=80
     				deck[x].onAnimationCompleted = function(){
      					this.onAnimationCompleted = function(){
          				}
      				this.flipCardAnimated();
      				status.text=""
     				}//end onAnimationCompteted
     			}//end for
     				handDealt=true
     		}else{//end if handdealt
      			mc7.gotoAndStop(3);
     			//deal flipped cards
     		if (!handOver){
     			for (x=0;x<4;x++){
     			status.text="Dealing new cards"
     				if (!deck[x].$s){
     					var tempx=deck[x]._x
     					deck.swap(x,cardsDrawn)
     					deck[x].ox=deck[x]._x
     					deck[cardsDrawn]._x=-100
     					deck[x]._x=drawCard._x
     					deck[x]._visible=true
     					deck[x].moveCardAnimated(tempx,cr)
     					deck[x].swapDepths(9999)
    					cardsDrawn++
     					deck[x].onAnimationCompleted = function(){
      							this.onAnimationCompleted = function(){
      							status.text=""
          					}
      					this.flipCardAnimated();
      					
     					}//end onAnimationCompteted
     					}
     					handOver=true
     			}//end for loop
     		}
     				
      			}//end else
     
     
     		}else{//end if bet
    			status.text="Place Bet First"
    		}//end if Dealt
    
    		}
    }//end drawcard button action

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center