Make MoveTracker its own class
This commit is contained in:
parent
43bf9f9adf
commit
65b27a7335
@ -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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
56
src/main/java/com/charego/freecellfx/model/MoveTracker.java
Normal file
56
src/main/java/com/charego/freecellfx/model/MoveTracker.java
Normal 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);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user