SHARE
TWEET

Untitled

a guest Sep 11th, 2019 95 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.maluuba.debugging;
  2. import java.util.ArrayList;
  3. import java.util.HashMap;
  4. import java.util.List;
  5. import java.util.Map;
  6. import java.util.Random;
  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, Integer> registry = new HashMap<>();
  17.     private final List<Contestant> drawOrder = new ArrayList<>();
  18.     private boolean eliminateOdd = true;
  19.     public static class Contestant {
  20.         private final String name;
  21.         private final int age;
  22.         public Contestant(String name, int age) {
  23.             this.name = name;
  24.             this.age = age;
  25.         }
  26.         public String getName() {
  27.             return name;
  28.         }
  29.         public int getAge() {
  30.             return age;
  31.         }
  32.         @Override
  33.         public boolean equals(Object obj) {
  34.             if (this == obj)
  35.                 return true;
  36.             if (obj == null)
  37.                 return false;
  38.             Contestant other = (Contestant) obj;
  39.             if (age != other.age)
  40.                 return false;
  41.             if (name != other.name)
  42.                 return false;
  43.             return true;
  44.         }
  45.     }
  46.     public static class PayToWinContestant extends Contestant {
  47.         private int lives;
  48.         public PayToWinContestant(String name, int age, int lives) {
  49.             super(name, age);
  50.             this.lives = lives;
  51.         }
  52.         public boolean removeLife() {
  53.             return --lives <= 0;
  54.         }
  55.         @Override
  56.         public boolean equals(Object obj) {
  57.             if (this == obj)
  58.                 return true;
  59.             if (!super.equals(obj))
  60.                 return false;
  61.             PayToWinContestant other = (PayToWinContestant) obj;
  62.             if (lives != other.lives)
  63.                 return false;
  64.             return true;
  65.         }
  66.     }
  67.     /**
  68.      * Register a Contestant to participate in the contest.
  69.      * A Contestant is unique by their name and age.
  70.      * Contestants get a token valued from [-10,10] that is part of the game rules.
  71.      * All tokens have equal probability of winning.
  72.      *
  73.      * @param contestant
  74.      */
  75.     private void register(Contestant contestant) {
  76.         registry.put(contestant, PRNG.nextInt(21) - 10);
  77.         drawOrder.add(contestant);
  78.     }
  79.     /**
  80.      * Run a contest with the registered contestants.
  81.      *
  82.      * A contestant is randomly chosen from the group.
  83.      * If their token value matches eliminateOdd parity
  84.      * (starts as odd numbers, then flips to even numbers and back over time) then they are eliminated.
  85.      *
  86.      * PayToWinContestant's lose a life when they are selected to be eliminated, and are only
  87.      * actually eliminated when they have no lives remaining.
  88.      *
  89.      * The contest ends when there is only one contestant remaining.
  90.      *
  91.      * @return Returns the winner!
  92.      */
  93.     private Contestant computeWinner() {
  94.         if (registry.isEmpty()) {
  95.             throw new IllegalStateException("Cannot run a contest with no registered contestants!");
  96.         }
  97.         int index;
  98.         int time = 0;
  99.         while (registry.size() != 1) {
  100.             if (time++ % drawOrder.size() == 0) {
  101.                 eliminateOdd = !eliminateOdd;
  102.             }
  103.             index = PRNG.nextInt(drawOrder.size());
  104.             determineFate(drawOrder.get(index));
  105.         }
  106.         return registry.keySet().iterator().next();
  107.     }
  108.     private void determineFate(Contestant contestant) {
  109.         int token = registry.get(contestant);
  110.         boolean isOdd = token % 2 == 1;
  111.         if (!eliminateOdd) {
  112.             isOdd = !isOdd;
  113.         }
  114.         if (isOdd) {
  115.             boolean eliminate = true;
  116.             if (contestant instanceof PayToWinContestant) {
  117.                 eliminate = ((PayToWinContestant) contestant).removeLife();
  118.             }
  119.             if (eliminate) {
  120.                 registry.remove(contestant);
  121.             }
  122.         }
  123.     }
  124.     public static void main(String[] args) {
  125.         UnfairContest contest = new UnfairContest();
  126.         contest.register(new Contestant("Alice", 21));
  127.         contest.register(new Contestant("Bob", 21));
  128.         contest.register(new PayToWinContestant("Charlie", 12, 2));
  129.         contest.register(new Contestant("Dan", 65));
  130.         contest.register(new PayToWinContestant("Eve", 44, 3));
  131.         System.out.format("And the winner is... %s!\n", contest.computeWinner().getName());
  132.     }
  133. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top