frankkarsten

Goblin Dark-Dwellers simulation

Jan 18th, 2016
94
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package goblindarkdwellersim;
  2.  
  3. import java.util.Arrays.*;
  4. import java.util.Random;
  5.  
  6. public class GoblinDarkDwellerSim {
  7.  
  8.     public static void main(String[] args) {
  9.        
  10.         Deck deck=new Deck();
  11.            
  12.         //CardType 1 is a land
  13.         //CardType 2 is another creature
  14.         //CardType 3 is a Goblin Dark-Dweller
  15.         //CardType 5 is a spell that can be flashed back
  16.        
  17.        
  18.         for (int NrSpells=0; NrSpells<=30; NrSpells++){
  19.             deck.SetDeck(26,30-NrSpells,4,0,NrSpells);
  20.             double Probability=ProbabilityForRandomHand(deck,7,1000000);
  21.             System.out.print("Probability of being able to flash back a card in games where you play a turn-5 Goblin Dark-Dwellers in a deck with "+NrSpells+" spells: ");
  22.             System.out.println(Math.round(Probability*10000)/100.0);
  23.         }
  24.                
  25.     }//end of main
  26.  
  27.     public static double ProbabilityForRandomHand(Deck deck, int StartingCards, int NumberOfIterations){
  28.         Deck remainingdeck=new Deck();
  29.         double NumberOfGamesWithfavorableOutcome=0;
  30.         double NumberOfGamesWithTurn5GDD=0;
  31.         for (int IterationCounter=1; IterationCounter<=NumberOfIterations; IterationCounter++){
  32.             OpeningHand openinghand=GiveOpeningHandAfterMulls(deck, StartingCards);
  33.             remainingdeck.SetDeck(deck.NumberOfLands-openinghand.NumberOfLands,deck.NumberOfCardType2-openinghand.NumberOfCardType2,deck.NumberOfCardType3-openinghand.NumberOfCardType3,deck.NumberOfCardType4-openinghand.NumberOfCardType4,deck.NumberOfSpells-openinghand.NumberOfSpells);
  34.             int Outcome=SimulateGame(remainingdeck,openinghand);
  35.             if (Outcome==0) {NumberOfGamesWithfavorableOutcome++; NumberOfGamesWithTurn5GDD++;}
  36.             if (Outcome==1) {NumberOfGamesWithTurn5GDD++;}
  37.         }
  38.         return NumberOfGamesWithfavorableOutcome/(NumberOfGamesWithTurn5GDD+0.0);
  39.     }//end of AverageKillTurnForRandomHand
  40.    
  41.     static OpeningHand GiveOpeningHandAfterMulls (Deck deck, int StartingCards) {
  42.        
  43.         Deck remainingdeck=new Deck();
  44.         OpeningHand openinghand=new OpeningHand();
  45.         int TypeOfCardDrawn;
  46.         boolean KeepHand=false;
  47.        
  48.         for (int OpeningHandSize=7; OpeningHandSize>=1; OpeningHandSize--){
  49.             if (KeepHand==false){
  50.                 openinghand.SetHand(0,0,0,0,0);
  51.                 remainingdeck.SetDeck(deck.NumberOfLands,deck.NumberOfCardType2,deck.NumberOfCardType3,deck.NumberOfCardType4,deck.NumberOfSpells);
  52.                 int NumberOfLands=0;
  53.                 for (int CardsDrawn=0; CardsDrawn<OpeningHandSize; CardsDrawn++){
  54.                     TypeOfCardDrawn=remainingdeck.DrawCard();
  55.                     if (TypeOfCardDrawn==1) {openinghand.NumberOfLands++; NumberOfLands++;}
  56.                     if (TypeOfCardDrawn==2) {openinghand.NumberOfCardType2++;}
  57.                     if (TypeOfCardDrawn==3) {openinghand.NumberOfCardType3++;}
  58.                     if (TypeOfCardDrawn==4) {openinghand.NumberOfCardType4++;}
  59.                     if (TypeOfCardDrawn==5) {openinghand.NumberOfSpells++;}
  60.                 }
  61.                 KeepHand=true;
  62.                 if (OpeningHandSize>1) {
  63.                     // We mulligan a hand if it contains 0, 1, 6, or 7 lands
  64.                     if (NumberOfLands<=1 || NumberOfLands>=6) {KeepHand=false;}
  65.                     //
  66.                 }
  67.             }
  68.         }
  69.        
  70.         return openinghand;
  71.     }//end of GiveOpeningHandAfterMulls
  72.    
  73.    
  74.     static int SimulateGame(Deck remainingdeck, OpeningHand openinghand) {
  75.        
  76.         //Initializing
  77.         int TypeOfCardDrawn=0;
  78.         int Outcome=2;
  79.         boolean SpellInGraveyard=false;
  80.  
  81.         //We are on the draw with probability 0.5
  82.         boolean WeAreOnTheDraw=false;
  83.         Random generator = new Random();
  84.         if (generator.nextDouble()>=0.5) {WeAreOnTheDraw=true;}
  85.        
  86.         int LandsInHand=openinghand.NumberOfLands;
  87.         int SpellsInHand=openinghand.NumberOfSpells;
  88.         int GDDInHand=openinghand.NumberOfCardType3;
  89.         int LandsInPlay=0;
  90.        
  91.         for (int Turn=1; Turn<=5; Turn++){
  92.            
  93.             if (Turn>1 || (Turn==1 && WeAreOnTheDraw)) {
  94.                 TypeOfCardDrawn=remainingdeck.DrawCard();
  95.                 if (TypeOfCardDrawn==1) {LandsInHand++;}
  96.                 if (TypeOfCardDrawn==3) {GDDInHand++;}
  97.                 if (TypeOfCardDrawn==5) {SpellsInHand++;}
  98.             }
  99.  
  100.             if (LandsInHand>=1) {LandsInHand--; LandsInPlay++;}
  101.             if (LandsInPlay==4 & SpellsInHand>=1) {SpellInGraveyard=true;}
  102.             //On turn 5, we need to play GDD if possible--no mana for a spell. So we essentially only have to check if we can cast a spell on turn 4.
  103.         }
  104.        
  105.         if (GDDInHand>=1 && LandsInPlay==5 && SpellInGraveyard) {Outcome=0;}
  106.         if (GDDInHand>=1 && LandsInPlay==5 && !SpellInGraveyard) {Outcome=1;}
  107.        
  108.         return Outcome;
  109.        
  110.     }//end of SimulateGame
  111.    
  112. }
  113.  
  114. class OpeningHand {
  115.     int NumberOfLands;
  116.     int NumberOfCardType2;
  117.     int NumberOfCardType3;
  118.     int NumberOfCardType4;
  119.     int NumberOfSpells;
  120.  
  121.     int NrOfCards(){
  122.         return NumberOfLands+NumberOfCardType2+NumberOfCardType3+NumberOfCardType4+NumberOfSpells;
  123.     }
  124.  
  125.     void SetHand (int Nr1, int Nr2, int Nr3, int Nr4, int Nr5) {
  126.         NumberOfLands=Nr1;
  127.         NumberOfCardType2=Nr2;
  128.         NumberOfCardType3=Nr3;
  129.         NumberOfCardType4=Nr4;
  130.         NumberOfSpells=Nr5;
  131.     }
  132.  
  133. }//end of OpeningHand
  134.  
  135. class Deck {
  136.     int NumberOfLands;
  137.     int NumberOfCardType2;
  138.     int NumberOfCardType3;
  139.     int NumberOfCardType4;
  140.     int NumberOfSpells;
  141.    
  142.     void SetDeck (int Nr1, int Nr2, int Nr3, int Nr4, int Nr5) {
  143.         NumberOfLands=Nr1;
  144.         NumberOfCardType2=Nr2;
  145.         NumberOfCardType3=Nr3;
  146.         NumberOfCardType4=Nr4;
  147.         NumberOfSpells=Nr5;
  148.     }
  149.    
  150.     int NrOfCards(){
  151.         return NumberOfLands+NumberOfCardType2+NumberOfCardType3+NumberOfCardType4+NumberOfSpells;
  152.     }
  153.    
  154.     int DrawCard (){
  155.         Random generator = new Random();
  156.         int CardType=0;
  157.         int RandomIntegerBetweenOneAndDeckSize=generator.nextInt( this.NrOfCards() )+1;
  158.         int OneCutoff=NumberOfLands;
  159.         int TwoCutoff=OneCutoff+NumberOfCardType2;
  160.         int ThreeCutoff=TwoCutoff+NumberOfCardType3;
  161.         int FourCutoff=ThreeCutoff+NumberOfCardType4;
  162.         int FiveCutoff=FourCutoff+NumberOfSpells;
  163.  
  164.         if (RandomIntegerBetweenOneAndDeckSize<=OneCutoff) {CardType=1; this.NumberOfLands--;}
  165.         if (RandomIntegerBetweenOneAndDeckSize>OneCutoff && RandomIntegerBetweenOneAndDeckSize<=TwoCutoff) {CardType=2; this.NumberOfCardType2--;}
  166.         if (RandomIntegerBetweenOneAndDeckSize>TwoCutoff && RandomIntegerBetweenOneAndDeckSize<=ThreeCutoff) {CardType=3; this.NumberOfCardType3--;}
  167.         if (RandomIntegerBetweenOneAndDeckSize>ThreeCutoff && RandomIntegerBetweenOneAndDeckSize<=FourCutoff) {CardType=4; this.NumberOfCardType4--;}
  168.         if (RandomIntegerBetweenOneAndDeckSize>FourCutoff && RandomIntegerBetweenOneAndDeckSize<=FiveCutoff) {CardType=5; this.NumberOfSpells--;}
  169.         return CardType;
  170.     }
  171.    
  172. }//end of Deck
RAW Paste Data