Reputation: 1165
/**
* All the states in the game.
*/
public enum State {
START_MENU,
PLAYER_ONE_MENU,
PLAYER_ONE_CATEGORY,
PLAYER_TWO_MENU,
PLAYER_TWO_CATEGORY,
WIN_SCREEN,
LOSE_SCREEN,
PLAY_SCREEN
}
That is the enum i have made for a hangman game i am designing. However I am new to enums but someone suggested I use enums for game states. However I do not know how to track which state the player is currently on. Could someone explain a way to track which state is selected?
Upvotes: 0
Views: 507
Reputation: 43758
Use a variable with the type of the enum, for example:
State var;
var = State.PLAYER_TWO_MENU;
Upvotes: 1
Reputation: 5882
As per Java Doc for Enum:
valueOf(Class enumType, String name)
Returns the enum constant of the specified enum type with the specified name.
Upvotes: 1
Reputation: 12398
Just keep a variable of type States
with the current state.
class Player {
States currentState=States.STARTMENU;
void doSomething() {
switch (currentState) {
case STARTMENU:...;
case PLAYERONEMENU:...;
//etc
}
}
void playMenu() {
if (currentState==States.PLAYMENU) {...}
}
}
Upvotes: 1