« Web

Poker Game

Play the game at www.opalgames.com/poker

Sample from the game in progress:

Click a card from the deck to deal that card.

Click deal to deal randomly from the deck.

When all 7 cards are dealt click deal to reveal the hands.

The winning hand will get a check mark and all the hands will highlight the best five cards they can play.

My next step is to work on calculating odds by determining what outs are left in the deck that could help you hand.

Here's the code for my Hand object mostly handles the scoring so the Hand object knows what it's score is at all time.


package
{
	import flash.display.MovieClip;
	import flash.text.TextField;  	
	
	public class PokerHand extends MovieClip
    {		
		public var playerNumber:Number = 0;
		public var handTypes:Array = new Array("high","pair","two pair","set","strait","flush","full house","four of kind","strait flush","five of a kind");		
		public var cardSuits:Array = new Array("clubs","diamonds","hearts","spades");
		public var cardNumbers:Array = new Array("A","2","3","4","5","6","7","8","9","10","J","Q","K");		
		
		public var i:Number;
		public var j:Number;
		
		public var cards:Array = new Array();								
		public var suits:Array = new Array();
		public var values:Array = new Array();		
		
		public var pairs:Array = new Array();
		public var trips:Array = new Array();
		public var quads:Array = new Array();
		public var fives:Array = new Array();
		
		public var boats:Array = new Array();
		public var flushes:Array = new Array();
		public var straits:Array = new Array();	
				
		public var bestFive:Array = new Array();
		public var handType:String = ""; //the printed score				
		public var score:Number = 0;
		public var winningType:Boolean = false;
		public var winningHand:Boolean = false;
			
	    public function PokerHand():void
        {							
		
        }

		private function returnFace(tempValue)
		{
			var cardFace = tempValue;
			if(cardFace == 14){cardFace = "A";}
			if(cardFace == 13){cardFace = "K";}
			if(cardFace == 12){cardFace = "Q";}
			if(cardFace == 11){cardFace = "J";}
			if(cardFace == 1){cardFace = "A";}
			return cardFace;
		}		
		
		public function getHandStats():void
		{
			trace("\n")
			trace("---" + name + "---");
			trace("cards: " + cards.length);
			var cardString:String;
			for(i=0; i < cards.length;i++){					
				if(cards[i].inBestFive == true){cardString += (cards[i].name)+", "};	
			}
			trace(cardString);
			trace("Type: " + handType);
			trace("Score: " + score);
			trace("Winning Type? " + winningType);
			trace("Winning Hand? " + winningHand);			
		}

		public function scoreHand():void
		{	
			cards.sortOn("cardValue", Array.NUMERIC | Array.DESCENDING);
			var tempArray:Array;			
			//build 2d arrays to hold cards
			for(j=0; j < 4;j++){				
				tempArray = new Array();
				suits.push(tempArray);
			}			
			for(j=0; j < 14;j++){
				tempArray = new Array();
				values.push(tempArray);
			}							
			//LOOP HAND
			for(i=0; i < cards.length;i++){									
				//count up suit array
				for(j=0; j < 4;j++){
					if(cards[i].suitId == j){
						suits[j].push(cards[i]);
					}
				}				
				//count up value array
				for(j=0; j < 14;j++){
					if(cards[i].cardValue == j+1){
						values[j].push(cards[i]);						
					}
				}												
			}						
			//find high card										
			score = cards[0].cardValue;
			handType = returnFace(score) + " " + handTypes[0];			
			//count up values and build pairs etc
			for(i=0; i < values.length;i++){
				var tempPush = values[i];
				//pairs
				if(values[i].length == 2){
					pairs.push(tempPush);									
				}
				//trips
				if(values[i].length == 3){
					trips.push(tempPush);
				}
				//quads
				if(values[i].length == 4){
					quads.push(tempPush);
				}	
				//fives
				if(values[i].length == 5){
					fives.push(tempPush);				
				}					
			}
			//score hand by type											
			//paris
			if(pairs.length > 0){							
				pairs.sortOn("");
				pairs.reverse();				
				score = 100;	
				handType = handTypes[1] + " of " + returnFace(pairs[0][0].cardValue) + "s";					
			}						
			//two pair
			if(pairs.length > 1){				
				score = 200;
				handType = handTypes[2] + " " + returnFace(pairs[0][0].cardValue) + "s and " + returnFace(pairs[1][0].cardValue) + "s";;		
			}
			//trips
			if(trips.length > 0){
				trips.sortOn("");
				trips.reverse();
				score = 300;
				handType = handTypes[3] + " of " + returnFace(trips[0][0].cardValue) + "s";					
			}									
			//strait
			for(i=0; i < values.length - 4;i++){					
				if(values[i].length > 0 && values[i+1].length > 0 && values[i+2].length > 0 && values[i+3].length > 0 && values[i+4].length > 0){					
					straits[0] = values[i+4][0];
					straits[1] = values[i+3][0];
					straits[2] = values[i+2][0];
					straits[3] = values[i+1][0];
					straits[4] = values[i][0];
					handType = handTypes[4] + " to the " + returnFace(straits[0].cardValue);
					score = 400;
				}			
			}
			//ace high strait
			if(values[0].length > 0 && values[12].length > 0 && values[11].length > 0 && values[10].length > 0 && values[9].length > 0){					
					straits[0] = values[0][0];
					straits[1] = values[1][0];
					straits[2] = values[2][0];
					straits[3] = values[3][0];
					straits[4] = values[4][0];				
					handType = handTypes[4] + " to the " + returnFace(straits[0].cardValue);
					score = 400;
			}											
			//flush		 
			for(i=0; i < suits.length;i++){
				if(suits[i].length > 4){	
					for(j=0; j < cards.length;j++){
						if(cards[j].suit == cardSuits[i]){
							flushes.push(cards[j]);		
						}
					}
					flushes.sortOn("cardValue" , Array.NUMERIC | Array.DESCENDING);
					score = 500;
					handType =  cardSuits[i] + " " + handTypes[5] + " " + returnFace(flushes[0].cardValue) + " high";
				}	
			}			
			//boat (full house)			
			if(pairs.length > 0 && trips.length > 0){
					score = 600;
					handType = handTypes[6] + " " + returnFace(trips[0][0].cardValue) + "s full of " + returnFace(pairs[0][0].cardValue) + "s";		
			}
			if(trips.length == 2){	
					score = 600;
					handType = handTypes[6] + " " + returnFace(trips[0][0].cardValue) + "s full of " + returnFace(trips[1][0].cardValue) + "s";		
			}							
			//quads (four of a kind)
			if(quads.length > 0){
				quads.sortOn("cardValue");
				quads.reverse();
				score = 700;
				handType = handTypes[7] + " " + returnFace(quads[0][0].cardValue) + "s";
			}			
			//strait flush!
			if(flushes.length >= 5){
				for(i=0; i < flushes.length - 4;i++){
					var tempPlace = flushes[i].cardValue;
					if(score < 800){
						if(    (tempPlace == (flushes[i+1].cardValue+1)) && (tempPlace == (flushes[i+2].cardValue+2)) && (tempPlace == (flushes[i+3].cardValue+3)) && (tempPlace == (flushes[i+4].cardValue+4)) ){					
							handType =  handTypes[8] + " " + returnFace(tempPlace) + " high";
							score = 800;
							bestFive[0] = flushes[i];
							bestFive[1] = flushes[i+1];
							bestFive[2] = flushes[i+2];
							bestFive[3] = flushes[i+3];
							bestFive[4] = flushes[i+4];
						}			
					}
				}				
			}						
			//fives
			if(fives.length > 0){
				score = 900;
				handType = handTypes[9] + " " + returnFace(fives[0][0].cardValue) + "s";
			}																
			//Get the best five cards
			//high card only
			if(score < 100){
				var cardCount = cards.length;
				if(cardCount > 5){cardCount = 5;}
				for(i=0;i<cardCount;i++){				
				bestFive[i] = cards[i];
				cards[i].inBestFive = true;
				}				
			}
			//pair
			if(score == 100){
				bestFive[0] = pairs[0][0].cardValue;
				bestFive[1] = pairs[0][1].cardValue;											
				pairs[0][0].inBestFive = true;
				pairs[0][1].inBestFive = true;				
				bestFive[2] = getKicker();
				bestFive[3] = getKicker();
				bestFive[4] = getKicker();					
			}
			//two pair
			if(score == 200){
				bestFive[0] = pairs[0][0].cardValue;
				bestFive[1] = pairs[0][1].cardValue;				
				bestFive[2] = pairs[1][0].cardValue;				
				bestFive[3] = pairs[1][1].cardValue;													
				pairs[0][0].inBestFive = true;
				pairs[0][1].inBestFive = true;
				pairs[1][0].inBestFive = true;
				pairs[1][1].inBestFive = true;				
				bestFive[4] = getKicker();				
			}		
			//trip
			if(score == 300){
				bestFive[0] = trips[0][0].cardValue;				
				bestFive[1] = trips[0][1].cardValue;				
				bestFive[2] = trips[0][2].cardValue;			
				trips[0][0].inBestFive = true;
				trips[0][1].inBestFive = true;
				trips[0][2].inBestFive = true;				
				bestFive[3] = getKicker();
				bestFive[4] = getKicker();					
			}	
			//strait
			if(score == 400){
				bestFive[0] = straits[0];
				bestFive[1] = straits[1];
				bestFive[2] = straits[2];
				bestFive[3] = straits[3];
				bestFive[4] = straits[4];
				straits[0].inBestFive = true;
				straits[1].inBestFive = true;
				straits[2].inBestFive = true;
				straits[3].inBestFive = true;
				straits[4].inBestFive = true;	
			}
			//flush
			if(score == 500){
				bestFive[0] = flushes[0];
				bestFive[1] = flushes[1];
				bestFive[2] = flushes[2];
				bestFive[3] = flushes[3];
				bestFive[4] = flushes[4];
				for(i=0; i < suits.length;i++){
					if(suits[i].length > 4){						
						suits[i][0].inBestFive = true;
						suits[i][1].inBestFive = true;
						suits[i][2].inBestFive = true;
						suits[i][3].inBestFive = true;
						suits[i][4].inBestFive = true;
					}
				}					
			}
			//boat
			if(score == 600){
				bestFive[0] = trips[0][0].cardValue;
				bestFive[1] = trips[0][1].cardValue;
				bestFive[2] = trips[0][2].cardValue;
				trips[0][0].inBestFive = true;
				trips[0][1].inBestFive = true;
				trips[0][2].inBestFive = true;
				if(trips.length > 1){
				bestFive[3] = trips[1][0].cardValue;
				bestFive[4] = trips[1][1].cardValue;
				trips[1][0].inBestFive = true;
				trips[1][1].inBestFive = true;
				}else{
				bestFive[3] = pairs[0][0].cardValue;
				bestFive[4] = pairs[0][1].cardValue;	
				pairs[0][0].inBestFive = true;
				pairs[0][1].inBestFive = true;
				}
			}
			//quads
			if(score == 700){
				bestFive[0] = quads[0][0].cardValue;				
				bestFive[1] = quads[0][1].cardValue;				
				bestFive[2] = quads[0][2].cardValue;				
				bestFive[3] = quads[0][3].cardValue;				
				quads[0][0].inBestFive = true;
				quads[0][1].inBestFive = true;
				quads[0][2].inBestFive = true;
				quads[0][3].inBestFive = true;
				bestFive[4] = getKicker();				
			}			
			//strait flush
			if(score == 800){
				bestFive[0].inBestFive = true;
				bestFive[1].inBestFive = true;
				bestFive[2].inBestFive = true;
				bestFive[3].inBestFive = true;
				bestFive[4].inBestFive = true;	
			}						
			getHandStats();
		}		
		
		public function getKicker():Number
		{
			var tempHigh:Number = 0;
			for(i=0;i<cards.length;i++)
			{
				if(cards[i].inBestFive == false)
				{
					if(cards[i].cardValue > tempHigh)
					{
						tempHigh = cards[i].cardValue;							
					}
				}												
			}	
			var gotKicker = false;
			for(i=0;i<cards.length;i++)
			{			
				if((cards[i].inBestFive == false)&&(cards[i].cardValue == tempHigh)&&(gotKicker == false))
				{
					cards[i].inBestFive = true;			
					gotKicker = true;
				}			
			}
			return tempHigh;
		}
				
		public function clearHandScore():void
		{								
			score = 0;
			handType ="";
			winningType = false;
			checkmark.visible = false;
			dealToMe.visible = false;			
			pairs.splice(0, pairs.length);
			trips.splice(0, trips.length);
			quads.splice(0, quads.length);
			fives.splice(0, fives.length);
			flushes.splice(0, flushes.length);
			straits.splice(0, straits.length);
			suits.splice(0, suits.length);
			values.splice(0, values.length);
			for(i=0;i<cards.length;i++)
			{
				cards[i].inBestFive = false;
				cards[i].bestMc.visible = false;
				cards[i].bestMc.anim.stop();
			}			
		}					
	}		
}