OnJava8-Examples/enums/RoShamBo6.java
Bruce Eckel 186333e022 enums
2015-05-31 10:58:40 -07:00

22 lines
596 B
Java

//: enums/RoShamBo6.java
// ©2015 MindView LLC: see Copyright.txt
// Enums using "tables" instead of multiple dispatch.
package enums;
import static enums.Outcome.*;
enum RoShamBo6 implements Competitor<RoShamBo6> {
PAPER, SCISSORS, ROCK;
private static Outcome[][] table = {
{ DRAW, LOSE, WIN }, // PAPER
{ WIN, DRAW, LOSE }, // SCISSORS
{ LOSE, WIN, DRAW }, // ROCK
};
@Override
public Outcome compete(RoShamBo6 other) {
return table[this.ordinal()][other.ordinal()];
}
public static void main(String[] args) {
RoShamBo.play(RoShamBo6.class, 20);
}
} ///:~