frankkarsten

Landfall simulation

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