diff --git a/src/main/java/com/charego/freecellfx/model/Game.java b/src/main/java/com/charego/freecellfx/model/Game.java index f5bea05..0e9ac32 100644 --- a/src/main/java/com/charego/freecellfx/model/Game.java +++ b/src/main/java/com/charego/freecellfx/model/Game.java @@ -1,16 +1,14 @@ package com.charego.freecellfx.model; -import com.charego.freecellfx.model.pile.Cell; -import com.charego.freecellfx.model.pile.Pile; -import com.charego.freecellfx.model.action.MoveAction; -import com.charego.freecellfx.model.pile.Foundation; -import com.charego.freecellfx.model.pile.Tableau; - import java.util.ArrayList; -import java.util.Deque; -import java.util.LinkedList; import java.util.List; +import com.charego.freecellfx.model.action.MoveAction; +import com.charego.freecellfx.model.pile.Cell; +import com.charego.freecellfx.model.pile.Foundation; +import com.charego.freecellfx.model.pile.Pile; +import com.charego.freecellfx.model.pile.Tableau; + public class Game { private List cells = new ArrayList<>(4); private List foundations = new ArrayList<>(4); @@ -149,47 +147,4 @@ public class Game { } } - private static class MoveTracker { - private final Deque nextMoves = new LinkedList<>(); - private final Deque previousMoves = new LinkedList<>(); - - public void clearMoves() { - nextMoves.clear(); - previousMoves.clear(); - } - - public boolean hasLastMove() { - return !previousMoves.isEmpty(); - } - - public MoveAction getLastMove() { - MoveAction lastMove = previousMoves.pop(); - nextMoves.push(lastMove); - return lastMove; - } - - public boolean hasNextMove() { - return !nextMoves.isEmpty(); - } - - public MoveAction getNextMove() { - MoveAction nextMove = nextMoves.pop(); - previousMoves.push(nextMove); - return nextMove; - } - - public void addMove(MoveAction move) { - MoveAction nextMove = nextMoves.peek(); - /* - * if new move differs from saved next move, clear the remaining - * next moves - */ - if (move.equals(nextMove)) { - nextMoves.pop(); - } else { - nextMoves.clear(); - } - previousMoves.push(move); - } - } } diff --git a/src/main/java/com/charego/freecellfx/model/MoveTracker.java b/src/main/java/com/charego/freecellfx/model/MoveTracker.java new file mode 100644 index 0000000..83a3fb3 --- /dev/null +++ b/src/main/java/com/charego/freecellfx/model/MoveTracker.java @@ -0,0 +1,56 @@ +package com.charego.freecellfx.model; + +import java.util.Deque; +import java.util.LinkedList; + +import com.charego.freecellfx.model.action.MoveAction; + +/** + * Records moves to enable redo and undo actions. + */ +public class MoveTracker { + + private final Deque nextMoves = new LinkedList<>(); + private final Deque previousMoves = new LinkedList<>(); + + public void clearMoves() { + nextMoves.clear(); + previousMoves.clear(); + } + + public boolean hasLastMove() { + return !previousMoves.isEmpty(); + } + + public MoveAction getLastMove() { + MoveAction lastMove = previousMoves.pop(); + nextMoves.push(lastMove); + return lastMove; + } + + public boolean hasNextMove() { + return !nextMoves.isEmpty(); + } + + public MoveAction getNextMove() { + MoveAction nextMove = nextMoves.pop(); + previousMoves.push(nextMove); + return nextMove; + } + + public void addMove(MoveAction move) { + MoveAction nextMove = nextMoves.peek(); + + /* + * If there is a saved next move and it differs from the player's move, + * clear the remaining next moves because the paths have diverged. + */ + if (move.equals(nextMove)) { + nextMoves.pop(); + } else { + nextMoves.clear(); + } + previousMoves.push(move); + } + +}