Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Monopoly
- {
- String[] spaces = {"Mediterranean Avenue", "Community Chest(1)", "Baltic Avenue", "Income Tax", "Reading Railroad",
- "Oriental Avenue", "Chance(1)", "Vermont Avenue", "Connecticut Avenue", "In Jail/Just Visiting", "St. Charles Place",
- "Electric Company", "States Avenue", "Virginia Avenue", "Pennsylvania Railroad", "St. James Place", "Community Chest(2)",
- "Tennessee Avenue", "New York Avenue", "Free Parking", "Kentucky Avenue", "Chance(2)", "Indiana Avenue", "Illinois Avenue",
- "B&O Railroad", "Atlantic Avenue", "Ventnor Avenue", "Water Works", "Marvin Gardens", "Go to Jail", "Pacific Avenue",
- "North Carolina Avenue", "Community Chest(3)", "Pennsylvania Avenue", "Short Line", "Chance(3)", "Park Place", "Luxury Tax",
- "Boardwalk", "Go"};
- final int players = 1;
- final int goAroundTimes = 25;
- float[] percentages = new float[40];
- public float[] analyze()
- {
- int[] visitCount = new int[40];
- for(int i = 0; i < players; i++)
- {
- String position;
- int timesAround = 1;
- Dice dice = new Dice();
- do
- {
- int move = dice.roll2Dice();
- position = spaces[move];
- for(int j = 0; j < 40; j++)
- {
- position = spaces[j];
- visitCount[j] += 1;
- }
- if( position == "Go" )
- {
- timesAround += 1;
- position = spaces[1];
- }
- else if( position == "In Jail/Just Visiting" )
- {
- timesAround += 1;
- position = spaces[10];
- visitCount[10] -= 1;
- }
- }while(timesAround < goAroundTimes);
- }
- for(int i = 0; i < 40; i++)
- {
- int visitTotal = 0;
- visitTotal += visitCount[i];
- percentages[i] = (visitCount[i] / visitTotal) * 100;
- }
- return percentages;
- }
- public void listResults(float[] results)
- {
- for(int i = 0; i < 40; i++)
- {
- System.out.printf("%5d/t5.2f \n", percentages[i] );
- }
- }
- }
- And then
- public class TestGame
- {
- public static void main(String[] args)
- {
- Monopoly game = new Monopoly();
- float[] results = game.analyze();
- game.listResults(results);
- }
- }
- Dice thingy
- import java.util.*;
- public class Dice
- {
- Random rand = new Random();
- public int roll1Die()
- {
- return rand.nextInt(6) + 1;
- }
- public int roll2Dice()
- {
- int die1 = roll1Die();
- int die2 = roll1Die();
- return die1 + die2;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement