Results 1 to 2 of 2

Thread: undo java

  1. #1
    new214 is offline Member
    Join Date
    Nov 2007
    Posts
    1
    Rep Power
    0

    Question undo java

    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???
    :confused:

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    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.

Similar Threads

  1. add an undo listener to a Jtable
    By christina in forum Advanced Java
    Replies: 12
    Last Post: 01-29-2009, 08:47 AM
  2. Undo Folder Drag and Drop
    By natel in forum Eclipse
    Replies: 0
    Last Post: 04-09-2008, 12:01 AM
  3. Undo shortcut malfunctioning.
    By Eranga in forum NetBeans
    Replies: 0
    Last Post: 03-31-2008, 06:12 AM
  4. java copy paste cut and undo functions
    By Mr tuition in forum AWT / Swing
    Replies: 1
    Last Post: 12-09-2007, 01:02 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •