Results 1 to 2 of 2
Thread: undo java
- 11-19-2007, 04:00 PM #1Member
- Join Date
- Nov 2007
- Rep Power
Heya al Ive got a JAVA chess program that works in the console area just wanted to know how would I be able to add a undo function that allows the person playing to undo their moves???
Where would I start - ive come across java.swing.undo but not sure????
I cant post any code yet but just want to know am I going along the right lines with using java.swing.undo- bearing in mind this is a console application???
- 11-20-2007, 09:16 AM #2
You could make up a class called Move which stores the information for each move such as:
token: knight, pawn, etc.
row and column moved from
row and column moved to.
And make up a class that keeps/manages these Moves for a player. You could keep them (the Moves) in an ArrayList or a similar array–type structure. Each player would have an instance of this class which tracked their moves. To undo a move would be as simple as removing the last Move from the list, reversing the player's last move using the data in the removed Move instance, and then discarding this Move instance.
It might be enough to keep (only) an ArrayList of Moves for each player or you may want to make up a separate class (as mentioned above), using an instance of it for each player, that keeps the ArrayList and could provide methods for updating the game with the undo activity.
If you did make this second class, say UndoManager, it could be updated from the Game class and could, as part of its undo support, make updates in/to the game class to reflect each undo operation. So a call to your undoManager.remove() would automatically update the game model and its view in the console.
- By christina in forum Advanced JavaReplies: 12Last Post: 01-29-2009, 07:47 AM
- By natel in forum EclipseReplies: 0Last Post: 04-08-2008, 11:01 PM
- By Eranga in forum NetBeansReplies: 0Last Post: 03-31-2008, 05:12 AM
- By Mr tuition in forum AWT / SwingReplies: 1Last Post: 12-09-2007, 12:02 AM