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???
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.