...
Code Block | ||
---|---|---|
| ||
class GameEntry implements Comparable { public enum Roshambo {ROCK, PAPER, SCISSORS} private Roshambo value; public GameEntry(Roshambo value) { this.value = value; } public int compareTo(Object that) { if (!(that instanceof Roshambo)) { throw new ClassCastException(); } GameEntry t = (GameEntry) that; return (value == t.value) ? 0 : (value == Roshambo.ROCK && t.value == Roshambo.PAPER) ? -1 : (value == Roshambo.PAPER && t.value == Roshambo.SCISSORS) ? -1 : (value == Roshambo.SCISSORS && t.value == Roshambo.ROCK) ? -1 : 1; } } |
However, this game violates the notion of transitivity, since Rock beats Scissors, Scissors beats Paper, but Rock does not beat Paper.
...
Code Block | ||
---|---|---|
| ||
class GameEntry { public enum Roshambo {ROCK, PAPER, SCISSORS} private Roshambo value; public GameEntry(Roshambo value) { this.value = value; } public int beats(Object that) { if (!(that instanceof Roshambo)) { throw new ClassCastException(); } GameEntry t = (GameEntry) that; return (value == t.value) ? 0 : (value == Roshambo.ROCK && t.value == Roshambo.PAPER) ? -1 : (value == Roshambo.PAPER && t.value == Roshambo.SCISSORS) ? -1 : (value == Roshambo.SCISSORS && t.value == Roshambo.ROCK) ? -1 : 1; } } |
Risk Assessment
Violating the general contract when implementing the compareTo()
method can result in unexpected results, possibly leading to invalid comparisons and information disclosure.
...