diff --git a/.idea/libraries/junit.xml b/.idea/libraries/junit.xml new file mode 100644 index 0000000..e0e3044 --- /dev/null +++ b/.idea/libraries/junit.xml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/src/Assignments/A1/models/Board.java b/src/Assignments/A1/models/Board.java new file mode 100644 index 0000000..3ec8032 --- /dev/null +++ b/src/Assignments/A1/models/Board.java @@ -0,0 +1,68 @@ +package Assignments.A1.models; + +/** + * This class keeps track of the current state (whether in permutation or not) of the board. + * + * @author Jonathan Turner + * @version Spring 2024 + */ +public class Board { + + private final Piece[] pieces; + + /** + * Default constructor that generates the solved board. + * + * @precondition none + * @postcondition a solved board is created. + */ + public Board() { + this.pieces = new Piece[9]; + for (int index = 1; index <= 9; index++) { + this.pieces[index-1] = new Piece(index-1, index); + } + } + + /** + * Constructor used to create a board with a pre-provided board. + * + * @precondition board.length == 9 + * @postcondition a board is created with given locations + * + * @param board the provided state of the board. + */ + public Board(Piece[] board) { + if (board.length != 9) { + throw new IllegalArgumentException("The board must be a array size of 9."); + } + + this.pieces = board; + } + + + public boolean isTaken(int loc) { + if (invalidLocation(loc)) { + throw new IllegalArgumentException("Invalid location specified. Valid Range: 0 <= Location <= 8"); + } + return (this.pieces[loc].getValue() != 0); + } + + /** + * Returns the array of pieces. + * + * @precondition none + * @postcondition none + * @return the array of pieces. + */ + public Piece[] getPieces() { + return this.pieces; + } + + + /* Private Methods Below. End of Java Docs. */ + + /* Checks if a given location is a valid location. */ + private boolean invalidLocation(int loc) { + return (loc >= 0 && loc <= 8); + } +} diff --git a/src/Assignments/A1/tests/Piece/TestConstructor.java b/src/Assignments/A1/tests/Piece/TestConstructor.java new file mode 100644 index 0000000..9d64122 --- /dev/null +++ b/src/Assignments/A1/tests/Piece/TestConstructor.java @@ -0,0 +1,75 @@ +package Assignments.A1.tests.Piece; + +import static org.junit.Assert.*; +import Assignments.A1.models.Piece; +import org.junit.Test; + +/** + * Tests the constructor for Piece.java. + * + * @author Jonathan Turner + * @version Spring 2024 + */ +public class TestConstructor { + + @Test + public void testZeroValue() { + assertThrows(IllegalArgumentException.class, () -> { + new Piece(5, 0); + }); + } + + @Test + public void testOneValue() { + Piece test = new Piece(5, 1); + assertEquals(1, test.getValue()); + } + + @Test + public void testEightValue() { + Piece test = new Piece(5, 8); + assertEquals(8, test.getValue()); + } + + @Test + public void testNineValue() { + assertThrows(IllegalArgumentException.class, () -> { + new Piece(5, 9); + }); + } + + @Test + public void testNegativeLoc() { + assertThrows(IllegalArgumentException.class, () -> { + new Piece(-1, 5); + }); + } + + @Test + public void testZeroLoc() { + Piece test = new Piece(0, 5); + assertEquals(0, test.getLoc()); + } + + @Test + public void testEightLoc() { + Piece test = new Piece(8, 5); + assertEquals(8, test.getLoc()); + } + + @Test + public void testNineLoc() { + assertThrows(IllegalArgumentException.class, () -> { + new Piece(9, 5); + }); + } + + + @Test + public void validParams() { + Piece test = new Piece(3, 6); + assertEquals(3, test.getLoc()); + assertEquals(6, test.getValue()); + } + +} diff --git a/src/Assignments/A1/tests/Piece/TestMoveTo.java b/src/Assignments/A1/tests/Piece/TestMoveTo.java new file mode 100644 index 0000000..678a6ea --- /dev/null +++ b/src/Assignments/A1/tests/Piece/TestMoveTo.java @@ -0,0 +1,60 @@ +package Assignments.A1.tests.Piece; + +import static org.junit.Assert.*; +import Assignments.A1.models.Piece; +import org.junit.Test; + +/** + * Tests the moveTo() for Piece.java. + * + * @author Jonathan Turner + * @version Spring 2024 + */ +public class TestMoveTo { + + @Test + public void testNegativeMove() { + Piece test = new Piece(3, 5); + assertThrows(IllegalArgumentException.class, () -> { + test.moveTo(-1); + }); + } + + @Test + public void testZeroMove() { + Piece test = new Piece(3, 5); + test.moveTo(0); + assertEquals(0, test.getLoc()); + } + + @Test + public void testEightMove() { + Piece test = new Piece(5, 5); + test.moveTo(8); + assertEquals(8, test.getLoc()); + } + + @Test + public void testNineMove() { + Piece test = new Piece(3, 5); + assertThrows(IllegalArgumentException.class, () -> { + test.moveTo(9); + }); + } + + @Test + public void MoveToSameLocation() { + Piece test = new Piece(3, 5); + assertThrows(IllegalArgumentException.class, () -> { + test.moveTo(3); + }); + } + + @Test + public void ValidMove() { + Piece test = new Piece(3, 5); + test.moveTo(4); + assertEquals(4, test.getLoc()); + } + +}