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 !!
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
Code java:
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.
Code java:
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;
}
Re: Help implementing an undo function
Quote:
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.