Advertisement
Guest User

Untitled

a guest
Sep 11th, 2019
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.75 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.HashMap;
  3. import java.util.List;
  4. import java.util.Map;
  5. import java.util.Random;
  6.  
  7. /**
  8.  * This class runs a simple contest that determines a winner at the end.
  9.  * Unfortunately, it is riddled with bugs and crashes, and some players have noticed
  10.  * the winning distribution does not match the game rules, and in the future might game the system!
  11.  *
  12.  * Your job is to get a fair contest running smoothly!
  13.  */
  14. public class UnfairContest {
  15.     private static final Random PRNG = new Random();
  16.     private final Map<Contestant, int> registry = new HashMap<>();
  17.     private final List<Contestant> drawOrder = new ArrayList<>();
  18.     private boolean eliminateOdd = true;
  19.  
  20.     public static class Contestant {
  21.         private final String name;
  22.         private final int age;
  23.  
  24.         public Contestant(String name, int age) {
  25.             this.name = name;
  26.             this.age = age;
  27.         }
  28.  
  29.         public String getName() {
  30.             return name;
  31.         }
  32.  
  33.         public int getAge() {
  34.             return age;
  35.         }
  36.  
  37.         @Override
  38.         public boolean equals(Object obj) {
  39.             if (this == obj)
  40.                 return true;
  41.             if (obj == null)
  42.                 return false;
  43.             Contestant other = (Contestant) obj;
  44.             if (age != other.age)
  45.                 return false;
  46.             if (name != other.name)
  47.                 return false;
  48.             return true;
  49.         }
  50.     }
  51.  
  52.     public static class PayToWinContestant extends Contestant {
  53.         private int lives;
  54.  
  55.         public PayToWinContestant(String name, int age, int lives) {
  56.             super(name, age);
  57.             this.lives = lives;
  58.         }
  59.  
  60.         public boolean removeLife() {
  61.             return --lives <= 0;
  62.         }
  63.  
  64.         @Override
  65.         public boolean equals(Object obj) {
  66.             if (this == obj)
  67.                 return true;
  68.             if (!super.equals(obj))
  69.                 return false;
  70.             PayToWinContestant other = (PayToWinContestant) obj;
  71.             if (lives != other.lives)
  72.                 return false;
  73.             return true;
  74.         }
  75.     }
  76.  
  77.     /**
  78.      * Register a Contestant to participate in the contest.
  79.      * A Contestant is unique by their name and age.
  80.      * Contestants get a token valued from [-10,10] that is part of the game rules.
  81.      * All tokens have equal probability of winning.
  82.      *
  83.      * @param contestant
  84.      */
  85.     private void register(Contestant contestant) {
  86.         registry.put(contestant, PRNG.nextInt(21) - 10);
  87.         drawOrder.add(contestant);
  88.     }
  89.  
  90.     /**
  91.      * Run a contest with the registered contestants.
  92.      *
  93.      * A contestant is randomly chosen from the group.
  94.      * If their token value matches eliminateOdd parity
  95.      * (starts as odd numbers, then flips to even numbers and back over time) then they are eliminated.
  96.      *
  97.      * PayToWinContestant's lose a life when they are selected to be eliminated, and are only
  98.      * actually eliminated when they have no lives remaining.
  99.      *
  100.      * The contest ends when there is only one contestant remaining.
  101.      *
  102.      * @return Returns the winner!
  103.      */
  104.     private Contestant computeWinner() {
  105.         if (registry.isEmpty()) {
  106.             throw new IllegalStateException("Cannot run a contest with no registered contestants!");
  107.         }
  108.         int index;
  109.         int time = 0;
  110.         while (registry.size() != 1) {
  111.             if (time++ % drawOrder.size() == 0) {
  112.                 eliminateOdd = !eliminateOdd;
  113.             }
  114.             index = PRNG.nextInt(drawOrder.size());
  115.             determineFate(drawOrder.get(index));
  116.         }
  117.         return registry.keySet().iterator().next();
  118.     }
  119.  
  120.     private void determineFate(Contestant contestant) {
  121.         int token = registry.get(contestant);
  122.         boolean isOdd = token % 2 == 1;
  123.         if (!eliminateOdd) {
  124.             isOdd = !isOdd;
  125.         }
  126.         if (isOdd) {
  127.             boolean eliminate = true;
  128.             if (contestant instanceof PayToWinContestant) {
  129.                 eliminate = ((PayToWinContestant) contestant).removeLife();
  130.             }
  131.             if (eliminate) {
  132.                 registry.remove(contestant);
  133.             }
  134.         }
  135.     }
  136.  
  137.     public static void main(String[] args) {
  138.         UnfairContest contest = new UnfairContest();
  139.         contest.register(new Contestant("Alice", 21));
  140.         contest.register(new Contestant("Bob", 21));
  141.         contest.register(new PayToWinContestant("Charlie", 12, 2));
  142.         contest.register(new Contestant("Dan", 65));
  143.         contest.register(new PayToWinContestant("Eve", 44, 3));
  144.  
  145.         System.out.format("And the winner is... %s!\n", contest.computeWinner().getName());
  146.     }
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement