0

I'm currently working on the Einstein riddle. For those who don't know it: https://udel.edu/~os/riddle.html I have to implement a constraintSolver and my task now is to model the riddle specific constraints and the problem itself and then give the problem into it.

My current plan is to set a value, check all constraints - and if it's still consistent, continue, else use backtracking and continue with something else.

In my backtracking algorithm, I just iterate through the list of domains, in it iterate over the addresses (indices) and in these, iterate over the possible domain-specific values.

The Einstein riddle, however, has 5 domains: colors, nationality, animals, etc

for example : (Domain is just an empty interface)

public enum Colors implements Domain{
    RED, BLUE, GREEN;
}

public enum Animals implements Domain{
    CAT, DOG, HORSE;
}

[...]
List<List<Domain>> domains = new ... ;
List<Colors> colors = ...
[...]
domains.add(colors)

I thought, using this variant in my backtracking algorithm I wouldn't have to care about the type of the specific domain. However, I can't add the list of colors to the list of domains.

I also tried it with a wildcard - however this doesn't work either. And I don't really like the idea of working just with strings.

Is there a way of getting this to work? (If my general idea is stupid, please tell me, too, but my main question is, if you can abstract the enums so that you can put lists of specific enums in a list of an interface for these enums.

Thanks for any feedback :)

Glenn
  • 8,932
  • 2
  • 41
  • 54
MrLowbob
  • 102
  • 6
  • PECS: `List>`. [A `List` is **not a** `List`](https://stackoverflow.com/questions/2660827/java-generics-covariance). – Boris the Spider Jun 12 '16 at 12:58
  • 1
    Nit: `RED` should be a `Color`, not a `Colors`. Similarly, if there are 5 domains, one of which is colors, `RED` is not a domain. Enum *values* implement the interface, not the enum. – Andy Turner Jun 12 '16 at 13:03
  • Following on from what [Andy Turner said](https://stackoverflow.com/questions/37774460/using-different-enums-with-an-interface-in-java#comment63016610_37774460), the fact that you have a `List>` should be **very odd**. You have a `List` where your `List<...>` _is a_ `Domain`. – Boris the Spider Jun 12 '16 at 13:20
  • Thanks for your feedback. @Andy Turner - regards the naming - yep, in my code (not what i just typed here) its right. – MrLowbob Jun 12 '16 at 13:31
  • 1
    @MrLowbob it's not naming, it's design. `RED` is _**not** a_ `Domain`. `Red` is member of the domain of `Color`. Your modelling is somewhat ... odd. – Boris the Spider Jun 12 '16 at 13:33

1 Answers1

0

You just need to change your declaration of the domains to

List<List<? extends Domain>> domains = ...;

... to tell the complier that it is a list of lists of some subclass of Domain. A list of Domain objects should allow insertion of any Domain object whereas the list of Colors will only allow insertion of colors...

Per Huss
  • 4,755
  • 12
  • 29
  • thank you, I should get some more sleep, i know the concept with the wildcards but didnt get it in mind ..^^ – MrLowbob Jun 12 '16 at 13:33