From 489ee0a50c08e217b33c049818e200e4b582d42e Mon Sep 17 00:00:00 2001 From: Jonathan Turner Date: Sat, 3 Feb 2024 15:25:22 -0500 Subject: [PATCH] Added functionality to keep track of moves. --- src/Assignments/A1/models/BoardGenerator.java | 2 +- src/Assignments/A1/models/EightPuzzle.java | 5 +- src/Assignments/A1/models/Move.java | 49 +++++++++++++++++++ src/Assignments/A1/models/Pair.java | 29 +++++++++++ 4 files changed, 80 insertions(+), 5 deletions(-) create mode 100644 src/Assignments/A1/models/Move.java create mode 100644 src/Assignments/A1/models/Pair.java diff --git a/src/Assignments/A1/models/BoardGenerator.java b/src/Assignments/A1/models/BoardGenerator.java index e9ee9e0..47bf92f 100644 --- a/src/Assignments/A1/models/BoardGenerator.java +++ b/src/Assignments/A1/models/BoardGenerator.java @@ -33,7 +33,7 @@ public class BoardGenerator { } - // Checks if the board is solveable. + // Checks if the board is solvable. Board generated = new Board(pieces); if (isSolvable(generated)) { return generated; diff --git a/src/Assignments/A1/models/EightPuzzle.java b/src/Assignments/A1/models/EightPuzzle.java index c455ecf..053c6ef 100644 --- a/src/Assignments/A1/models/EightPuzzle.java +++ b/src/Assignments/A1/models/EightPuzzle.java @@ -1,7 +1,6 @@ package Assignments.A1.models; -import java.util.ArrayList; -import java.util.List; +import java.util.*; /** * @author Jonathan Turner @@ -9,8 +8,6 @@ import java.util.List; */ public class EightPuzzle { - private List boards = new ArrayList<>(); - } diff --git a/src/Assignments/A1/models/Move.java b/src/Assignments/A1/models/Move.java new file mode 100644 index 0000000..654ff55 --- /dev/null +++ b/src/Assignments/A1/models/Move.java @@ -0,0 +1,49 @@ +package Assignments.A1.models; + +/** + * Used to keep track of moves in either a stack or queues. + * + * @author Jonathan Turner + * @version Spring 2024 + */ +public class Move { + + private Board board; + private final Pair points; + private boolean used; + + /** + * Creates a new move with the given points. + * + * @precondition none + * @postcondition a new move is created. + * @param firstpoint the first point + * @param secondpoint the second point + */ + public Move(int firstpoint, int secondpoint) { + this.points = new Pair<>(firstpoint, secondpoint); + } + + /** + * Checks if a move was used. + * @precondition none + * @postcondition none + * @return whether the move was used. + */ + public boolean used() { + return used; + } + + /** + * Returns the points for the move and marks it as used. + * + * @precondition none + * @postcondition used == true + * @return the pair of points. + */ + public Pair traverse() { + used = true; + return points; + } +} + diff --git a/src/Assignments/A1/models/Pair.java b/src/Assignments/A1/models/Pair.java new file mode 100644 index 0000000..93e7f0a --- /dev/null +++ b/src/Assignments/A1/models/Pair.java @@ -0,0 +1,29 @@ +package Assignments.A1.models; + +/** + * Used to pair two points for moves. + * @author Jonathan Turner + * @version Spring 2024 + * @param the type of the pair. + */ +public class Pair { + + /* the first and second points */ + public E first; + public E second; + + /** + * Creates and sets the first and second point of the pair. + * + * @precondition none + * @postcondition a pair is created. + * + * @param first the first point of the pair + * @param second the second point of the pair + */ + public Pair(E first, E second) { + this.first = first; + this.second = second; + } + +}