Exikle
Exikle

Reputation: 1165

Keeping Track of which enum is selected?

/**
 * 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

Answers (3)

Henry
Henry

Reputation: 43758

Use a variable with the type of the enum, for example:

State var;
var = State.PLAYER_TWO_MENU;

Upvotes: 1

smk
smk

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

Javier
Javier

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

Related Questions