From 8ecead1b37fed2b299ecdcdc5f91f4b47fb0725d Mon Sep 17 00:00:00 2001 From: Jonathan Turner Date: Sun, 4 Feb 2024 15:55:12 -0500 Subject: [PATCH] Add the ability to get a list of possible moves on the board. --- src/Assignments/A1/models/Board.java | 30 +++++++++++++++++++ src/Assignments/A1/models/EightPuzzle.java | 13 -------- .../A1/solving_algorithms/DFS.java | 3 +- 3 files changed, 31 insertions(+), 15 deletions(-) delete mode 100644 src/Assignments/A1/models/EightPuzzle.java diff --git a/src/Assignments/A1/models/Board.java b/src/Assignments/A1/models/Board.java index 90d3114..6358577 100644 --- a/src/Assignments/A1/models/Board.java +++ b/src/Assignments/A1/models/Board.java @@ -1,6 +1,8 @@ package Assignments.A1.models; +import java.util.ArrayList; import java.util.Arrays; +import java.util.List; /** * This class keeps track of the current state (whether in permutation or not) of the board. @@ -149,6 +151,34 @@ public class Board { return ordered; } + /** + * Adds all the possible moves the state can make based off the open space. + * + * @precondition none + * @postcondition none + * @return the list of possible moves. + */ + public List getMoves() { + List moves = new ArrayList<>(); + int space = getOpenLocation(); + int row = space / 3; + int column = space % 3; + + if (row > 0) { + moves.add(new Move(space, space-1)); + } + if (row < 2) { + moves.add(new Move(space, space+1)); + } + if (column > 0) { + moves.add(new Move(space, space-3)); + } + if (column < 2) { + moves.add(new Move(space, space+3)); + } + return moves; + } + /** * Hashes the board by adding the pieces to an array and hashes the array. * diff --git a/src/Assignments/A1/models/EightPuzzle.java b/src/Assignments/A1/models/EightPuzzle.java deleted file mode 100644 index 053c6ef..0000000 --- a/src/Assignments/A1/models/EightPuzzle.java +++ /dev/null @@ -1,13 +0,0 @@ -package Assignments.A1.models; - -import java.util.*; - -/** - * @author Jonathan Turner - * @version Spring 2024 - */ -public class EightPuzzle { - - - -} diff --git a/src/Assignments/A1/solving_algorithms/DFS.java b/src/Assignments/A1/solving_algorithms/DFS.java index 1b39c7d..0c65e84 100644 --- a/src/Assignments/A1/solving_algorithms/DFS.java +++ b/src/Assignments/A1/solving_algorithms/DFS.java @@ -12,8 +12,7 @@ public class DFS { private HashSet visited = new HashSet<>(); private HashMap> moves = new HashMap<>(); - public void dfs(Board currentState) { - + public void dfs(Board root) { }