Concept of Building Undo Functionality within an App

49 views
Skip to first unread message

Jonathan Webb

unread,
Jul 15, 2015, 12:34:15 PM7/15/15
to mitappinv...@googlegroups.com

Hi
 
I want to build an undo function into may app but am not quite sure how to tackle this. One way I thought of was to store current values in variables before they are updated and recall them when the undo button is pressed however this seems a little long winded. I've read on other posts about stacking and using lists but can't really find any detailed info on how to actally acheive this or the concept behind it. Can anybody give me a few pointers about the best way to build an undo function?  

Italo

unread,
Jul 15, 2015, 1:36:30 PM7/15/15
to mitappinv...@googlegroups.com
Are you talking about graphics (canvas)?

Abraham Getzler

unread,
Jul 15, 2015, 3:00:19 PM7/15/15
to mitappinv...@googlegroups.com
Generally, you need to keep a representation of a starting state,
like the initial configuration of a chess board for example,
and a history stack (list) of the moves that have been made in the game.
For a chess game, each move would name the piece (or starting position)
and the destination.

So that implies a list of lists for the stack, since any move typically needs
multiple pieces of data to describe it.

This technique needs a routine that can accept a list of moves and a starting
state, and generate a current state from that.

To Undo, remove the latest (last) move from the move history list and rebuild
a new current game board from the original board and the shorter move list.

An alternative technique would be to just store each successive board state
on the history stack.  To Undo, just remove the last entry, and use the new last entry
as the current board.  (This precludes you from being able to show a game log.)


If you want to add a Redo function, I recommend an extra Redo stack, consisting
of everything that has been removed from the History stack since the last move.
To Redo, take the last item off the Redo stack and add it back to the History stack.
To Undo, take the last item off the History stack and add it to the Redo stack.
To make a move, add the new move (or board) to the History stack.

ABG

Jonathan Webb

unread,
Jul 18, 2015, 7:04:31 AM7/18/15
to mitappinv...@googlegroups.com
Hi, I was talking about general undo rather than just canvas, thanks.

Jonathan Webb

unread,
Jul 18, 2015, 7:05:35 AM7/18/15
to mitappinv...@googlegroups.com
Many thanks Abraham. I think I understand the concept now so will have a play ....
Reply all
Reply to author
Forward
0 new messages