• API
• FAQ
• Tools
• Trends
• Archive
daily pastebin goal
36%
SHARE
TWEET

# Untitled

a guest Jan 13th, 2013 26 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. /**
2.  * Context: I'm making a variable length board game. There is a deck of cards
3.  * that tell the players what to do each turn. 5 of the cards are 'Discard
4.  * Energy' cards and after drawing all 5 of these cards the players will have no
5.  * energy left and they lose the game. Depending on the distribution of these
6.  * cards, the game will last a different number of turns. If all 5 cards are at
7.  * the top of the deck, the game will be over in 5 turns. If one of the cards
8.  * finds its way to the bottom of the deck, every turn card will be drawn before
9.  * the game is over. I'm ok with this, as long as 'most' games last a minimum
10.  * number of turns. I think 15 turns is a good length of time. So my question is
11.  * this: how many non-'discard energy' cards should I include in the deck? Is
12.  * there an equation that will tell me the percentage of games that last at
13.  * least 15 rounds given the total number of cards? What about an equation that
14.  * tells me the percentage of games that will be shorter than 10 games?
15.  * Alternatively, what if I said I wanted 90% of the game to last 15 turns or
16.  * longer. How many cards would I need? Thanks!
17.  *
19.  * probability_question_how_long_will_my_game_last/
20.  *
21.  * Goal:
22.  *
23.  * Make a program that simulates drawing cards until a target number of special
24.  * cards are hit and then prints out the the probability and cumumaltive
25.  * probability of each number of turns
26.  * */
27.
28. public class HowLongWillMyGameLast {
29.
30.         public static void main(String[] args) {
31.                 /*
32.                  * The use of doubles for integer values is to avoid having to recast to
33.                  * skip integer division
34.                  */
35.
36.                 // control variables
37.                 double totalNumberOfCards = 21;
38.                 double numberOfEnergyCards = 5;
39.                 int numberOfTrials = 1_000_000;
40.                 boolean showCumulative = true;
41.
42.                 // data
43.                 /* index represents finishing turn, value how many times finished there */
44.                 double[] numberFinishedOnTurnN = new double[(int) totalNumberOfCards + 1];
45.
46.                 for (int i = 1; i <= numberOfTrials; i++) {
47.                         double deck = totalNumberOfCards;
48.                         double energyCards = numberOfEnergyCards;
49.                         int turn = 0;
50.                         while (energyCards > 0) {
51.                                 if (Math.random() >= 1.0 - (energyCards / deck)) {
52.                                         energyCards--;
53.                                         deck--;
54.                                 } else {
55.                                         deck--;
56.                                 }
57.                                 turn++;
58.                                 if (deck < 0) { // check for infinite loops
59.                                         System.out
60.                                                         .println("drawing cards from a non-existant deck :-(");
61.                                         System.exit(1);
62.                                 }
63.                         }
64.                         numberFinishedOnTurnN[turn] += 1;
65.                 } // end of simulations
66.
67.                 // calculate average number of turns
68.                 Double averageNumberOfTurns = 0.0;
69.                 for (int i = 0; i < numberFinishedOnTurnN.length; i++){
70.                         averageNumberOfTurns += i*(numberFinishedOnTurnN[i]/numberOfTrials);
71.                 }
72.
73.                 // printouts
74.
75.
76.                 System.out.println("There were " + (int) totalNumberOfCards + " in the deck, of which " + (int) numberOfEnergyCards + " were energy cards. " + numberOfTrials + " trials were run." );
77.                 System.out.println("On average the games lasted " + averageNumberOfTurns.toString().substring(0,5) + " turns");
78.                 if(showCumulative){
79.                         double cumulativeFinishes = 0;
80.                         for (int i = numberFinishedOnTurnN.length-1; i >= (int) numberOfEnergyCards ; i--){
81.                                 cumulativeFinishes += numberFinishedOnTurnN[i];
82.                                 System.out.println((int)(cumulativeFinishes/numberOfTrials * 100) + "%" + " of the games finished on turn " + i + " or later");
83.                         }
84.
85.                 } else {
86.                         for (int i = (int) numberOfEnergyCards; i < numberFinishedOnTurnN.length; i++){
87.                                 if (numberFinishedOnTurnN[i] > 0.0){
88.                                         System.out.println((int)(numberFinishedOnTurnN[i]/numberOfTrials * 100) + "%" + " of the games finished on turn " + i);
89.                                 }
90.                         }
91.                 }
92.
93.
94.
95.
96.         }
97. }
RAW Paste Data
Top