Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

198
Views
Java store combinations of enums with result

I have quite a tricky question where I currently cannot find an answer to:

What I have is an enum with x variants like:

public enum Symbol {
    ROCK,
    PAPER,
    SCISSORS;
}

This enum is supposed to be extended by one, two, ... variants (like Rock, Paper, Scissors, Spock, Lizard) and I am searching for an option to store the result of the combinations (in the example, who is winning). Like in Rock, Paper, Scissors I cannot assign a weight/value to the enum which then could be used for comparision. The result of the comparision is not based on logic. What I am currently doing is:

public Result calculateResult(Symbol hand2) {

            Symbol handP1 = this;
            Symbol handP2 = hand2;

            if (handP1 == Symbol.ROCK) {
                switch (handP2) {
                    case SCISSORS:
                        return Result.WIN;
                    case ROCK:
                        return Result.TIE;
                    case PAPER:
                        return Result.LOOSE;
                } ....

I do this for every possible option for the first element of the comparison (in the example hand1). With 3 enums this is quite easy and tidy, but with 4,5 or more it gets messy real quick. Using if and else if for comparison is not really suitable as well.Is there any better way to handle this issue?

I have setup a repl if you want to try it out yourself: REPL Link

Using for or more choices break the circular relationship you might have with 3 choices. Please see here for an example:

5 choices

Thanks for any tips and hints which might help me get a better solution

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

Something like this could help

public enum Symbol {
    ROCK,
    SCISSORS,
    PAPER;

  private List<Symbol> beats;

  static {
    ROCK.beats = Arrays.asList(SCISSORS);
    SCISSORS.beats = Arrays.asList(PAPER);
    PAPER.beats = Arrays.asList(ROCK);
  }
    
    public Result calculateResult(Symbol hand2) {

        if (this.beats.contains(hand2)) return Result.WIN;
        if (hand2.beats.contains(this)) return Result.LOOSE;
        return Result.TIE;
    }
}
over 4 years ago · Santiago Trujillo Report

0

Something like this might work:

import java.util.Arrays;
import java.util.List;

public class SymbolGame {

    public enum Result {
        WIN,TIE,LOOSE;
    }
    public enum Symbol {
        ROCK,
        PAPER,
        SCISSORS;
        List<Symbol> winsFrom;

        public void setWinsFrom(Symbol... winsFrom) {
            this.winsFrom = Arrays.asList(winsFrom);
        }
        public Result calculate(Symbol other) {
            if(this==other)
                return Result.TIE;
            if (getWinsFrom().contains(other))
                return Result.WIN;
            return Result.LOOSE;
        }
    }
    // somewhere you need to define the rules
    static {
        Symbol.ROCK.setWinsFrom(Symbol.SCISSORS);
        Symbol.PAPER.setWinsFrom(Symbol.ROCK);
        Symbol.SCISSORS.setWinsFrom(Symbol.PAPER);
    }
    public static void main(String[] args) {
        System.out.println("ROCK against PAPER:"+Symbol.ROCK.calculate(Symbol.PAPER));
        System.out.println("PAPER against ROCK:"+Symbol.PAPER.calculate(Symbol.ROCK));
        System.out.println("ROCK against ROCK:"+Symbol.ROCK.calculate(Symbol.ROCK));
    }
}
over 4 years ago · Santiago Trujillo Report

0

Enums have an ordinal associated with each value, so that makes a natural way to index into a 2-dimensional array. This is also nice because it is quite similar to how you might write down all the possibilities on a piece of paper. It's easy to extend by simply adding more columns and rows:

// @formatter:off
private static final Result[][] RESULTS = {
    // v versus >    ROCK  PAPER SCISSORS
    /* ROCK.    */ { TIE,  LOSE, WIN  },
    /* PAPER    */ { WIN,  TIE,  LOSE },
    /* SCISSORS */ { LOSE, WIN,  TIE  },
    }
// @formatter:on

public Result calculateResult(Symbol hand1, Symbol hand2) {
    return RESULTS[hand1.ordinal()][hand2.ordinal()]
}

Many IDEs support the @formatter:on/off comment to stop them reformatting your table.

IMPORTANT: Effective Java and the Java API itself say not to use ordinal() because of the dangers of future changes (reording, inserting new values) causing subtle bugs. It is true here, too: if you just change the enum to be ROCK,SCISSORS,PAPER, then this table form will fail. At the very least, put this code inside Symbol so it's clear that it needs updating, and write good unit tests.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!