Game State Manager(FSM) with in.nextLine()
up vote
-1
down vote
favorite
I recently started creating a text based game and had posted a question regarding it here once before. Upon asking that question it was brought to my attention that my game loop was too long and that I should split things up. I was given multiple different suggestions all of which I am currently looking into. Right now I am trying to create GameStates through use of an FSM to better help with readability and "cleanliness" (for lack of a better term) of code. Aside from that I have taken on this task to learn about the uses of FSM's in general as I am self taught and constantly trying to learn more about Game Programming.
My Question today is...
Within the switch statement here:
public void stateSwitch() {
switch(state) {
case NAME_STATE:
n.getName();
break;
case BATTLE_STATE:
b.spawnEnemy();
break;
case VICTORY_STATE:
v.reward();
break;
case DEATH_STATE:
d.die();
}
How would I go about setting the in.nextLine() as the parameter or requirement for advancing to the next state using:
public class nameState {
private GameStateManager state;
GameState game = new GameStateManager.GameState(state);
public void getName() {
Scanner in = new Scanner(System.in);
System.out.println("t ############################## " +
"nt # Welcome to Text Adventure! # " +
"nt ############################## ");
System.out.println("t ---------------------------------");
System.out.println("t What is your name Adventurer?");
String userName = in.nextLine();
}
Similar to the use of:
switch(m_curState) {
case TITLE_STATE:
UpdateTitleScreen();
if(UserPressesEnter()) {
m_curState = MAINGAME_STATE;
}
break;
Note that the:
private GameStateManager state;
GameState game = new GameStateManager.GameState(state);
are currently throwing errors that I am aware of. However this line was written in an attempt to figure out how to retrieve this in.nextLine() and set it as what is required to ultimately move from NAME_STATE to BATTLE_STATE.
The idea is that when the game is opened/ran the user will be prompted with the "Welcome" message and then asked what their name shall be and then once the user enters their desired name they will transition or switch to the BATTLE_STATE and enter battle with an enemy using spawnEnemy(). This is temporary as I would like to incorporate a MAIN_MENU_STATE at some point but am just taking things one step at a time.
Below is the entirety of my code for GameStateManager:
public enum GameStateManager {
NAME_STATE, BATTLE_STATE, VICTORY_STATE, DEATH_STATE;
public class GameState {
GameStateManager state;
public GameState(GameStateManager state) {
this.state = state;
}
nameState n = new nameState();
Battle b = new Battle();
deathState d = new deathState();
victoryState v = new victoryState();
public void stateSwitch() {
switch(state) {
case NAME_STATE:
n.getName();
break;
case BATTLE_STATE:
b.spawnEnemy();
break;
case VICTORY_STATE:
v.reward();
break;
case DEATH_STATE:
d.die();
}
}
}
and the nameState code linked earlier is the entire class as of now.
Note that most of the code in enum GameStateManager below NAME_STATE within the switch is not actually doing anything yet. In fact victoryState and deathState are not even classes as of the time of writing.
Please feel free to critique this code in anyway you like or offer help on things you might consider wrong or clunky even if they don't pertain to the question asked as I am wanting to learn as much as I can. If the way I am writing my enum is incorrect PLEASE tell me as I have struggled finding a reference to whether this is the correct way this should be done or not.
java game
New contributor
Brian Craycraft is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |
up vote
-1
down vote
favorite
I recently started creating a text based game and had posted a question regarding it here once before. Upon asking that question it was brought to my attention that my game loop was too long and that I should split things up. I was given multiple different suggestions all of which I am currently looking into. Right now I am trying to create GameStates through use of an FSM to better help with readability and "cleanliness" (for lack of a better term) of code. Aside from that I have taken on this task to learn about the uses of FSM's in general as I am self taught and constantly trying to learn more about Game Programming.
My Question today is...
Within the switch statement here:
public void stateSwitch() {
switch(state) {
case NAME_STATE:
n.getName();
break;
case BATTLE_STATE:
b.spawnEnemy();
break;
case VICTORY_STATE:
v.reward();
break;
case DEATH_STATE:
d.die();
}
How would I go about setting the in.nextLine() as the parameter or requirement for advancing to the next state using:
public class nameState {
private GameStateManager state;
GameState game = new GameStateManager.GameState(state);
public void getName() {
Scanner in = new Scanner(System.in);
System.out.println("t ############################## " +
"nt # Welcome to Text Adventure! # " +
"nt ############################## ");
System.out.println("t ---------------------------------");
System.out.println("t What is your name Adventurer?");
String userName = in.nextLine();
}
Similar to the use of:
switch(m_curState) {
case TITLE_STATE:
UpdateTitleScreen();
if(UserPressesEnter()) {
m_curState = MAINGAME_STATE;
}
break;
Note that the:
private GameStateManager state;
GameState game = new GameStateManager.GameState(state);
are currently throwing errors that I am aware of. However this line was written in an attempt to figure out how to retrieve this in.nextLine() and set it as what is required to ultimately move from NAME_STATE to BATTLE_STATE.
The idea is that when the game is opened/ran the user will be prompted with the "Welcome" message and then asked what their name shall be and then once the user enters their desired name they will transition or switch to the BATTLE_STATE and enter battle with an enemy using spawnEnemy(). This is temporary as I would like to incorporate a MAIN_MENU_STATE at some point but am just taking things one step at a time.
Below is the entirety of my code for GameStateManager:
public enum GameStateManager {
NAME_STATE, BATTLE_STATE, VICTORY_STATE, DEATH_STATE;
public class GameState {
GameStateManager state;
public GameState(GameStateManager state) {
this.state = state;
}
nameState n = new nameState();
Battle b = new Battle();
deathState d = new deathState();
victoryState v = new victoryState();
public void stateSwitch() {
switch(state) {
case NAME_STATE:
n.getName();
break;
case BATTLE_STATE:
b.spawnEnemy();
break;
case VICTORY_STATE:
v.reward();
break;
case DEATH_STATE:
d.die();
}
}
}
and the nameState code linked earlier is the entire class as of now.
Note that most of the code in enum GameStateManager below NAME_STATE within the switch is not actually doing anything yet. In fact victoryState and deathState are not even classes as of the time of writing.
Please feel free to critique this code in anyway you like or offer help on things you might consider wrong or clunky even if they don't pertain to the question asked as I am wanting to learn as much as I can. If the way I am writing my enum is incorrect PLEASE tell me as I have struggled finding a reference to whether this is the correct way this should be done or not.
java game
New contributor
Brian Craycraft is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
2
I've voted to close this question as code that doesn't work, and therefore not eligible for a review. For example,.getName()setsuserNameas a local variable, then never does anything useful with it.
– 200_success
1 hour ago
I understand your reason behind that however the code you are referring to is not the code I am requesting help with. That code was simply there as reference to what I was asking about. I also did not provide my main class where getName() is actually being called and used temporarily while I figure out the GameStates.
– Brian Craycraft
1 hour ago
2
@BrianCraycraft It is often difficult or impossible to give a good critique of partial code. In order to provide helpful comments, we need to see the real context where the code is being used. In your post, you've left out some of the calling context (where does GameState actually get used?) as well as lower-level details (what are the implementations of nameState, deathState and victoryState?) that are important for a helpful critique.
– cariehl
1 hour ago
@cariehl I was simply asking how to implement the user entering a name into the switch provided to transition between states. The critique I mentioned at the end of the post was meant more as a critique on how thepublic enum GameStateManagerwas written and general structure of the code. It is its own standalone enum. It is not a class, interface etc,. It is New > Enum. I am currently in the middle of writing this GameStateManager and setting up my states therefore they are not being called or used anywhere as of yet. As mentioned in a closing note deathState and victoryState are not made.
– Brian Craycraft
52 mins ago
add a comment |
up vote
-1
down vote
favorite
up vote
-1
down vote
favorite
I recently started creating a text based game and had posted a question regarding it here once before. Upon asking that question it was brought to my attention that my game loop was too long and that I should split things up. I was given multiple different suggestions all of which I am currently looking into. Right now I am trying to create GameStates through use of an FSM to better help with readability and "cleanliness" (for lack of a better term) of code. Aside from that I have taken on this task to learn about the uses of FSM's in general as I am self taught and constantly trying to learn more about Game Programming.
My Question today is...
Within the switch statement here:
public void stateSwitch() {
switch(state) {
case NAME_STATE:
n.getName();
break;
case BATTLE_STATE:
b.spawnEnemy();
break;
case VICTORY_STATE:
v.reward();
break;
case DEATH_STATE:
d.die();
}
How would I go about setting the in.nextLine() as the parameter or requirement for advancing to the next state using:
public class nameState {
private GameStateManager state;
GameState game = new GameStateManager.GameState(state);
public void getName() {
Scanner in = new Scanner(System.in);
System.out.println("t ############################## " +
"nt # Welcome to Text Adventure! # " +
"nt ############################## ");
System.out.println("t ---------------------------------");
System.out.println("t What is your name Adventurer?");
String userName = in.nextLine();
}
Similar to the use of:
switch(m_curState) {
case TITLE_STATE:
UpdateTitleScreen();
if(UserPressesEnter()) {
m_curState = MAINGAME_STATE;
}
break;
Note that the:
private GameStateManager state;
GameState game = new GameStateManager.GameState(state);
are currently throwing errors that I am aware of. However this line was written in an attempt to figure out how to retrieve this in.nextLine() and set it as what is required to ultimately move from NAME_STATE to BATTLE_STATE.
The idea is that when the game is opened/ran the user will be prompted with the "Welcome" message and then asked what their name shall be and then once the user enters their desired name they will transition or switch to the BATTLE_STATE and enter battle with an enemy using spawnEnemy(). This is temporary as I would like to incorporate a MAIN_MENU_STATE at some point but am just taking things one step at a time.
Below is the entirety of my code for GameStateManager:
public enum GameStateManager {
NAME_STATE, BATTLE_STATE, VICTORY_STATE, DEATH_STATE;
public class GameState {
GameStateManager state;
public GameState(GameStateManager state) {
this.state = state;
}
nameState n = new nameState();
Battle b = new Battle();
deathState d = new deathState();
victoryState v = new victoryState();
public void stateSwitch() {
switch(state) {
case NAME_STATE:
n.getName();
break;
case BATTLE_STATE:
b.spawnEnemy();
break;
case VICTORY_STATE:
v.reward();
break;
case DEATH_STATE:
d.die();
}
}
}
and the nameState code linked earlier is the entire class as of now.
Note that most of the code in enum GameStateManager below NAME_STATE within the switch is not actually doing anything yet. In fact victoryState and deathState are not even classes as of the time of writing.
Please feel free to critique this code in anyway you like or offer help on things you might consider wrong or clunky even if they don't pertain to the question asked as I am wanting to learn as much as I can. If the way I am writing my enum is incorrect PLEASE tell me as I have struggled finding a reference to whether this is the correct way this should be done or not.
java game
New contributor
Brian Craycraft is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
I recently started creating a text based game and had posted a question regarding it here once before. Upon asking that question it was brought to my attention that my game loop was too long and that I should split things up. I was given multiple different suggestions all of which I am currently looking into. Right now I am trying to create GameStates through use of an FSM to better help with readability and "cleanliness" (for lack of a better term) of code. Aside from that I have taken on this task to learn about the uses of FSM's in general as I am self taught and constantly trying to learn more about Game Programming.
My Question today is...
Within the switch statement here:
public void stateSwitch() {
switch(state) {
case NAME_STATE:
n.getName();
break;
case BATTLE_STATE:
b.spawnEnemy();
break;
case VICTORY_STATE:
v.reward();
break;
case DEATH_STATE:
d.die();
}
How would I go about setting the in.nextLine() as the parameter or requirement for advancing to the next state using:
public class nameState {
private GameStateManager state;
GameState game = new GameStateManager.GameState(state);
public void getName() {
Scanner in = new Scanner(System.in);
System.out.println("t ############################## " +
"nt # Welcome to Text Adventure! # " +
"nt ############################## ");
System.out.println("t ---------------------------------");
System.out.println("t What is your name Adventurer?");
String userName = in.nextLine();
}
Similar to the use of:
switch(m_curState) {
case TITLE_STATE:
UpdateTitleScreen();
if(UserPressesEnter()) {
m_curState = MAINGAME_STATE;
}
break;
Note that the:
private GameStateManager state;
GameState game = new GameStateManager.GameState(state);
are currently throwing errors that I am aware of. However this line was written in an attempt to figure out how to retrieve this in.nextLine() and set it as what is required to ultimately move from NAME_STATE to BATTLE_STATE.
The idea is that when the game is opened/ran the user will be prompted with the "Welcome" message and then asked what their name shall be and then once the user enters their desired name they will transition or switch to the BATTLE_STATE and enter battle with an enemy using spawnEnemy(). This is temporary as I would like to incorporate a MAIN_MENU_STATE at some point but am just taking things one step at a time.
Below is the entirety of my code for GameStateManager:
public enum GameStateManager {
NAME_STATE, BATTLE_STATE, VICTORY_STATE, DEATH_STATE;
public class GameState {
GameStateManager state;
public GameState(GameStateManager state) {
this.state = state;
}
nameState n = new nameState();
Battle b = new Battle();
deathState d = new deathState();
victoryState v = new victoryState();
public void stateSwitch() {
switch(state) {
case NAME_STATE:
n.getName();
break;
case BATTLE_STATE:
b.spawnEnemy();
break;
case VICTORY_STATE:
v.reward();
break;
case DEATH_STATE:
d.die();
}
}
}
and the nameState code linked earlier is the entire class as of now.
Note that most of the code in enum GameStateManager below NAME_STATE within the switch is not actually doing anything yet. In fact victoryState and deathState are not even classes as of the time of writing.
Please feel free to critique this code in anyway you like or offer help on things you might consider wrong or clunky even if they don't pertain to the question asked as I am wanting to learn as much as I can. If the way I am writing my enum is incorrect PLEASE tell me as I have struggled finding a reference to whether this is the correct way this should be done or not.
java game
java game
New contributor
Brian Craycraft is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Brian Craycraft is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Brian Craycraft is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
asked 1 hour ago
Brian Craycraft
363
363
New contributor
Brian Craycraft is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Brian Craycraft is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
Brian Craycraft is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
2
I've voted to close this question as code that doesn't work, and therefore not eligible for a review. For example,.getName()setsuserNameas a local variable, then never does anything useful with it.
– 200_success
1 hour ago
I understand your reason behind that however the code you are referring to is not the code I am requesting help with. That code was simply there as reference to what I was asking about. I also did not provide my main class where getName() is actually being called and used temporarily while I figure out the GameStates.
– Brian Craycraft
1 hour ago
2
@BrianCraycraft It is often difficult or impossible to give a good critique of partial code. In order to provide helpful comments, we need to see the real context where the code is being used. In your post, you've left out some of the calling context (where does GameState actually get used?) as well as lower-level details (what are the implementations of nameState, deathState and victoryState?) that are important for a helpful critique.
– cariehl
1 hour ago
@cariehl I was simply asking how to implement the user entering a name into the switch provided to transition between states. The critique I mentioned at the end of the post was meant more as a critique on how thepublic enum GameStateManagerwas written and general structure of the code. It is its own standalone enum. It is not a class, interface etc,. It is New > Enum. I am currently in the middle of writing this GameStateManager and setting up my states therefore they are not being called or used anywhere as of yet. As mentioned in a closing note deathState and victoryState are not made.
– Brian Craycraft
52 mins ago
add a comment |
2
I've voted to close this question as code that doesn't work, and therefore not eligible for a review. For example,.getName()setsuserNameas a local variable, then never does anything useful with it.
– 200_success
1 hour ago
I understand your reason behind that however the code you are referring to is not the code I am requesting help with. That code was simply there as reference to what I was asking about. I also did not provide my main class where getName() is actually being called and used temporarily while I figure out the GameStates.
– Brian Craycraft
1 hour ago
2
@BrianCraycraft It is often difficult or impossible to give a good critique of partial code. In order to provide helpful comments, we need to see the real context where the code is being used. In your post, you've left out some of the calling context (where does GameState actually get used?) as well as lower-level details (what are the implementations of nameState, deathState and victoryState?) that are important for a helpful critique.
– cariehl
1 hour ago
@cariehl I was simply asking how to implement the user entering a name into the switch provided to transition between states. The critique I mentioned at the end of the post was meant more as a critique on how thepublic enum GameStateManagerwas written and general structure of the code. It is its own standalone enum. It is not a class, interface etc,. It is New > Enum. I am currently in the middle of writing this GameStateManager and setting up my states therefore they are not being called or used anywhere as of yet. As mentioned in a closing note deathState and victoryState are not made.
– Brian Craycraft
52 mins ago
2
2
I've voted to close this question as code that doesn't work, and therefore not eligible for a review. For example,
.getName() sets userName as a local variable, then never does anything useful with it.– 200_success
1 hour ago
I've voted to close this question as code that doesn't work, and therefore not eligible for a review. For example,
.getName() sets userName as a local variable, then never does anything useful with it.– 200_success
1 hour ago
I understand your reason behind that however the code you are referring to is not the code I am requesting help with. That code was simply there as reference to what I was asking about. I also did not provide my main class where getName() is actually being called and used temporarily while I figure out the GameStates.
– Brian Craycraft
1 hour ago
I understand your reason behind that however the code you are referring to is not the code I am requesting help with. That code was simply there as reference to what I was asking about. I also did not provide my main class where getName() is actually being called and used temporarily while I figure out the GameStates.
– Brian Craycraft
1 hour ago
2
2
@BrianCraycraft It is often difficult or impossible to give a good critique of partial code. In order to provide helpful comments, we need to see the real context where the code is being used. In your post, you've left out some of the calling context (where does GameState actually get used?) as well as lower-level details (what are the implementations of nameState, deathState and victoryState?) that are important for a helpful critique.
– cariehl
1 hour ago
@BrianCraycraft It is often difficult or impossible to give a good critique of partial code. In order to provide helpful comments, we need to see the real context where the code is being used. In your post, you've left out some of the calling context (where does GameState actually get used?) as well as lower-level details (what are the implementations of nameState, deathState and victoryState?) that are important for a helpful critique.
– cariehl
1 hour ago
@cariehl I was simply asking how to implement the user entering a name into the switch provided to transition between states. The critique I mentioned at the end of the post was meant more as a critique on how the
public enum GameStateManager was written and general structure of the code. It is its own standalone enum. It is not a class, interface etc,. It is New > Enum. I am currently in the middle of writing this GameStateManager and setting up my states therefore they are not being called or used anywhere as of yet. As mentioned in a closing note deathState and victoryState are not made.– Brian Craycraft
52 mins ago
@cariehl I was simply asking how to implement the user entering a name into the switch provided to transition between states. The critique I mentioned at the end of the post was meant more as a critique on how the
public enum GameStateManager was written and general structure of the code. It is its own standalone enum. It is not a class, interface etc,. It is New > Enum. I am currently in the middle of writing this GameStateManager and setting up my states therefore they are not being called or used anywhere as of yet. As mentioned in a closing note deathState and victoryState are not made.– Brian Craycraft
52 mins ago
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Brian Craycraft is a new contributor. Be nice, and check out our Code of Conduct.
Brian Craycraft is a new contributor. Be nice, and check out our Code of Conduct.
Brian Craycraft is a new contributor. Be nice, and check out our Code of Conduct.
Brian Craycraft is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f208568%2fgame-state-managerfsm-with-in-nextline%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
I've voted to close this question as code that doesn't work, and therefore not eligible for a review. For example,
.getName()setsuserNameas a local variable, then never does anything useful with it.– 200_success
1 hour ago
I understand your reason behind that however the code you are referring to is not the code I am requesting help with. That code was simply there as reference to what I was asking about. I also did not provide my main class where getName() is actually being called and used temporarily while I figure out the GameStates.
– Brian Craycraft
1 hour ago
2
@BrianCraycraft It is often difficult or impossible to give a good critique of partial code. In order to provide helpful comments, we need to see the real context where the code is being used. In your post, you've left out some of the calling context (where does GameState actually get used?) as well as lower-level details (what are the implementations of nameState, deathState and victoryState?) that are important for a helpful critique.
– cariehl
1 hour ago
@cariehl I was simply asking how to implement the user entering a name into the switch provided to transition between states. The critique I mentioned at the end of the post was meant more as a critique on how the
public enum GameStateManagerwas written and general structure of the code. It is its own standalone enum. It is not a class, interface etc,. It is New > Enum. I am currently in the middle of writing this GameStateManager and setting up my states therefore they are not being called or used anywhere as of yet. As mentioned in a closing note deathState and victoryState are not made.– Brian Craycraft
52 mins ago