fork download
  1. import java.util.Map;
  2.  
  3. enum Symbol {
  4. ROCK,
  5. PAPER,
  6. SCISSORS;
  7.  
  8. private static final Map<Symbol, Symbol> ASSOCIATIONS = Map.of(
  9. ROCK, SCISSORS,
  10. SCISSORS, PAPER,
  11. PAPER, ROCK
  12. );
  13.  
  14. public static void main(String[] args) {
  15. System.out.println("Rock against scissors: " + ROCK.against(SCISSORS));
  16. System.out.println("Rock against paper: " + ROCK.against(PAPER));
  17. System.out.println("Rock against rock: " + ROCK.against(ROCK));
  18. }
  19.  
  20. public Result against(Symbol other) {
  21. if (ASSOCIATIONS.get(this) == other) {
  22. return Result.WIN;
  23. }
  24. else if (ASSOCIATIONS.get(other) == this) {
  25. return Result.LOSE;
  26. }
  27. else {
  28. return Result.TIE;
  29. }
  30. }
  31. }
  32.  
  33. enum Result {
  34. WIN, LOSE, TIE;
  35. }
  36.  
Success #stdin #stdout 0.12s 50072KB
stdin
Standard input is empty
stdout
Rock against scissors: WIN
Rock against paper: LOSE
Rock against rock: TIE