Let's say I created the following class:
public enum Position {
Dealer(1), //1
SB(2), //2
BB(3), //3
UTG(4), //4
UTG1(5), //5
UTG2(6), //6
UTG3(7), //7
HJ(8), //8
CO(9); //9
//Constructor
int code;
Position(int code) {
this.code = code;
}
}
How do I manipulate ENUM by using the numbers in parenthesis? For example, in my Poker Table class, I initiate new players. Each player passes the parameter Position. So initially,
player[1].getPosition() = Dealer
player[2].getPosition() = SB
player[3].getPosition() = BB
etc etc etc
After the hand is over, all the positions need to be shifted over by one.
So player[1] needs to have the CO(9) position.
player[2] needs to have the Dealer(1) position.
player[3] needs to have the SB(2) position.
etc etc
I understand that I can just make a for loop with a variable cycling through the numbers 1 through 9, but how do I access the position based on the integer inside the PositionENUM?
EDIT: I already have the getters and setters.
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
However the getters and the setters do not provide me with the correctly change the Positions of the players each round.
After every betting round, I need to change the Position of each player, so I need to figure out how to shift the ENUM Position of each player after each betting round.
It's possible to choose an enum instance based on the value of its code. You can use a static Map<Integer, Position>
to do this. The only gotcha is that it has to be housed in a static inner class:
public enum Position {
...
Position(int code) {
this.code = code;
MapHolder.BY_CODE.put(code, this);
}
private static class MapHolder {
private static final Map<Integer, Position> BY_CODE = new HashMap<Integer, Position>();
}
public static Position findByCode(int code) {
return MapHolder.BY_CODE.get(code);
}
I'd also recommend delegating the logic of picking the next position to the enum itself. Just add this method to the Position
enum:
public Position nextPosition() {
return findByCode((this.code + 1) % Position.values().length);
}
Then, your client code can simply go:
for (Player player : players) {
player.setPosition(player.getPosition().nextPosition());
}