Java store combinations of enums with result
Asked Answered
C

6

5

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

Chary answered 21/10, 2021 at 16:7 Comment(5)
What you have is a circular relationship. Rock beats Scissors beats Paper beats Rock, etc... So model it as a circle. If you hold an item it will win against the next one clockwise and lose against the one counter-clockwise. If you go to more choices the question is if the rules are the same or not ?Fructify
@RomainHippeau unfortunately if using 4 or more choices you do not have that circular relationship. I have updated the initial question, thanks!Chary
look at the pattern, you have your circular relationship and you also have the relationship where it beats n - 2Fructify
What happens if you have 4 or 6 or more ?Fructify
The solutions below will work, if you are looking for a generic solution you will need to understand the pattern and come up with an algorithm to calculate.Fructify
C
4

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;
    }
}
Confiscable answered 21/10, 2021 at 16:49 Comment(0)
P
3

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));
    }
}
Pasco answered 21/10, 2021 at 16:44 Comment(0)
H
2

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.

Harless answered 22/10, 2021 at 8:22 Comment(0)
H
1

I would propose the following:

enum Symbol {
    ROCK,
    PAPER,
    SCISSORS;

    private static final Map<Symbol, Symbol> ASSOCIATIONS = Map.of(
        ROCK, SCISSORS,
        SCISSORS, PAPER,
        PAPER, ROCK
    );

    public Result against(Symbol other) {
        if (ASSOCIATIONS.get(this) == other) {
            return Result.WIN;
        }
        else if (ASSOCIATIONS.get(other) == this) {
            return Result.LOSE;
        }
        else {
            return Result.TIE;
        }
    }
}

enum Result {
    WIN, LOSE, TIE;
}

What happens here, is that we put all associations into a map. The key associated to the value means that the key wins from the value.

So the against method needs to do this:

  • Lookup this within the map, and of the contained value is other, then it's a win.
  • The other way around, lookup other within the map, and if the contained value is this, then it's a loss.
  • Otherwise, it's a tie.

This also works with more than three symbols. Just map a key to a value, in order to define that key wins from value.

Online demo

Herbalist answered 22/10, 2021 at 13:25 Comment(0)
M
1

Enums can have properties, you can keep a list of who a Symbol loses to.

    ROCK("SCISSORS", "LIZZARD")

And then just check if a given symbol is in the list

public Result calculateResult(Symbol hand2) {
  return this == hand2 
    ? Result.TIE
    : beats.contains(hand2.toString()) ? Result.WIN : Result.LOOSE;
}

Full running program below:

import java.util.*;
enum Result {
  WIN,
  TIE,
  LOOSE;
}

enum Symbol {

  ROCK("SCISSORS", "LIZZARD"),
  SCISSORS("PAPER", "LIZZARD"),
  PAPER("ROCK", "SPOCK"),
  SPOCK("SCISSORS", "ROCK"),
  LIZZARD("SPOCK", "PAPER");


  private List<String> beats;
  
  Symbol(String ... beats) {
    this.beats = Arrays.asList(beats);
  }
    
  public Result calculateResult(Symbol hand2) {
    return this == hand2 
      ? Result.TIE
      : beats.contains(hand2.toString()) ? Result.WIN : Result.LOOSE;
  }
}
class Main {
  public static void main(String[] args) {
    var random = new Random();
    var values = Symbol.values();
    Symbol one = values[random.nextInt(values.length)];
    Symbol two = values[random.nextInt(values.length)];
    System.out.printf("%s vs %s  = %s%n", one, two, one.calculateResult(two));

  }
}

You may notice I'm using the string value in the constructor, this is because apparently I cannot define a forward definition:

ROCK(SCISSORS,LIZZARD)

While in general is not recommended to stringify your types, I think this is a very valid case. The strings are not exposed publicly and the users of the enum will not be aware of them

Result r = Symbol.ROCK.calculateResult(Symbol.PAPER);
Mosaic answered 22/10, 2021 at 20:9 Comment(0)
H
0

An ideal solution would be to have each element know which other elements it is able to defeat. However, you can't always pass enum values in each other's constructors because when one enum is being constructed, the other enums might not yet be initialised. Here are some workarounds:

You can define an abstract method that will return all the symbols that each symbol will beat, then define a concrete implementation for each symbol:

enum Symbol {
    ROCK {
        protected Set<Symbol> beats() {
            return EnumSet.of(SCISSORS);
        }
    },
    PAPER {
        protected Set<Symbol> beats() {
            return EnumSet.of(ROCK);
        }
    },
    SCISSORS {
        protected Set<Symbol> beats() {
            return EnumSet.of(PAPER);
        }
    },
    LASER {
        protected Symbol[] beats() {
            return EnumSet.of(ROCK, PAPER, SCISSORS);
        }
    };

    protected abstract Symbol[] beats();

    public Result calculateResult(Symbol hand2) {
        if (this.beats().contains(hand2)) {
            return WIN;
        } else if (hand2.beats().contains(this)) {
            return LOSE;
        } else {
            return TIE;
        }
    }

You could extract each of those sets as a constant, if creating the sets each time is an proven performance issue.

Alternatively, you could initialise each symbol with the symbols it can defeat with a static initialiser:

enum Symbol {
    ROCK,
    PAPER,
    SCISSORS,
    LASER;

    static {
        ROCK.beats = EnumSet.of(SCISSORS);
        PAPER.beats = EnumSet.of(ROCK);
        SCISSORS.beats = EnumSet.of(ROCK);
        LASER.beats = EnumSet.of(ROCK, PAPER, SCISSORS);
    }

    private Set<Symbol> beats;

    public Result calculateResult(Symbol hand2) {
        if (this.beats.contains(hand2)) {
            return WIN;
        } else if (hand2.beats.contains(this)) {
            return LOSE;
        } else {
            return TIE;
        }
    }
Harless answered 3/11, 2021 at 13:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.