Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Help implementing an undo function

  1. #1
    Junior Member
    Join Date
    Feb 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Help implementing an undo function

    Hello all!

    I am creating a free cell game for a java class, and I am having a little bit of trouble implementing a feature to the game. The feature is an undo feature and I am not sure if I have the logic correct on account of having no success with the code.
    What I was thinking of doing is making a copy of the object with the game data and adding it to a stack every time the user makes a move in the game. if the undo feature is called, this current object would be set to what ever is popped off the top of the stack. However I am not sure if this is the correct way to do this, or why I am not having any success implementing it.
    The following is the code where the copy is generated and stored in the stack
    bakersGame - Pastebin.com Case 'F' is what I am dealing with. This is the corresponding class of which it is dependent on. bakersGame - Pastebin.com

    any idea or input would be greatly appreciated thank you !!


  2. #2
    Junior Member
    Join Date
    Feb 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Problem implementing an undo function.

    Hello all,
    I am having a little bit of trouble implementing a function to a free cell game that I created for a java class. I have to make the function revert through all the previous moves made by the user until the start of the game. I am having some trouble though,
    I was thinking of every time a move is made in the game, of making a copy of that object and storing it in a stack, then when the undo function is called i would just set the current object to what ever is popped of the top of the stack.
    Here is a copy of the class, I am dealing with case F in the switch statement, below the switch statement you can see the stack being populated and its corresponding method.
    I can provide the GameBoard class code as well if that helps any help would be greatly appropriated, or ideas perhaps my logic is wrong, but i think its more my implementation then logic. Thanks!!
    JaeDuck
     
      private GameBoard game ;
      private Scanner scan ;
      private Stack<GameBoard> undoStack;
        public GameBoardController(){  
            game = new GameBoard();
            scan = new Scanner(System.in);
            undoStack = new Stack<GameBoard>();
        }
     
        public void startGame(){
            game.populateGameCells();
     
     
     
            game.printGameBoard();
            gameMenu();
        }
     
        public void gameMenu(){
            char choice;
            int  row;
            int  row2;
            int  finalCellRow;
            int  holdingCellRow;
     
     
            System.out.println("What would you like to do?");
            System.out.println("A: Move from stack of cards to another stack of cards\n"
                    + "B: Move Card from Stack to Holding Row\n"
                    + "C: Move Card from Holding Row to Stack of Cards\n"
                    + "D: Move Card from Stacks to Final Row\n" 
                    + "E: Move Card from Holding Row to Final Row\n"
                    + "F: Undo");
     
            choice = scan.next().toUpperCase().charAt(0);
     
     
            try{
            switch (choice) {
     
                case 'A': 
                    System.out.println("Enter Row nubmer to get card from");
                    row = scan.nextInt();
                    System.out.println("Enter Row numer to place card on");
                    row2 = scan.nextInt();
                    game.rowToRow(row2, row);
                    break;
                case 'B':
                    System.out.println("Enter Row nubmer to get card from");
                    row = scan.nextInt();
                    System.out.println("Enter holding cell nubmer to place card on");
                    row2 = scan.nextInt();
                    game.rowToHoldingCell(row2, row);
                    break;
                case 'C':
                    System.out.println("Enter Holding Row nubmer to get card from");
                    row = scan.nextInt();
                    System.out.println("Enter Row of Stack of Card to put card on");
                    row2 = scan.nextInt();
                    game.holdingCellToRow(row2, row);
                    break;
                case 'D':
                    System.out.println("Enter Row nubmer to get card from");
                    row = scan.nextInt();
                    System.out.println("Enter final cell nubmer to place card on");
                    row2 = scan.nextInt();
                    game.rowToFinalCell(row2, row);
                    break;
                case 'E':
                    System.out.println("Enter Holding Row nubmer to get card from");
                    row = scan.nextInt();
                    System.out.println("Enter Final row number to put card on");
                    row2 = scan.nextInt();
                    game.holdingCellToFinalCell(row2, row);
                    break;
                case 'F':
                    undo();
                    break;
     
     
                default: 
                    JOptionPane.showMessageDialog(null, "Letters are required!");
                    break;
     
                }
            }catch(InputMismatchException e){
                 JOptionPane.showMessageDialog(null, "Numbers are required!");
                }
     
            undoStack.push(new GameBoard(game));
     
            game.printGameBoard();
            gameMenu();
     
     
        }
     
     
        public void undo(){
      game = undoStack.pop();
     
    }
     
    }


    --- Update ---

    here is the top of the gameBoard class just in case
    thanks again for any help input or ideas.
    import java.util.*;
    import javax.swing.JOptionPane;
    /**
     * Write a description of class GameBoard here.
     * 
     * @author (your name) 
     * @version (a version number or a date)
     */
    public class GameBoard
    {
        protected ArrayList<Stack<Card>> gameCells = new ArrayList<Stack<Card>>(); 
        protected ArrayList<Stack<Card>> holdingCells = new ArrayList<Stack<Card>>(); 
        protected ArrayList<Stack<Card>> finalCells = new ArrayList<Stack<Card>>(); 
        protected Deck newDeck = new Deck();
        private final int CARDCELLS = 8;
        private final int HFCELLS = 4;
        public GameBoard(){
            for(int i = 0;i<CARDCELLS;i++){
                gameCells.add( new Stack<Card>());
            }
            for(int i = 0;i<HFCELLS;i++){
                holdingCells.add( new Stack<Card>());
                finalCells.add( new Stack<Card>());
            }
        }
        public GameBoard(GameBoard copy){
     
            this.gameCells = copy.gameCells;
            this.holdingCells = copy.holdingCells;
            this.finalCells = copy.finalCells;
            this.newDeck = copy.newDeck;
     
        }
        public void copy(GameBoard copy){
            this.gameCells = copy.gameCells;
            this.holdingCells = copy.holdingCells;
            this.finalCells = copy.finalCells;
            this.newDeck = copy.newDeck;
        }

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,143
    Thanks
    48
    Thanked 2,157 Times in 2,130 Posts

    Default Re: Help implementing an undo function

    I am having a little bit of trouble implementing a function
    Can you explain what happens when the code is executed? What does the code do that is wrong?
    What should the code do?

    The posted code is not complete and wouldn't compile for testing.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Implementing a webproxy
    By arsenewenger in forum Java Networking
    Replies: 5
    Last Post: February 12th, 2013, 10:19 AM
  2. undo delete method
    By Trunk Monkeey in forum Object Oriented Programming
    Replies: 7
    Last Post: February 7th, 2012, 02:49 PM
  3. Disabling undo button in Word.
    By Silversurfer21 in forum Java Theory & Questions
    Replies: 0
    Last Post: September 5th, 2011, 09:03 AM
  4. Java Image Viewer undo function
    By mccaffrey in forum AWT / Java Swing
    Replies: 0
    Last Post: April 21st, 2011, 04:28 PM
  5. [SOLVED] how do I undo an AlphaComposite?
    By gib65 in forum AWT / Java Swing
    Replies: 3
    Last Post: October 21st, 2010, 04:23 PM