patternjavaMinor
Adding buttons for a text adventure game
Viewed 0 times
textadventureaddingbuttonsgamefor
Problem
The game will be combination of text adventure (navigate through buttons), RPG, sandbox, strategy and turn-based game where the main task of player is to explore the game's world and survive (since I cannot classify my game with one genre just yet, I'm still experimenting).
The interaction between player and game, as I said, will be based on button clicks. The player will be able to go to different locations using tabs, check some statistics ("Attributes") and perform actions in each location to advance in the game.
I've got a code that compiles, and it works the way I want. However, I wondered if I can do anything to make process of programming-in more Actions easier and 'cheaper' in terms of time&effort I have to put into each button, since I want to have a LOT of actions available for player.
Currently I am using this method below to display Actions to player and I'm calling it on the start of each turn to refresh the whole layout and display Actions that are available to player (based on his Resources and his Achievements so far).
The code is much longer but it is pretty similar with each next action in the 'database' of sorts.
Do I have any options to reduce the size of the code needed to make another button (considering I have a few
Because as I add more actions (and I plan to have way over 100 actions at some point, of course not all will be displayed to player) the code is getting really long and really littered due to the space being taken JUST by this single method (
```
public void displayactions(){
setContentView(R.layout.main_game);
displaytabs();
displayturns();
textView = (TextView) findViewById(R.id.workers);
switch(people){
case 1:
textView.setText(workers+"/"+people+" worker available.");
break;
default:
textView.setText(workers+"/"+people+" work
The interaction between player and game, as I said, will be based on button clicks. The player will be able to go to different locations using tabs, check some statistics ("Attributes") and perform actions in each location to advance in the game.
I've got a code that compiles, and it works the way I want. However, I wondered if I can do anything to make process of programming-in more Actions easier and 'cheaper' in terms of time&effort I have to put into each button, since I want to have a LOT of actions available for player.
Currently I am using this method below to display Actions to player and I'm calling it on the start of each turn to refresh the whole layout and display Actions that are available to player (based on his Resources and his Achievements so far).
The code is much longer but it is pretty similar with each next action in the 'database' of sorts.
Do I have any options to reduce the size of the code needed to make another button (considering I have a few
if statements here and there), and/or reduce time I have to put into adding more actions into the game?Because as I add more actions (and I plan to have way over 100 actions at some point, of course not all will be displayed to player) the code is getting really long and really littered due to the space being taken JUST by this single method (
displayactivity()) in my MainActivity.java.```
public void displayactions(){
setContentView(R.layout.main_game);
displaytabs();
displayturns();
textView = (TextView) findViewById(R.id.workers);
switch(people){
case 1:
textView.setText(workers+"/"+people+" worker available.");
break;
default:
textView.setText(workers+"/"+people+" work
Solution
When reading your code, the Strategy Pattern alarm goes off in my head. Before that however, I have some other remarks.
-
Indentation. If you are using Eclipse, please select your code and press Ctrl + I to indent your code properly. Please read up on indentation in the Java Coding Conventions
-
Spacing. This can be seen as a very minor thing, but when you think about it it makes sense that you should be consistent in the usage of spacing around braces; in Java, it is convention to always use a space before a
-
As stated by Jeff Vanzella in a comment to your question,
-
Duplicate as small strings as possible. Instead of using two separate long strings, use one string for
Now, about that strategy pattern...
(This is only an example on how to do it, there are many other ways. Use the things that fits you. The important thing is that you understand the overall structure of what I am doing below)
Something that can improve the cleanliness of your code is to put the game variables in one object of a
Then create a
Then create a
Create some implementations of
Initialize the list, and add an instance of
Loop through the list, and create buttons for the list - and call the
I hope this will get you started in the right direction!
-
Indentation. If you are using Eclipse, please select your code and press Ctrl + I to indent your code properly. Please read up on indentation in the Java Coding Conventions
-
Spacing. This can be seen as a very minor thing, but when you think about it it makes sense that you should be consistent in the usage of spacing around braces; in Java, it is convention to always use a space before a
{.-
As stated by Jeff Vanzella in a comment to your question,
if(true) { is an unnecessary condition. If you use a different block to separate variable scoping to avoid variable naming collisions, you can simply use { }-bracers without an if-condition. Or you might want to extract a method that is doing that part. Or, you might want to remove the extra block entirely. (without removing the code, of course)-
Duplicate as small strings as possible. Instead of using two separate long strings, use one string for
portion and one for portions. One string for worker and one for workers. Use Android String Resources, which even supports plurals, instead of hard-coding strings. It is quite neat to gather all strings in one place, and it makes internationalization so much easier.Now, about that strategy pattern...
(This is only an example on how to do it, there are many other ways. Use the things that fits you. The important thing is that you understand the overall structure of what I am doing below)
Something that can improve the cleanliness of your code is to put the game variables in one object of a
Game class; this could contain variables such as food, workers, materials and so on.Then create a
GameAction interface.public interface GameAction {
// You might not want all these methods, perhaps you only want one method for returning a Button
// and one for updating the Button status or something similar. Use what fits you.
boolean isAllowed(Game game);
String getText(Game game);
void execute(Game game);
}Then create a
List actionList;Create some implementations of
GameAction (possibly one for each action, or some actions might be possible to group together).public class CollectBranchesAction implements GameAction {
@Override
public boolean isAllowed(Game game) {
// This action is allowed if we have at least 1 food and at least 1 worker.
return game.getFood() > 0 && game.getWorkers() > 0;
}
@Override
public String getText(Game game) {
return "Collect wood | Cost: 1 [" + game.getWorkers() + "] Worker & 1 [" + game.getFood() + "] Food | Effect: +2-5 Materials";
}
@Override
public void execute(Game game) {
game.changeFood(-1);
game.changeWorkers(-1);
int found = getrandom(2, 5);
game.changeMaterials(found);
game.addText("There are lots of branches to collect in the forest. You find "+found+" materials.\n");
}
}Initialize the list, and add an instance of
CollectBranchesAction to the list:List actionList = new ArrayList();
actionList.add(new CollectBranchesAction());Loop through the list, and create buttons for the list - and call the
GameAction methods to determine the state of the button.for (final GameAction action : actionList) {
Button button = new Button(this);
button.setText(action.getText(game));
button.setEnabled(action.isAllowed(game));
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
action.execute(game);
}
});
layoutmain1.addView(button);
}I hope this will get you started in the right direction!
Code Snippets
public interface GameAction {
// You might not want all these methods, perhaps you only want one method for returning a Button
// and one for updating the Button status or something similar. Use what fits you.
boolean isAllowed(Game game);
String getText(Game game);
void execute(Game game);
}public class CollectBranchesAction implements GameAction {
@Override
public boolean isAllowed(Game game) {
// This action is allowed if we have at least 1 food and at least 1 worker.
return game.getFood() > 0 && game.getWorkers() > 0;
}
@Override
public String getText(Game game) {
return "Collect wood | Cost: 1 [" + game.getWorkers() + "] Worker & 1 [" + game.getFood() + "] Food | Effect: +2-5 Materials";
}
@Override
public void execute(Game game) {
game.changeFood(-1);
game.changeWorkers(-1);
int found = getrandom(2, 5);
game.changeMaterials(found);
game.addText("There are lots of branches to collect in the forest. You find "+found+" materials.\n");
}
}List<GameAction> actionList = new ArrayList<GameAction>();
actionList.add(new CollectBranchesAction());for (final GameAction action : actionList) {
Button button = new Button(this);
button.setText(action.getText(game));
button.setEnabled(action.isAllowed(game));
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
action.execute(game);
}
});
layoutmain1.addView(button);
}Context
StackExchange Code Review Q#36732, answer score: 4
Revisions (0)
No revisions yet.