Modified and updated classes.

This commit is contained in:
Jonathan Turner 2024-02-02 20:25:43 -05:00
parent d15216acf2
commit b7b59d282f
4 changed files with 55 additions and 5 deletions

View File

@ -7,5 +7,6 @@
</content> </content>
<orderEntry type="inheritedJdk" /> <orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" /> <orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="junit" level="project" />
</component> </component>
</module> </module>

View File

@ -1,4 +1,5 @@
package Assignments.A1.managers; package Assignments.A1.managers;
public class PuzzleGenerator { public class BoardGenerator {
} }

View File

@ -1,4 +1,11 @@
package Assignments.A1.models; package Assignments.A1.models;
/**
* @author Jonathan Turner
* @version Spring 2024
*/
public class EightPuzzle { public class EightPuzzle {
} }

View File

@ -26,7 +26,7 @@ public class Piece {
throw new IllegalArgumentException("The piece value is not valid. Valid Range: 1 <= Value <= 8"); throw new IllegalArgumentException("The piece value is not valid. Valid Range: 1 <= Value <= 8");
} }
this.moveTo(loc); this.setLoc(loc);
this.value = value; this.value = value;
} }
@ -39,10 +39,10 @@ public class Piece {
* @param newLoc the new location of the piece. * @param newLoc the new location of the piece.
*/ */
public void moveTo(int newLoc) { public void moveTo(int newLoc) {
if (newLoc < 0 || newLoc > 8) { if (newLoc == this.loc) {
throw new IllegalArgumentException("The new location is not valid. Valid Range: 0 <= Location <= 8."); 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() { public int getValue() {
return this.value; 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);
}
} }