From: Erik V. <eri...@hc...> - 2006-07-19 22:31:58
|
I have implemented a very basic Undo facility, currently for stock trading only. The hard part, as expected, is handling the state changes, which are numerous. Each different way of holding some state requires its own Move subclass. For instance, in a stock round, players may not buy shares of companies they have sold before. This state is held in a HashMap of HashMaps (see StockRound.playersThatSoldThisRound), and to manage do and undo of this type of state I need a specific Move subclass, which I have named DoubleMapChange. (I use class names like XxxMove when physical items change location, and XxxChange for state changes). The decision on which level to insert the Action.add() calls is also not always easy. It's mostly done on a pretty low level, sometimes in code that is reused in other actions that are not yet undoable. To cope with this problem, I have implemented Action.finish() in such a way, that is an Action has been opened before (with Action.start()), the moves are added to the action, and executed as a whole (with Action.finish()); in this case Undo is possible. If no Action is open, the move is executed immediately and cannot be undone. This allows for a gradual implementation of Undo. Redo is not yet done, but can be added easily. Beware: not all side effects of share trading are covered yet. Not yet undone are: - Company start (i.e. setting the par price), - Company flotation, - Player turn end (to cope with this, I now empty the undo stack at each turn end). - Some cases of presidency change. - Share price movements (except on selling shares). The Status Window now has an extra Undo menu option, with is greyed out if the Undo stack is empty. I have also 'normalised' a few Log messages around sell buying and selling, using the parameter placeholders (like {0}, {1} etc.) added recently. This is just a start. Erik Vos |