Advertisement
Arxkanite

Hearthstone Collection Simulation Code

Aug 24th, 2013
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package
  2. {
  3.     import flash.display.Sprite;
  4.     import flash.events.Event;
  5.     import flash.text.TextField;
  6.    
  7.     // Based off calculations performed by Hamlet
  8.    
  9.     public class Simulation
  10.     {
  11.         // GLOBAL VARIABLES //
  12.         // THESE VARIABLES CHANGE THE WHOLE SIMULATION //
  13.        
  14.         // Static Strings prevent typos and make my job easier.
  15.         private static var COMMON:String = "COMMON";
  16.         private static var RARE:String = "RARE";
  17.         private static var EPIC:String = "EPIC";
  18.         private static var LEGENDARY:String = "LEGENDARY";
  19.         private static var GOLDEN:String = "GOLDEN";
  20.         private static var STANDARD:String = "STANDARD";
  21.        
  22.         // The number of cards of each rarity that are available to collect.
  23.         private static var availableCards:Object = {
  24.             "COMMON"    :   94,
  25.             "RARE"      :   81,
  26.             "EPIC"      :   37,
  27.             "LEGENDARY" :   33
  28.         };
  29.        
  30.         // The number of items of each card required to complete that item. Based on rarity.
  31.         private static var collectionLimitbyRarity:Object = {
  32.             "COMMON"    :   2,
  33.             "RARE"      :   2,
  34.             "EPIC"      :   2,
  35.             "LEGENDARY" :   1
  36.         }
  37.        
  38.         // The amount of dust earned by disenchanting an card.
  39.         private static var disenchantValue:Object = {
  40.             "COMMON": {
  41.                 "STANDARD"  :   5,
  42.                 "GOLDEN"    :   50
  43.             },
  44.             "RARE": {
  45.                 "STANDARD"  :   20,
  46.                 "GOLDEN"    :   100
  47.             },
  48.             "EPIC": {
  49.                 "STANDARD"  :   100,
  50.                 "GOLDEN"    :   400
  51.             },
  52.             "LEGENDARY": {
  53.                 "STANDARD"  :   400,
  54.                 "GOLDEN"    :   1600
  55.             }
  56.         };
  57.        
  58.         // The amount of dust required to craft a card.
  59.         private static var craftValue:Object = {
  60.             "COMMON": {
  61.                 "STANDARD"  :   40,
  62.                 "GOLDEN"    :   400
  63.             },
  64.             "RARE": {
  65.                 "STANDARD"  :   100,
  66.                 "GOLDEN"    :   800
  67.             },
  68.             "EPIC": {
  69.                 "STANDARD"  :   400,
  70.                 "GOLDEN"    :   1600
  71.             },
  72.             "LEGENDARY": {
  73.                 "STANDARD"  :   1600,
  74.                 "GOLDEN"    :   3200
  75.             }
  76.         };
  77.        
  78.         // Rarity Distribution Multiplied by 1000 for easier calculations. Example: 730 = 0.73 or 73%
  79.         private static var rarityDistribution:Object = {
  80.             "COMMON"    :   730,
  81.             "RARE"      :   210,
  82.             "EPIC"      :   50,
  83.             "LEGENDARY" :   10,
  84.             "GOLDEN"    :   25
  85.         };
  86.        
  87.         // List of rarities
  88.         private static var rarities:Array = new Array(COMMON, RARE, EPIC, LEGENDARY);
  89.        
  90.         // SIMULATOR VARIABLES //
  91.         // THESE VARIABLES ARE USED BY THE SIMULATOR TO TRACK PROGRESS. DO NOT CHANGE THEM. //
  92.        
  93.         // Create the collection "Array"
  94.         private var collection:Object = {
  95.             "COMMON": {
  96.                 "STANDARD"  :   {},
  97.                 "GOLDEN"    :   {}
  98.             },
  99.             "RARE": {
  100.                 "STANDARD"  :   {},
  101.                 "GOLDEN"    :   {}
  102.             },
  103.             "EPIC": {
  104.                 "STANDARD"  :   {},
  105.                 "GOLDEN"    :   {}
  106.             },
  107.             "LEGENDARY": {
  108.                 "STANDARD"  :   {},
  109.                 "GOLDEN"    :   {}
  110.             }
  111.         };
  112.        
  113.         // The amount of dust currently collected
  114.         public var dust:int = 0;
  115.        
  116.         // Number of Cards available in the collection
  117.         public var completeCollectionCardCount:int = 0;
  118.        
  119.         // The number of packs opened.
  120.         public var packsOpened:int = 0;
  121.        
  122.         // This is a calculated amount of dust required to complete the collection after each pack is opened.
  123.         public var dustRequiredToCompleteCollection:int = 0;
  124.        
  125.         // The nubmer of cards that required disenchantment.
  126.         public var cardsDisenchanted:int = 0;
  127.        
  128.         // The number of cards collected so far.
  129.         public var cardsCollected:int = 0;
  130.  
  131.         public function Simulation():void
  132.         {  
  133.             /*
  134.              * This is the plan.
  135.              *
  136.              * 1. Open packs, and track collected cards. As soon as a card exceeds the collection limit (2 for most cards, 1 for LEGENDARY) that card will be disenchanted.
  137.              * 2. After each pack is opened a check will be made to determine if enough dust is available to complete the collection.
  138.              * 3. Output the results
  139.              *
  140.              * Note: a "RARE or better" card is "guaranteed" in each pack. This complicates things. However, the percentages given for rarities are based on opening a huge number of packs. Since this is what this simulation is attempting, theoretically we do not need to account for the rarity guarantee.
  141.              */
  142.            
  143.             // Build the Collection and set all card quantity to 0;
  144.             buildCollectionArray();
  145.            
  146.             // How many cards do we need to collected?
  147.             calculateCardCount();
  148.            
  149.             // Let's Calculate the total dust remaining.
  150.             calculateDustRequired();
  151.            
  152.             // Now let's run the simulation
  153.             simulation();
  154.         }
  155.        
  156.         private function simulation():void
  157.         {
  158.             while ( dust < dustRequiredToCompleteCollection )
  159.                 openAPack();
  160.                
  161.             // Done!
  162.         }
  163.        
  164.         private function openAPack():void
  165.         {
  166.             // Record that a pack was opened!
  167.             packsOpened++;
  168.            
  169.             for ( var cardNumber:int = 0; cardNumber < 5; cardNumber ++ )
  170.             {
  171.                 // Geat a new random card.
  172.                 var card:Object = generateRandomCard();
  173.                 //trace("Opened a Card!", card.rarity, card.version, card.cardNumber);
  174.                
  175.                 // Check the existing quantity of this card.
  176.                 var existingQuantity:int = collection[card.rarity][card.version][card.cardNumber];
  177.                
  178.                 // Do we already have the maximum of this card?
  179.                 if ( existingQuantity == collectionLimitbyRarity[card.rarity] )
  180.                 {
  181.                     // Disenchant it!
  182.                     dust += disenchantValue[card.rarity][card.version];
  183.                     cardsDisenchanted++;
  184.                 }
  185.                 else
  186.                 {
  187.                     // Add the card to the collection
  188.                     collection[card.rarity][card.version][card.cardNumber]++;
  189.                     cardsCollected++;
  190.                    
  191.                     // Subtract it's craft value from the dust required to complete the collection
  192.                     dustRequiredToCompleteCollection -= craftValue[card.rarity][card.version];
  193.                 }
  194.             }
  195.         }
  196.        
  197.         private function generateRandomCard():Object
  198.         {
  199.             // First let's determine the rarity.
  200.             var rarity:String = determineRarity();
  201.            
  202.             // Is the card Golden or standard
  203.             var version:String = randomNumber(0, 999) < rarityDistribution[GOLDEN] ? GOLDEN : STANDARD;
  204.            
  205.             // Now generate a card number
  206.             var cardNumber:int = randomNumber(1, availableCards[rarity]);
  207.            
  208.             // Return the card info
  209.             return {
  210.                 "rarity"        :   rarity,
  211.                 "version"       :   version,
  212.                 "cardNumber"    :   cardNumber
  213.             };
  214.         }
  215.        
  216.         private function determineRarity():String
  217.         {
  218.             var rarityTarget:int = randomNumber(0, 999);
  219.            
  220.             if ( rarityTarget < rarityDistribution[COMMON] )
  221.                 return COMMON;
  222.             else if ( rarityTarget < rarityDistribution[COMMON] + rarityDistribution[RARE] )
  223.                 return RARE;
  224.             else if ( rarityTarget < rarityDistribution[COMMON] + rarityDistribution[RARE] + rarityDistribution[EPIC] )
  225.                 return EPIC;
  226.             else
  227.                 return LEGENDARY;
  228.         }
  229.        
  230.         private function randomNumber(min:int, max:int):int
  231.         {
  232.             return Math.floor(Math.random() * (1 + max - min)) + min;
  233.         }
  234.        
  235.         private function buildCollectionArray():void
  236.         {
  237.             for ( var rarity:String in availableCards )
  238.             {
  239.                 buildCollectionSubArray(collection[rarity], availableCards[rarity]);
  240.             }
  241.         }
  242.        
  243.         private function buildCollectionSubArray(subCollection:Object, quantity:int):void
  244.         {
  245.             for ( var i:int = 1; i <= quantity; i++ )
  246.             {
  247.                 subCollection.STANDARD[i] = 0;
  248.                 subCollection.GOLDEN[i] = 0;
  249.             }
  250.         }
  251.        
  252.         private function calculateDustRequired():void
  253.         {
  254.             for ( var rarity:String in availableCards )
  255.             {
  256.                 // Cost to craft Standard Cards
  257.                 dustRequiredToCompleteCollection += availableCards[rarity] * craftValue[rarity][STANDARD] * collectionLimitbyRarity[rarity];
  258.                
  259.                 // Cost to craft Golden Cards
  260.                 dustRequiredToCompleteCollection += availableCards[rarity] * craftValue[rarity][GOLDEN] * collectionLimitbyRarity[rarity];
  261.             }
  262.         }
  263.        
  264.         private function calculateCardCount():void
  265.         {
  266.             for ( var rarity:String in availableCards )
  267.             {
  268.                 // Multiplied by 2 to include gold and standard cards.
  269.                 completeCollectionCardCount += availableCards[rarity] * collectionLimitbyRarity[rarity] * 2;
  270.                
  271.             }
  272.         }
  273.     }
  274.    
  275. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement