From b7b59d282f0b68f785221489170e4862b90d64b3 Mon Sep 17 00:00:00 2001 From: Jonathan Turner Date: Fri, 2 Feb 2024 20:25:43 -0500 Subject: [PATCH] Modified and updated classes. --- CS3642-Artificial_Intelligence.iml | 1 + ...zzleGenerator.java => BoardGenerator.java} | 3 +- src/Assignments/A1/models/EightPuzzle.java | 7 +++ src/Assignments/A1/models/Piece.java | 49 +++++++++++++++++-- 4 files changed, 55 insertions(+), 5 deletions(-) rename src/Assignments/A1/managers/{PuzzleGenerator.java => BoardGenerator.java} (53%) diff --git a/CS3642-Artificial_Intelligence.iml b/CS3642-Artificial_Intelligence.iml index c90834f..8e1b626 100644 --- a/CS3642-Artificial_Intelligence.iml +++ b/CS3642-Artificial_Intelligence.iml @@ -7,5 +7,6 @@ + \ No newline at end of file diff --git a/src/Assignments/A1/managers/PuzzleGenerator.java b/src/Assignments/A1/managers/BoardGenerator.java similarity index 53% rename from src/Assignments/A1/managers/PuzzleGenerator.java rename to src/Assignments/A1/managers/BoardGenerator.java index 6624f4a..8d9c8d7 100644 --- a/src/Assignments/A1/managers/PuzzleGenerator.java +++ b/src/Assignments/A1/managers/BoardGenerator.java @@ -1,4 +1,5 @@ package Assignments.A1.managers; -public class PuzzleGenerator { +public class BoardGenerator { + } diff --git a/src/Assignments/A1/models/EightPuzzle.java b/src/Assignments/A1/models/EightPuzzle.java index 92feb55..fb0b27a 100644 --- a/src/Assignments/A1/models/EightPuzzle.java +++ b/src/Assignments/A1/models/EightPuzzle.java @@ -1,4 +1,11 @@ package Assignments.A1.models; +/** + * @author Jonathan Turner + * @version Spring 2024 + */ public class EightPuzzle { + + + } diff --git a/src/Assignments/A1/models/Piece.java b/src/Assignments/A1/models/Piece.java index 2932c1d..999702b 100644 --- a/src/Assignments/A1/models/Piece.java +++ b/src/Assignments/A1/models/Piece.java @@ -26,7 +26,7 @@ public class Piece { throw new IllegalArgumentException("The piece value is not valid. Valid Range: 1 <= Value <= 8"); } - this.moveTo(loc); + this.setLoc(loc); this.value = value; } @@ -39,10 +39,10 @@ public class Piece { * @param newLoc the new location of the piece. */ public void moveTo(int newLoc) { - if (newLoc < 0 || newLoc > 8) { - throw new IllegalArgumentException("The new location is not valid. Valid Range: 0 <= Location <= 8."); + if (newLoc == this.loc) { + throw new IllegalArgumentException("The new location cannot be the same as the old location."); } - this.loc = newLoc; + this.setLoc(newLoc); } /** @@ -68,4 +68,45 @@ public class Piece { public int getValue() { return this.value; } + + /** + * Checks if the move to a location is valid. + * + * @precondition none + * @postcondition none + * + * @param desiredLoc the location being checked if move is valid. + * @param board the current board layout. + * @return if the move is valid, True + * else, False + */ + public boolean validMove(int desiredLoc, Board board) { + if (desiredLoc != this.loc) { + return false; + } + if (this.invalidLocation(desiredLoc)) { + return false; + } + // Checks if the desiredLoc is above, to the left/right, or below. + if (desiredLoc != this.loc-3 && desiredLoc != this.loc-1 + && desiredLoc != this.loc+1 && desiredLoc == this.loc+3) { + return false; + } + return board.isTaken(desiredLoc); + } + + /* Private Methods Below. End of Java Docs. */ + + /* Sets the location for both Moving and Initialization. */ + private void setLoc(int loc) { + if (this.invalidLocation(loc)) { + throw new IllegalArgumentException("The new location is not valid. Valid Range: 0 <= Location <= 8."); + } + this.loc = loc; + } + + /* Checks if a given location is a valid location. */ + private boolean invalidLocation(int loc) { + return (loc >= 0 && loc <= 8); + } }