I'd appreciate any comments and feedback about what I did regarding enum constants in Go, as described below.
I tried implementing a Rock Paper Scissors Lizard Spock program in Go. In my
Java implementation, I defined an enum called Move to represent the possible moves one can make.
The constants are defined in that order to take advantage of the implicit
ordinal() method in calculating the winner of a round.
In Go, I did this:
I decided to define LAST_Move because I couldn't find a way to limit the value of a Move to the range (0..4) since Move is basically an alias for the built-in type
int. It seems if you define an "enum" in Go like this, you have to do your own range checking, which is why I added the
NotLast() and
InRange() methods for Move. This is how I used
NotLast():
And here's what MatchUp looks like:
And this is the output of
SheldonExplains():
Scissors cuts Paper
Paper covers Rock
Rock crushes Lizard
Lizard poisons Spock
Spock smashes Scissors
Scissors decapitates Lizard
Lizard eats Paper
Paper disproves Spock
Spock vaporizes Rock
...and as it always has, Rock crushes Scissors