Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- import java.util.Random;
- /**
- * This class runs a simple contest that determines a winner at the end.
- * Unfortunately, it is riddled with bugs and crashes, and some players have noticed
- * the winning distribution does not match the game rules, and in the future might game the system!
- *
- * Your job is to get a fair contest running smoothly!
- */
- public class UnfairContest {
- private static final Random PRNG = new Random();
- private final Map<Contestant, int> registry = new HashMap<>();
- private final List<Contestant> drawOrder = new ArrayList<>();
- private boolean eliminateOdd = true;
- public static class Contestant {
- private final String name;
- private final int age;
- public Contestant(String name, int age) {
- this.name = name;
- this.age = age;
- }
- public String getName() {
- return name;
- }
- public int getAge() {
- return age;
- }
- @Override
- public boolean equals(Object obj) {
- if (this == obj)
- return true;
- if (obj == null)
- return false;
- Contestant other = (Contestant) obj;
- if (age != other.age)
- return false;
- if (name != other.name)
- return false;
- return true;
- }
- }
- public static class PayToWinContestant extends Contestant {
- private int lives;
- public PayToWinContestant(String name, int age, int lives) {
- super(name, age);
- this.lives = lives;
- }
- public boolean removeLife() {
- return --lives <= 0;
- }
- @Override
- public boolean equals(Object obj) {
- if (this == obj)
- return true;
- if (!super.equals(obj))
- return false;
- PayToWinContestant other = (PayToWinContestant) obj;
- if (lives != other.lives)
- return false;
- return true;
- }
- }
- /**
- * Register a Contestant to participate in the contest.
- * A Contestant is unique by their name and age.
- * Contestants get a token valued from [-10,10] that is part of the game rules.
- * All tokens have equal probability of winning.
- *
- * @param contestant
- */
- private void register(Contestant contestant) {
- registry.put(contestant, PRNG.nextInt(21) - 10);
- drawOrder.add(contestant);
- }
- /**
- * Run a contest with the registered contestants.
- *
- * A contestant is randomly chosen from the group.
- * If their token value matches eliminateOdd parity
- * (starts as odd numbers, then flips to even numbers and back over time) then they are eliminated.
- *
- * PayToWinContestant's lose a life when they are selected to be eliminated, and are only
- * actually eliminated when they have no lives remaining.
- *
- * The contest ends when there is only one contestant remaining.
- *
- * @return Returns the winner!
- */
- private Contestant computeWinner() {
- if (registry.isEmpty()) {
- throw new IllegalStateException("Cannot run a contest with no registered contestants!");
- }
- int index;
- int time = 0;
- while (registry.size() != 1) {
- if (time++ % drawOrder.size() == 0) {
- eliminateOdd = !eliminateOdd;
- }
- index = PRNG.nextInt(drawOrder.size());
- determineFate(drawOrder.get(index));
- }
- return registry.keySet().iterator().next();
- }
- private void determineFate(Contestant contestant) {
- int token = registry.get(contestant);
- boolean isOdd = token % 2 == 1;
- if (!eliminateOdd) {
- isOdd = !isOdd;
- }
- if (isOdd) {
- boolean eliminate = true;
- if (contestant instanceof PayToWinContestant) {
- eliminate = ((PayToWinContestant) contestant).removeLife();
- }
- if (eliminate) {
- registry.remove(contestant);
- }
- }
- }
- public static void main(String[] args) {
- UnfairContest contest = new UnfairContest();
- contest.register(new Contestant("Alice", 21));
- contest.register(new Contestant("Bob", 21));
- contest.register(new PayToWinContestant("Charlie", 12, 2));
- contest.register(new Contestant("Dan", 65));
- contest.register(new PayToWinContestant("Eve", 44, 3));
- System.out.format("And the winner is... %s!\n", contest.computeWinner().getName());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement