0

Java beginner, who would appreciate feedback on how best to approach a problem which I'm working on in isolation.....

Trying to build a basic game, where a player moves around a grid/board. Have up/down/left/right methods (e.g. public void playerMovesUp() etc), and need to implement undo/redo move functionality, so a player can retrace x number of steps, and then retake x number at any point during game play.

Have been looking into ways to do this i.e. to record the order of the up/down/left/right method calls, and then re-call both in reverse order (undo) and then redo.

A stack, or queue collection seems logical....and I have also looked into the java stack memory storing the order of function calls, and have just read about java.lang.Thread.getStackTrace().

Current thoughts.....If I isolate those four methods into one thread, then I could use java.lang.Thread.getStackTrace() to return an array, and call getMethodName(), to then somehow call applicable methods in order?

Also, is it possible to store a queue/stack of method calls?

I feel like I'm on the right path, but the last suggestion seems a bit messy/illogical! Any pointers/advice would be appreciated.

javapalava
  • 711
  • 1
  • 6
  • 15
  • Java `Stack` - https://docs.oracle.com/javase/7/docs/api/java/util/Stack.html – brso05 Apr 11 '16 at 13:48
  • usually, one would design the actions in different Classes using the Command-Pattern to implement undo. (see http://stackoverflow.com/questions/49755/design-pattern-for-undo-engine) – Raphael Roth Apr 11 '16 at 13:50
  • Store your movements in a `Stack` i.e. "right" "left" "up" "down"...then when you want to traverse backwards just grab the top element of the `Stack` and do the opposite. `if(topElement.equalsIgnoreCase("right")){moveLeft();}`. – brso05 Apr 11 '16 at 13:50

2 Answers2

2

Don't use the stack like that. That's not what this if for. Just keep a list of steps you took and retrace your steps using that list.

mahieus
  • 580
  • 3
  • 17
1

I think you cannot use the Stack for this. You have to keep track of your method calls yourself. And no, you cannot store "method calls" into a Collection.

Instead, it would be easier to design classes for your actions, where each class implements a do and a undo. The instances of these classes can then be stored in a Stack or Queue. If the user wants to undo his actions, you can process it backwards and calling undo on all elements. This pattern is known as the Command Pattern (see eg.g. Design Pattern for Undo Engine)

Community
  • 1
  • 1
Raphael Roth
  • 26,751
  • 15
  • 88
  • 145
  • Thanks Raphael. If I created a new class for each action (up/down etc), and it generates an object each time the player moves one grid space, wouldn't it be a lot more inefficient than say, just adding a string each time the player moves to my stack? So I have a stack of "left", "up", "right", "up" etc. Otherwise I could end up with a vast amount of objects in my stack as there are no limitations on the number of times a player can move. What do you think? – javapalava Apr 11 '16 at 14:23