package
{

	
	/**
	 * ...
	 * @author (t)ad
	 */
	public class cardShute
	{
		
		/* This class controls which cards get dealt.
		 * It is the card shute.
		 *
		 *  By default it just has 3, 52 card decks in it.
		 *  Changing the middle number of the arrays changes the amount of each card in the shute.
		 *
		 * */
		
		public static var suitOfLastCardPulled:String = "c";

		public static var theCards:Array;
		
		public static var cardsInShoot:Object = {
				
				cA:["A",3,"c"],
				dA:["A",3,"d"],
				hA:["A",3,"h"],
				sA:["A",3,"s"],
				
				c2:["2",3,"c"],
				d2:["2",3,"d"],
				h2:["2",3,"h"],
				s2:["2",3,"s"],
				
				c3:["3",3,"c"],
				d3:["3",3,"d"],
				h3:["3",3,"h"],
				s3:["3",3,"s"],
				
				c4:["4",3,"c"],
				d4:["4",3,"d"],
				h4:["4",3,"h"],
				s4:["4",3,"s"],
				
				c5:["5",3,"c"],
				d5:["5",3,"d"],
				h5:["5",3,"h"],
				s5:["5",3,"s"],
				
				c6:["6",3,"c"],
				d6:["6",3,"d"],
				h6:["6",3,"h"],
				s6:["6",3,"s"],
				
				c7:["7",3,"c"],
				d7:["7",3,"d"],
				h7:["7",3,"h"],
				s7:["7",3,"s"],
				
				c8:["8",3,"c"],
				d8:["8",3,"d"],
				h8:["8",3,"h"],
				s8:["8",3,"s"],
				
				c9:["9",3,"c"],
				d9:["9",3,"d"],
				h9:["9",3,"h"],
				s9:["9",3,"s"],
				
				c10:["10",3,"c"],
				d10:["10",3,"d"],
				h10:["10",3,"h"],
				s10:["10",3,"s"],
				
				cK:["K",3,"c"],
				dK:["K",3,"d"],
				hK:["K",3,"h"],
				sK:["K",3,"s"],
				
				cQ:["Q",3,"c"],
				dQ:["Q",3,"d"],
				hQ:["Q",3,"h"],
				sQ:["Q",3,"s"],
				
				cJ:["J",3,"c"],
				dJ:["J",3,"d"],
				hJ:["J",3,"h"],
				sJ:["J",3,"s"]
				
		};
	
		public static function shuffle():void {

			
			var cardChoices:Array = ["A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "K", "Q", "J"];
			var suChoices:Array = ["c", "d", "h", "s"];
			var totalCards:Number = 0;

			var allNumsIn:Array = [];

			for (var j:String in cardsInShoot) {

				allNumsIn.push([j, cardsInShoot[j][1]]);
			}

			for (var i:String in cardsInShoot) {

				totalCards += cardsInShoot[i][1];

			}
			theCards = new Array(totalCards);
			var ranNum:int;
			var ranSu:int;

			for (var a:int = 0; a < totalCards; a ++) {
				ranNum = Math.floor(Math.random() * 13);
				ranSu = Math.floor(Math.random() * 4);

				if (cardsInShoot[suChoices[ranSu] + "" + cardChoices[ranNum]][1] >= 1) {

					cardsInShoot[suChoices[ranSu] + "" + cardChoices[ranNum]][1] -= 1;
					theCards.push(cardsInShoot[suChoices[ranSu] + "" + cardChoices[ranNum]]);

				} else {
					ranNum = Math.floor(Math.random() * 13);
					ranSu = Math.floor(Math.random() * 4);

					if (cardsInShoot[suChoices[ranSu] + "" + cardChoices[ranNum]][1] >= 1) {

						cardsInShoot[suChoices[ranSu] + "" + cardChoices[ranNum]][1] -= 1;
						theCards.push(cardsInShoot[suChoices[ranSu] + "" + cardChoices[ranNum]]);

					} else {
						for (var ac:String in cardsInShoot) {

							if (cardsInShoot[ac][1] >= 1) {

								theCards.splice( Math.floor(Math.random() * totalCards), 0, cardsInShoot[ac] );
								cardsInShoot[ac][1] -= 1;
								break;
							}
						}
					}

				}
			}
			
			for (var ai:int = 0; ai < allNumsIn.length; ai++) {

				cardsInShoot[allNumsIn[ai][0]][1] = allNumsIn[ai][1];
			}

		}
		

		public static function retrieveCard():String {
			
			var cardSpec:Array;
			var out:String;
			
			
			var reset:int = 0;
			for (var aic:String in cardsInShoot) {
				if (cardsInShoot[aic][1] <= 0) reset++;
			}

			if (reset == 52) resetCards();

			shuffle();

			var len:int = theCards.length;

			for ( var i:int = 0; i < len; i ++) {
				if (theCards[i] != null && theCards[i][1] >= 1) {
					cardSpec = theCards[i];break;
				}
			}
				
			out = cardSpec[0];
			suitOfLastCardPulled = cardSpec[2];
			cardsInShoot[suitOfLastCardPulled + "" + out][1] -= 1;
			
			
			return out;
			
		}
		
		
		
		public static function resetCards():void {
			
			
			cardsInShoot = {
				
				cA:["A",3,"c"],
				dA:["A",3,"d"],
				hA:["A",3,"h"],
				sA:["A",3,"s"],
				
				c2:["2",3,"c"],
				d2:["2",3,"d"],
				h2:["2",3,"h"],
				s2:["2",3,"s"],
				
				c3:["3",3,"c"],
				d3:["3",3,"d"],
				h3:["3",3,"h"],
				s3:["3",3,"s"],
				
				c4:["4",3,"c"],
				d4:["4",3,"d"],
				h4:["4",3,"h"],
				s4:["4",3,"s"],
				
				c5:["5",3,"c"],
				d5:["5",3,"d"],
				h5:["5",3,"h"],
				s5:["5",3,"s"],
				
				c6:["6",3,"c"],
				d6:["6",3,"d"],
				h6:["6",3,"h"],
				s6:["6",3,"s"],
				
				c7:["7",3,"c"],
				d7:["7",3,"d"],
				h7:["7",3,"h"],
				s7:["7",3,"s"],
				
				c8:["8",3,"c"],
				d8:["8",3,"d"],
				h8:["8",3,"h"],
				s8:["8",3,"s"],
				
				c9:["9",3,"c"],
				d9:["9",3,"d"],
				h9:["9",3,"h"],
				s9:["9",3,"s"],
				
				c10:["10",3,"c"],
				d10:["10",3,"d"],
				h10:["10",3,"h"],
				s10:["10",3,"s"],
				
				cK:["K",3,"c"],
				dK:["K",3,"d"],
				hK:["K",3,"h"],
				sK:["K",3,"s"],
				
				cQ:["Q",3,"c"],
				dQ:["Q",3,"d"],
				hQ:["Q",3,"h"],
				sQ:["Q",3,"s"],
				
				cJ:["J",3,"c"],
				dJ:["J",3,"d"],
				hJ:["J",3,"h"],
				sJ:["J",3,"s"]
				
			};
			
		}
		
	}
	
}