Make MoveTracker its own class

This commit is contained in:
Charles Gould 2015-06-23 15:20:24 -04:00
parent 43bf9f9adf
commit 65b27a7335
2 changed files with 62 additions and 51 deletions

View File

@ -1,16 +1,14 @@
package com.charego.freecellfx.model; 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.ArrayList;
import java.util.Deque;
import java.util.LinkedList;
import java.util.List; 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 { public class Game {
private List<Cell> cells = new ArrayList<>(4); private List<Cell> cells = new ArrayList<>(4);
private List<Foundation> foundations = new ArrayList<>(4); private List<Foundation> foundations = new ArrayList<>(4);
@ -149,47 +147,4 @@ public class Game {
} }
} }
private static class MoveTracker {
private final Deque<MoveAction> nextMoves = new LinkedList<>();
private final Deque<MoveAction> 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);
}
}
} }

View File

@ -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<MoveAction> nextMoves = new LinkedList<>();
private final Deque<MoveAction> 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);
}
}