Question
Tests: /* * compiling: * javac -cp .:someJunitVersion.jar Thisfile.java * * running: * java -cp .:someJunitVersion.jar Thisfile * */ import org.junit.*; // Rule, Test import
Tests:
/* * compiling: * javac -cp .:someJunitVersion.jar Thisfile.java * * running: * java -cp .:someJunitVersion.jar Thisfile * */ import org.junit.*; // Rule, Test import static org.junit.Assert.*; import java.util.*;
import org.junit.rules.Timeout;
public class BoardTests { public static void main(String args[]){ org.junit.runner.JUnitCore.main("BoardTests"); } // 1 second max per method tested @Rule public Timeout globalTimeout = Timeout.seconds(1); /* SUMMARY OF TEST BOARDS:
board1: 4x4, 1x2, gap #16. board2: 4x4, 3x4, gap #16. board3: 4x4, 4x4, gap #10. board4: 7x4, 1x2, gap #28. board5: 3x8, 1x2, gap #24. board6: 3x3, 1x2, gap # 5. board7: 4x4, 1x2, gap # 7. (standard board with different gap) scramble0: 4x4, 1x2, #16. gap is in the interior. scramble1: 4x4, 1x2, #16. gap is on top edge. scramble2: 4x4, 1x2, #16. gap is on left edge. scramble3: 4x4, 1x2, #16. gap is on right edge. scramble4: 4x4, 1x2, #16. gap is on bottom edge. scramble5: 4x4, 1x2, #16. gap is in upper-left corner. scramble6: 4x4, 1x2, #16. gap is in upper-right corner. scramble7: 4x4, 1x2, #16. gap is in lower-left corner. scramble8: 4x4, 1x2, #16. gap is in lower-right corner.
*/ // tiles of "standard" board: 4x4, tiles are 1x2, gap #16. public static Board board1() { return new Board(getTiles1(),getOpts1()); } public static BoardOptions getOpts1(){ return new BoardOptions(); } public static Tile[][] getTiles1(){ return new Tile[][] { { new Tile(new String[]{"1 "},1), new Tile(new String[]{"2 "},2), new Tile(new String[]{"3 "},3), new Tile(new String[]{"4 "},4), }, { new Tile(new String[]{"5 "},5), new Tile(new String[]{"6 "},6), new Tile(new String[]{"7 "},7), new Tile(new String[]{"8 "},8), }, { new Tile(new String[]{"9 "},9), new Tile(new String[]{"10"},10), new Tile(new String[]{"11"},11), new Tile(new String[]{"12"},12), }, { new Tile(new String[]{"13"},13), new Tile(new String[]{"14"},14), new Tile(new String[]{"15"},15), new Tile(new String[]{" "},16), } }; } // 4x4, 3x4, gap#16. public static Board board2() { return new Board(getTiles2(),getOpts2()); } public static BoardOptions getOpts2(){ return new BoardOptions().withTileHeight(3).withTileWidth(4); } public static Tile[][] getTiles2(){ return new Tile[][] { { new Tile(new String[]{" "," 1 "," "},1), new Tile(new String[]{" "," 2 "," "},2), new Tile(new String[]{" "," 3 "," "},3), new Tile(new String[]{" "," 4 "," "},4), }, { new Tile(new String[]{" "," 5 "," "},5), new Tile(new String[]{" "," 6 "," "},6), new Tile(new String[]{" "," 7 "," "},7), new Tile(new String[]{" "," 8 "," "},8), }, { new Tile(new String[]{" "," 9 "," "},9), new Tile(new String[]{" "," 10 "," "},10), new Tile(new String[]{" "," 11 "," "},11), new Tile(new String[]{" "," 12 "," "},12), }, { new Tile(new String[]{" "," 13 "," "},13), new Tile(new String[]{" "," 14 "," "},14), new Tile(new String[]{" "," 15 "," "},15), new Tile(new String[]{" "," "," "},16), } }; } // 4x4, 4x4, gap #10. public static Board board3() { return new Board(getTiles3(),getOpts3()); } public static BoardOptions getOpts3(){ return new BoardOptions().withTileHeight(4).withTileWidth(4).withGapID(10); } public static Tile[][] getTiles3(){ return new Tile[][] { { new Tile(new String[]{" "," 1 "," "," "},1), new Tile(new String[]{" "," 2 "," "," "},2), new Tile(new String[]{" "," 3 "," "," "},3), new Tile(new String[]{" "," 4 "," "," "},4), }, { new Tile(new String[]{" "," 5 "," "," "},5), new Tile(new String[]{" "," 6 "," "," "},6), new Tile(new String[]{" "," 7 "," "," "},7), new Tile(new String[]{" "," 8 "," "," "},8), }, { new Tile(new String[]{" "," 9 "," "," "},9), new Tile(new String[]{" "," "," "," "},10), new Tile(new String[]{" "," 11 "," "," "},11), new Tile(new String[]{" "," 12 "," "," "},12), }, { new Tile(new String[]{" "," 13 "," "," "},13), new Tile(new String[]{" "," 14 "," "," "},14), new Tile(new String[]{" "," 15 "," "," "},15), new Tile(new String[]{" "," 16 "," "," "},16), } }; } // 7x4, 1x2, gap=28. public static Board board4() { return new Board(getTiles4(),getOpts4()); } public static BoardOptions getOpts4(){ return new BoardOptions().withNumRows(7).withGapID(28); } public static Tile[][] getTiles4(){ return new Tile[][] { { new Tile(new String[]{"1 "},1), new Tile(new String[]{"2 "},2), new Tile(new String[]{"3 "},3), new Tile(new String[]{"4 "},4), }, { new Tile(new String[]{"5 "},5), new Tile(new String[]{"6 "},6), new Tile(new String[]{"7 "},7), new Tile(new String[]{"8 "},8), }, { new Tile(new String[]{"9 "}, 9), new Tile(new String[]{"10"},10), new Tile(new String[]{"11"},11), new Tile(new String[]{"12"},12), }, { new Tile(new String[]{"13"},13), new Tile(new String[]{"14"},14), new Tile(new String[]{"15"},15), new Tile(new String[]{"16"},16), }, { new Tile(new String[]{"17"},17), new Tile(new String[]{"18"},18), new Tile(new String[]{"19"},19), new Tile(new String[]{"20"},20), }, { new Tile(new String[]{"21"},21), new Tile(new String[]{"22"},22), new Tile(new String[]{"23"},23), new Tile(new String[]{"24"},24), }, { new Tile(new String[]{"25"},25), new Tile(new String[]{"26"},26), new Tile(new String[]{"27"},27), new Tile(new String[]{" "},28), } }; } // 3x8, 1x2, gap #24. public static Board board5() { return new Board(getTiles5(),getOpts5()); } public static BoardOptions getOpts5(){ return new BoardOptions().withNumRows(3).withNumCols(8).withGapID(24); } public static Tile[][] getTiles5(){ return new Tile[][] { { new Tile(new String[]{"1 "},1), new Tile(new String[]{"2 "},2), new Tile(new String[]{"3 "},3), new Tile(new String[]{"4 "},4), new Tile(new String[]{"5 "},5), new Tile(new String[]{"6 "},6), new Tile(new String[]{"7 "},7), new Tile(new String[]{"8 "},8), }, { new Tile(new String[]{"9 "}, 9), new Tile(new String[]{"10"},10), new Tile(new String[]{"11"},11), new Tile(new String[]{"12"},12), new Tile(new String[]{"13"},13), new Tile(new String[]{"14"},14), new Tile(new String[]{"15"},15), new Tile(new String[]{"16"},16), }, { new Tile(new String[]{"17"},17), new Tile(new String[]{"18"},18), new Tile(new String[]{"19"},19), new Tile(new String[]{"20"},20), new Tile(new String[]{"21"},21), new Tile(new String[]{"22"},22), new Tile(new String[]{"23"},23), new Tile(new String[]{" "},24), } }; } // 3x3, 1x2, gap #5. public static Board board6() { return new Board(getTiles6(),getOpts6()); } public static BoardOptions getOpts6(){ return new BoardOptions().withNumRows(3).withNumCols(3).withGapID(5); } public static Tile[][] getTiles6(){ return new Tile[][] { { new Tile(new String[]{"1 "},1), new Tile(new String[]{"2 "},2), new Tile(new String[]{"3 "},3), }, { new Tile(new String[]{"4 "},4), new Tile(new String[]{" "},5), new Tile(new String[]{"6 "},6), }, { new Tile(new String[]{"7 "},7), new Tile(new String[]{"8 "},8), new Tile(new String[]{"9 "}, 9), } }; } // tiles of "standard" board: 4x4, tiles are 1x2, but gap is in the interior @ #7. public static Board board7() { return new Board(getTiles7(),getOpts7()); } public static BoardOptions getOpts7(){ return new BoardOptions().withGapID(7); } public static Tile[][] getTiles7(){ return new Tile[][] { { new Tile(new String[]{"1 "},1), new Tile(new String[]{"2 "},2), new Tile(new String[]{"3 "},3), new Tile(new String[]{"4 "},4), }, { new Tile(new String[]{"5 "},5), new Tile(new String[]{"6 "},6), new Tile(new String[]{" "},7), new Tile(new String[]{"8 "},8), }, { new Tile(new String[]{"9 "},9), new Tile(new String[]{"10"},10), new Tile(new String[]{"11"},11), new Tile(new String[]{"12"},12), }, { new Tile(new String[]{"13"},13), new Tile(new String[]{"14"},14), new Tile(new String[]{"15"},15), new Tile(new String[]{"16"},16), } }; } // 4x4, 1x2, #16. gap is in the interior. public static Board scramble0() { return new Board(getScrambleTiles0(),getScrambleOpts0()); } public static BoardOptions getScrambleOpts0(){ return new BoardOptions(); } public static Tile[][] getScrambleTiles0(){ return new Tile[][] { { new Tile(new String[]{"1 "},1), new Tile(new String[]{"2 "},2), new Tile(new String[]{"3 "},3), new Tile(new String[]{"4 "},4), }, { new Tile(new String[]{"5 "},5), new Tile(new String[]{"6 "},6), new Tile(new String[]{"7 "},7), new Tile(new String[]{"8 "},8), }, { new Tile(new String[]{"9 "}, 9), new Tile(new String[]{" "},16), new Tile(new String[]{"10"},10), new Tile(new String[]{"12"},12), }, { new Tile(new String[]{"13"},13), new Tile(new String[]{"11"},11), new Tile(new String[]{"14"},14), new Tile(new String[]{"15"},15), } }; } // 4x4, 1x2, #16. gap is on top edge. public static Board scramble1() { return new Board(getScrambleTiles1(),getScrambleOpts1()); } public static BoardOptions getScrambleOpts1(){ return new BoardOptions(); } public static Tile[][] getScrambleTiles1(){ return new Tile[][] { { new Tile(new String[]{"1 "},1), new Tile(new String[]{"2 "},2), new Tile(new String[]{" "},16), new Tile(new String[]{"4 "},4), }, { new Tile(new String[]{"5 "},5), new Tile(new String[]{"6 "},6), new Tile(new String[]{"7 "},7), new Tile(new String[]{"8 "},8), }, { new Tile(new String[]{"9 "}, 9), new Tile(new String[]{"3 "},3), new Tile(new String[]{"10"},10), new Tile(new String[]{"12"},12), }, { new Tile(new String[]{"13"},13), new Tile(new String[]{"11"},11), new Tile(new String[]{"14"},14), new Tile(new String[]{"15"},15), } }; } // 4x4, 1x2, #16. gap is on left edge. public static Board scramble2() { return new Board(getScrambleTiles2(),getScrambleOpts2()); } public static BoardOptions getScrambleOpts2(){ return new BoardOptions(); } public static Tile[][] getScrambleTiles2(){ return new Tile[][] { { new Tile(new String[]{"1 "},1), new Tile(new String[]{"2 "},2), new Tile(new String[]{"3 "},3), new Tile(new String[]{"4 "},4), }, { new Tile(new String[]{"5 "},5), new Tile(new String[]{"6 "},6), new Tile(new String[]{"7 "},7), new Tile(new String[]{"8 "},8), }, { new Tile(new String[]{" "},16), new Tile(new String[]{"9 "}, 9), new Tile(new String[]{"10"},10), new Tile(new String[]{"12"},12), }, { new Tile(new String[]{"13"},13), new Tile(new String[]{"11"},11), new Tile(new String[]{"14"},14), new Tile(new String[]{"15"},15), } }; } // 4x4, 1x2, #16. gap is on right edge. public static Board scramble3() { return new Board(getScrambleTiles3(),getScrambleOpts3()); } public static BoardOptions getScrambleOpts3(){ return new BoardOptions(); } public static Tile[][] getScrambleTiles3(){ return new Tile[][] { { new Tile(new String[]{"1 "},1), new Tile(new String[]{"2 "},2), new Tile(new String[]{"3 "},3), new Tile(new String[]{"4 "},4), }, { new Tile(new String[]{"5 "},5), new Tile(new String[]{"6 "},6), new Tile(new String[]{"7 "},7), new Tile(new String[]{" "},16), }, { new Tile(new String[]{"8 "},8), new Tile(new String[]{"9 "}, 9), new Tile(new String[]{"10"},10), new Tile(new String[]{"12"},12), }, { new Tile(new String[]{"13"},13), new Tile(new String[]{"11"},11), new Tile(new String[]{"14"},14), new Tile(new String[]{"15"},15), } }; } // 4x4, 1x2, #16. gap is on bottom edge. public static Board scramble4() { return new Board(getScrambleTiles4(),getScrambleOpts4()); } public static BoardOptions getScrambleOpts4(){ return new BoardOptions(); } public static Tile[][] getScrambleTiles4(){ return new Tile[][] { { new Tile(new String[]{"1 "},1), new Tile(new String[]{"2 "},2), new Tile(new String[]{"3 "},3), new Tile(new String[]{"4 "},4), }, { new Tile(new String[]{"5 "},5), new Tile(new String[]{"6 "},6), new Tile(new String[]{"7 "},7), new Tile(new String[]{"13"},13), }, { new Tile(new String[]{"8 "},8), new Tile(new String[]{"9 "}, 9), new Tile(new String[]{"10"},10), new Tile(new String[]{"12"},12), }, { new Tile(new String[]{"11"},11), new Tile(new String[]{" "},16), new Tile(new String[]{"14"},14), new Tile(new String[]{"15"},15), } }; } // 4x4, 1x2, #16. gap is in upper-left corner. public static Board scramble5() { return new Board(getScrambleTiles5(),getScrambleOpts5()); } public static BoardOptions getScrambleOpts5(){ return new BoardOptions(); } public static Tile[][] getScrambleTiles5(){ return new Tile[][] { { new Tile(new String[]{" "},16), new Tile(new String[]{"2 "},2), new Tile(new String[]{"7 "},7), new Tile(new String[]{"3 "},3), }, { new Tile(new String[]{"14"},14), new Tile(new String[]{"6 "},6), new Tile(new String[]{"4 "},4), new Tile(new String[]{"13"},13), }, { new Tile(new String[]{"8 "},8), new Tile(new String[]{"9 "}, 9), new Tile(new String[]{"10"},10), new Tile(new String[]{"12"},12), }, { new Tile(new String[]{"1 "},1), new Tile(new String[]{"11"},11), new Tile(new String[]{"5 "},5), new Tile(new String[]{"15"},15), } }; } // 4x4, 1x2, #16. gap is in upper-right corner. public static Board scramble6() { return new Board(getScrambleTiles6(),getScrambleOpts6()); } public static BoardOptions getScrambleOpts6(){ return new BoardOptions(); } public static Tile[][] getScrambleTiles6(){ return new Tile[][] { { new Tile(new String[]{"3 "},3), new Tile(new String[]{"1 "},1), new Tile(new String[]{"2 "},2), new Tile(new String[]{" "},16), }, { new Tile(new String[]{"5 "},5), new Tile(new String[]{"13"},13), new Tile(new String[]{"6 "},6), new Tile(new String[]{"7 "},7), }, { new Tile(new String[]{"8 "},8), new Tile(new String[]{"9 "}, 9), new Tile(new String[]{"10"},10), new Tile(new String[]{"12"},12), }, { new Tile(new String[]{"4 "},4), new Tile(new String[]{"11"},11), new Tile(new String[]{"14"},14), new Tile(new String[]{"15"},15), } }; } // 4x4, 1x2, #16. gap is in lower-left corner. public static Board scramble7() { return new Board(getScrambleTiles7(),getScrambleOpts7()); } public static BoardOptions getScrambleOpts7(){ return new BoardOptions(); } public static Tile[][] getScrambleTiles7(){ return new Tile[][] { { new Tile(new String[]{"1 "},1), new Tile(new String[]{"2 "},2), new Tile(new String[]{"14"},14), new Tile(new String[]{"4 "},4), }, { new Tile(new String[]{"5 "},5), new Tile(new String[]{"6 "},6), new Tile(new String[]{"7 "},7), new Tile(new String[]{"13"},13), }, { new Tile(new String[]{"8 "},8), new Tile(new String[]{"9 "}, 9), new Tile(new String[]{"10"},10), new Tile(new String[]{"15"},15), }, { new Tile(new String[]{" "},16), new Tile(new String[]{"3 "},3), new Tile(new String[]{"11"},11), new Tile(new String[]{"12"},12), } }; } // 4x4, 1x2, #16. gap is in lower-right corner. public static Board scramble8() { return new Board(getScrambleTiles8(),getScrambleOpts8()); } public static BoardOptions getScrambleOpts8(){ return new BoardOptions(); } public static Tile[][] getScrambleTiles8(){ return new Tile[][] { { new Tile(new String[]{"1 "},1), new Tile(new String[]{"2 "},2), new Tile(new String[]{"14"},14), new Tile(new String[]{"4 "},4), }, { new Tile(new String[]{"5 "},5), new Tile(new String[]{"6 "},6), new Tile(new String[]{"7 "},7), new Tile(new String[]{"13"},13), }, { new Tile(new String[]{"8 "},8), new Tile(new String[]{"9 "}, 9), new Tile(new String[]{"10"},10), new Tile(new String[]{"15"},15), }, { new Tile(new String[]{"3 "},3), new Tile(new String[]{"11"},11), new Tile(new String[]{"12"},12), new Tile(new String[]{" "},16), } }; } // check default values @Test public void board_01() { Board b = new Board(getTiles1(), getOpts1()); assertArrayEquals(getTiles1(), b.grid); assertEquals(new BoardOptions(), b.opts); } @Test public void board_02() { Board b = new Board(getTiles1(), getOpts1()); assertArrayEquals(getTiles1(), b.grid); assertEquals(getOpts1(), b.opts); b = new Board(getTiles2(),getOpts2()); assertArrayEquals(getTiles2(), b.grid); assertEquals(getOpts2(), b.opts); b = new Board(getTiles3(),getOpts3()); assertArrayEquals(getTiles3(), b.grid); assertEquals(getOpts3(), b.opts); b = new Board(getTiles4(),getOpts4()); assertArrayEquals(getTiles4(), b.grid); assertEquals(getOpts4(), b.opts); b = new Board(getTiles5(),getOpts5()); assertArrayEquals(getTiles5(), b.grid); assertEquals(getOpts5(), b.opts); } @Test public void board_03(){ Board b = scramble1(); // it should be here. assertEquals(new Coord(0,2), b.findOpening()); // it shouldn't be anywher else, e.g. (0,0). assertNotEquals(new Coord(0,0), b.findOpening()); }
// findOpening: edges. @Test public void board_04(){ Board b = board1(); assertEquals(new Coord(3,3), b.findOpening()); b = scramble2(); assertEquals(new Coord(2,0), b.findOpening()); b = scramble3(); assertEquals(new Coord(1,3), b.findOpening()); b = scramble4(); assertEquals(new Coord(3,1), b.findOpening()); }
// findOpening: corners. @Test public void board_05(){ Board b; b = scramble5(); assertEquals(new Coord(0,0), b.findOpening()); b = scramble6(); assertEquals(new Coord(0,3), b.findOpening()); b = scramble7(); assertEquals(new Coord(3,0), b.findOpening()); b = scramble8(); assertEquals(new Coord(3,3), b.findOpening()); } // findOpening: interior. @Test public void board_06(){ Board b = scramble0(); assertEquals(new Coord(2,1), b.findOpening()); }
// findOpening: long board. @Test public void board_07(){ Board b = board5(); assertEquals(new Coord(2,7), b.findOpening()); }
// findOpening: tall board. @Test public void board_08(){ Board b = board4(); assertEquals(new Coord(6,3), b.findOpening()); }
// findOpening: tiny board. @Test public void board_09(){ Board b = board6(); assertEquals(new Coord(1,1), b.findOpening()); } // must find the incompatible options @Test public void board_10(){ Tile[][] grid = new Tile[][] { { new Tile(new String[]{" "," 1 "," "},1), new Tile(new String[]{" "," 2 "," "},2), new Tile(new String[]{" "," 3 "," "},3), }, { new Tile(new String[]{" "," 4 "," "},4), new Tile(new String[]{" "," 5 "," "},5), new Tile(new String[]{" "," "," "},6) } }; try { Board b = new Board(grid, new BoardOptions().withGapID(1)); // all int fields are wrong. } catch (RuntimeException e) { if (e.getMessage().equals("incompatible options")){ return; } } fail("Board constructor should have noticed incompatible options."); } // onBoard: corners of a standard sized board. @Test public void board_11(){ Board b = board1(); // standard 4x4 size. // should be on board. assertTrue(b.onBoard(new Coord(0,0))); assertTrue(b.onBoard(new Coord(0,3))); assertTrue(b.onBoard(new Coord(3,0))); assertTrue(b.onBoard(new Coord(3,3))); // shouldn't be on board. assertFalse(b.onBoard(new Coord(4,4))); assertFalse(b.onBoard(new Coord(-1,-1))); assertFalse(b.onBoard(new Coord(2,5))); assertFalse(b.onBoard(new Coord(5,2))); } // onBoard: edges of standard board. @Test public void board_12(){ Board b = board1(); // standard 4x4 size. assertTrue(b.onBoard(new Coord(0,2))); assertTrue(b.onBoard(new Coord(2,0))); assertTrue(b.onBoard(new Coord(3,2))); assertTrue(b.onBoard(new Coord(1,3))); assertFalse(b.onBoard(new Coord(4,4))); } // onBoard: middle of standard board. @Test public void board_13(){ Board b = board1(); // standard 4x4 size. assertTrue(b.onBoard(new Coord(1,1))); assertTrue(b.onBoard(new Coord(1,2))); assertTrue(b.onBoard(new Coord(2,1))); assertTrue(b.onBoard(new Coord(2,2))); assertFalse(b.onBoard(new Coord(4,4))); } // onBoard: nonstandard boards. @Test public void board_14(){ Board b4 = board4(); // 7x4. assertTrue(b4.onBoard(new Coord(3,3))); assertTrue(b4.onBoard(new Coord(5,3))); assertTrue(b4.onBoard(new Coord(6,3))); assertTrue(b4.onBoard(new Coord(6,0))); assertFalse(b4.onBoard(new Coord(7,4))); assertFalse(b4.onBoard(new Coord(4,7))); assertFalse(b4.onBoard(new Coord(3,6))); assertFalse(b4.onBoard(new Coord(-2,-3))); Board b5 = board5(); // 3x8. assertTrue(b5.onBoard(new Coord(2,7))); assertTrue(b5.onBoard(new Coord(2,6))); assertTrue(b5.onBoard(new Coord(0,7))); assertTrue(b5.onBoard(new Coord(2,4))); assertFalse(b5.onBoard(new Coord(3,8))); assertFalse(b5.onBoard(new Coord(8,3))); assertFalse(b5.onBoard(new Coord(4,4))); assertFalse(b5.onBoard(new Coord(-1,-1))); } // onBoard(int,int) calls. same tests as above. @Test public void board_15(){ Board b = board1(); // standard 4x4 size. // should be on board. assertTrue(b.onBoard(0,0)); assertTrue(b.onBoard(0,2)); assertTrue(b.onBoard(0,3)); assertTrue(b.onBoard(1,1)); assertTrue(b.onBoard(1,2)); assertTrue(b.onBoard(1,3)); assertTrue(b.onBoard(2,0)); assertTrue(b.onBoard(2,1)); assertTrue(b.onBoard(2,2)); assertTrue(b.onBoard(3,0)); assertTrue(b.onBoard(3,2)); assertTrue(b.onBoard(3,3)); // shouldn't be on board. assertFalse(b.onBoard(-1,-1)); assertFalse(b.onBoard(2,5)); assertFalse(b.onBoard(4,4)); assertFalse(b.onBoard(5,2)); Board b4 = board4(); // 7x4. assertTrue(b4.onBoard(3,3)); assertTrue(b4.onBoard(5,3)); assertTrue(b4.onBoard(6,3)); assertTrue(b4.onBoard(6,0)); assertFalse(b4.onBoard(7,4)); assertFalse(b4.onBoard(4,7)); assertFalse(b4.onBoard(3,6)); assertFalse(b4.onBoard(-2,-3)); Board b5 = board5(); // 3x8. assertTrue(b5.onBoard(2,7)); assertTrue(b5.onBoard(2,6)); assertTrue(b5.onBoard(0,7)); assertTrue(b5.onBoard(2,4)); assertFalse(b5.onBoard(3,8)); assertFalse(b5.onBoard(8,3)); assertFalse(b5.onBoard(4,4)); assertFalse(b5.onBoard(-1,-1)); } // swap. @Test public void board_16(){ Board b = board7(); // standard board with gap at #7 (interior). // swap tile above the gap. Tile a1 = b.grid[1][2]; Tile b1 = b.grid[0][2]; b.swap(new Coord(0,2), new Coord(1,2)); Tile a2 = b.grid[0][2]; Tile b2 = b.grid[1][2]; assertEquals(a1,a2); assertEquals(b1,b2); } @Test public void board_17(){ Board b = board7(); // swap tile below the gap. Tile a1 = b.grid[1][2]; Tile b1 = b.grid[2][2]; b.swap(new Coord(2,2), new Coord(1,2)); Tile a2 = b.grid[2][2]; Tile b2 = b.grid[1][2]; assertEquals(a1,a2); assertEquals(b1,b2); } @Test public void board_18(){ // swap corner gap with tile above. Board b = board1(); Tile t1 = b.grid[3][3]; Tile t2 = b.grid[2][3]; b.swap(new Coord(2,3),new Coord(3,3)); assertEquals(t1,b.grid[2][3]); assertEquals(t2,b.grid[3][3]); b.swap(new Coord(2,3), new Coord(3,3)); assertEquals(t1,b.grid[3][3]); assertEquals(t2,b.grid[2][3]); } @Test public void board_19(){ // swap corner gap with tile to the left. Board b = board1(); Tile t1 = b.grid[3][3]; Tile t2 = b.grid[3][2]; b.swap(new Coord(3,2),new Coord(3,3)); assertEquals(t1,b.grid[3][2]); assertEquals(t2,b.grid[3][3]); b.swap(new Coord(3,2), new Coord(3,3)); assertEquals(t1,b.grid[3][3]); assertEquals(t2,b.grid[3][2]); } @Test public void board_20(){ // swap tiny board! Board b = new Board(new Tile[][]{{new Tile(new String[]{"1"},1),new Tile(new String[]{"2"},2)},{new Tile(new String[]{"3"},3),new Tile(new String[]{" "},4)}},new BoardOptions().withNumRows(2).withNumCols(2).withTileWidth(1)); Tile t1 = b.grid[0][0]; Tile t2 = b.grid[0][1]; Tile t3 = b.grid[1][0]; Tile t4 = b.grid[1][1]; // swap the gap clockwise all around, checking all four tiles' locations after each swap. b.swap(new Coord(1,0),new Coord(1,1)); assertEquals(t1,b.grid[0][0]); assertEquals(t2,b.grid[0][1]); assertEquals(t3,b.grid[1][1]); assertEquals(t4,b.grid[1][0]); b.swap(new Coord(1,0),new Coord(0,0)); assertEquals(t1,b.grid[1][0]); assertEquals(t2,b.grid[0][1]); assertEquals(t3,b.grid[1][1]); assertEquals(t4,b.grid[0][0]); b.swap(new Coord(0,0),new Coord(0,1)); assertEquals(t1,b.grid[1][0]); assertEquals(t2,b.grid[0][0]); assertEquals(t3,b.grid[1][1]); assertEquals(t4,b.grid[0][1]); b.swap(new Coord(0,1),new Coord(1,1)); assertEquals(t1,b.grid[1][0]); assertEquals(t2,b.grid[0][0]); assertEquals(t3,b.grid[0][1]); assertEquals(t4,b.grid[1][1]); } // move UP @Test public void board_21(){ Board b = board7(); // standard board with gap at #7 (interior). // swap tile above the gap. Tile a1 = b.grid[1][2]; Tile b1 = b.grid[0][2]; b.move(Dir.DOWN); Tile a2 = b.grid[0][2]; Tile b2 = b.grid[1][2]; assertEquals(a1,a2); assertEquals(b1,b2); } // move DOWN @Test public void board_22(){ Board b = board7(); // swap tile below the gap. Tile a1 = b.grid[1][2]; Tile b1 = b.grid[2][2]; b.move(Dir.UP); assertEquals(a1,b.grid[2][2]); assertEquals(b1,b.grid[1][2]); } // move DOWN/UP @Test public void board_23(){ // swap corner gap with tile above. Board b = board1(); Tile t1 = b.grid[3][3]; Tile t2 = b.grid[2][3]; b.move(Dir.DOWN); assertEquals(t1,b.grid[2][3]); assertEquals(t2,b.grid[3][3]); b.move(Dir.UP); assertEquals(t1,b.grid[3][3]); assertEquals(t2,b.grid[2][3]); } // move RIGHT/LEFT @Test public void board_24(){ // swap corner gap with tile to the left. Board b = board1(); Tile t1 = b.grid[3][3]; Tile t2 = b.grid[3][2]; b.move(Dir.RIGHT); assertEquals(t1,b.grid[3][2]); assertEquals(t2,b.grid[3][3]); b.move(Dir.LEFT); assertEquals(t1,b.grid[3][3]); assertEquals(t2,b.grid[3][2]); } // move around tiny board @Test public void board_25(){ // swap tiny board! Board b = new Board(new Tile[][]{{new Tile(new String[]{"1"},1),new Tile(new String[]{"2"},2)},{new Tile(new String[]{"3"},3),new Tile(new String[]{" "},4)}},new BoardOptions().withNumRows(2).withNumCols(2).withTileWidth(1)); Tile t1 = b.grid[0][0]; Tile t2 = b.grid[0][1]; Tile t3 = b.grid[1][0]; Tile t4 = b.grid[1][1]; // swap the gap clockwise all around, checking all four tiles' locations after each swap. b.move(Dir.RIGHT); assertEquals(t1,b.grid[0][0]); assertEquals(t2,b.grid[0][1]); assertEquals(t3,b.grid[1][1]); assertEquals(t4,b.grid[1][0]); b.move(Dir.DOWN); assertEquals(t1,b.grid[1][0]); assertEquals(t2,b.grid[0][1]); assertEquals(t3,b.grid[1][1]); assertEquals(t4,b.grid[0][0]); b.move(Dir.LEFT); assertEquals(t1,b.grid[1][0]); assertEquals(t2,b.grid[0][0]); assertEquals(t3,b.grid[1][1]); assertEquals(t4,b.grid[0][1]); b.move(Dir.UP); assertEquals(t1,b.grid[1][0]); assertEquals(t2,b.grid[0][0]); assertEquals(t3,b.grid[0][1]); assertEquals(t4,b.grid[1][1]); } // move should give boolean outcome answers correctly. @Test public void board_26(){ Board b = board7(); // standard board with gap at #7 (interior). assertTrue(b.move(Dir.DOWN)); assertFalse(b.move(Dir.DOWN)); assertFalse(b.move(Dir.DOWN)); assertTrue(b.move(Dir.UP)); assertTrue(b.move(Dir.UP)); assertTrue(b.move(Dir.UP)); assertFalse(b.move(Dir.UP)); assertTrue(b.move(Dir.DOWN)); assertTrue(b.move(Dir.RIGHT)); assertTrue(b.move(Dir.RIGHT)); assertFalse(b.move(Dir.RIGHT)); assertFalse(b.move(Dir.RIGHT)); assertTrue(b.move(Dir.LEFT)); assertTrue(b.move(Dir.LEFT)); assertTrue(b.move(Dir.LEFT)); assertFalse(b.move(Dir.LEFT)); assertFalse(b.move(Dir.LEFT)); } // isSolved - true @Test public void board_27(){ assertTrue(board1().isSolved()); assertTrue(board2().isSolved()); assertTrue(board3().isSolved()); } // isSolved - false @Test public void board_28(){ assertFalse(scramble0().isSolved()); assertFalse(scramble1().isSolved()); assertFalse(scramble2().isSolved()); assertFalse(scramble3().isSolved()); assertFalse(scramble4().isSolved()); } // isSolved - long/tall boards true. @Test public void board_29(){ assertTrue(board4().isSolved()); assertTrue(board5().isSolved()); } // isSolved - long/tall boards, false. @Test public void board_30(){ Board b4 = board4(); b4.move(Dir.DOWN); assertFalse(b4.isSolved()); Board b5 = board5(); b5.move(Dir.RIGHT); assertFalse(b5.isSolved()); } // isSolved - smallon-centered gaps, true. @Test public void board_31(){ assertTrue(board6().isSolved()); assertTrue(board7().isSolved()); } // isSolved - smallon-centered gaps, not solved. @Test public void board_32(){ Board b6 = board6(); b6.move(Dir.DOWN); assertFalse(b6.isSolved()); Board b7 = board7(); b7.move(Dir.LEFT); assertFalse(b7.isSolved()); } // isSolved - false (gap is in corners) @Test public void board_33(){ assertFalse(scramble5().isSolved()); assertFalse(scramble6().isSolved()); assertFalse(scramble7().isSolved()); assertFalse(scramble8().isSolved()); } // toString @Test public void board_34(){ Board b = board1(); String expected = "+--+--+--+--+ " +"|1 |2 |3 |4 | " +"+--+--+--+--+ " +"|5 |6 |7 |8 | " +"+--+--+--+--+ " +"|9 |10|11|12| " +"+--+--+--+--+ " +"|13|14|15| | " +"+--+--+--+--+ " ; assertEquals(expected, b.toString()); } // toString @Test public void board_35(){ Board b = board2(); String expected = "+----+----+----+----+ " +"| | | | | " +"| 1 | 2 | 3 | 4 | " +"| | | | | " +"+----+----+----+----+ " +"| | | | | " +"| 5 | 6 | 7 | 8 | " +"| | | | | " +"+----+----+----+----+ " +"| | | | | " +"| 9 | 10 | 11 | 12 | " +"| | | | | " +"+----+----+----+----+ " +"| | | | | " +"| 13 | 14 | 15 | | " +"| | | | | " +"+----+----+----+----+ " ; assertEquals(expected, b.toString()); } // toString @Test public void board_36(){ Board b = board4(); String expected = "+--+--+--+--+ " +"|1 |2 |3 |4 | " +"+--+--+--+--+ " +"|5 |6 |7 |8 | " +"+--+--+--+--+ " +"|9 |10|11|12| " +"+--+--+--+--+ " +"|13|14|15|16| " +"+--+--+--+--+ " +"|17|18|19|20| " +"+--+--+--+--+ " +"|21|22|23|24| " +"+--+--+--+--+ " +"|25|26|27| | " +"+--+--+--+--+ " ; assertEquals(expected, b.toString()); } // toString @Test public void board_37(){ Board b = board6(); String expected = "+--+--+--+ " +"|1 |2 |3 | " +"+--+--+--+ " +"|4 | |6 | " +"+--+--+--+ " +"|7 |8 |9 | " +"+--+--+--+ " ; assertEquals(expected, b.toString()); } // toString @Test public void board_38(){ Board b = scramble3(); String expected = "+--+--+--+--+ " +"|1 |2 |3 |4 | " +"+--+--+--+--+ " +"|5 |6 |7 | | " +"+--+--+--+--+ " +"|8 |9 |10|12| " +"+--+--+--+--+ " +"|13|11|14|15| " +"+--+--+--+--+ " ; assertEquals(expected, b.toString()); } // toString @Test public void board_39(){ Board b = scramble7(); String expected = "+--+--+--+--+ " +"|1 |2 |14|4 | " +"+--+--+--+--+ " +"|5 |6 |7 |13| " +"+--+--+--+--+ " +"|8 |9 |10|15| " +"+--+--+--+--+ " +"| |3 |11|12| " +"+--+--+--+--+ " ; assertEquals(expected, b.toString()); } @Test public void board_40(){ Board b = board1(); // any arbitrary long can be used; by inspecting what occurs, we can record what is expected. long seed = 12345; b.scramble(1,new Random(seed)); Tile[][] expected = new Tile[][] { { new Tile(new String[]{"1 "},1), new Tile(new String[]{"2 "},2), new Tile(new String[]{"3 "},3), new Tile(new String[]{"4 "},4), }, { new Tile(new String[]{"5 "},5), new Tile(new String[]{"6 "},6), new Tile(new String[]{"7 "},7), new Tile(new String[]{"8 "},8), }, { new Tile(new String[]{"9 "},9), new Tile(new String[]{"10"},10), new Tile(new String[]{"11"},11), new Tile(new String[]{" "},16), }, { new Tile(new String[]{"13"},13), new Tile(new String[]{"14"},14), new Tile(new String[]{"15"},15), new Tile(new String[]{"12"},12), } };
assertArrayEquals(expected,b.grid); }
@Test public void board_41(){ Board b = board1(); // any arbitrary long can be used; by inspecting what occurs, we can record what is expected. long seed = 12345; b.scramble(1000,new Random(seed)); Tile[][] expected = new Tile[][] { { new Tile(new String[]{"10"},10), new Tile(new String[]{"4 "},4), new Tile(new String[]{"15"},15), new Tile(new String[]{"1 "},1), }, { new Tile(new String[]{"9 "},9), new Tile(new String[]{"11"},11), new Tile(new String[]{"6 "},6), new Tile(new String[]{"2 "},2), }, { new Tile(new String[]{"5 "},5), new Tile(new String[]{"14"},14), new Tile(new String[]{" "},16), new Tile(new String[]{"8 "},8), }, { new Tile(new String[]{"12"},12), new Tile(new String[]{"13"},13), new Tile(new String[]{"3 "},3), new Tile(new String[]{"7 "},7), } };
assertArrayEquals(expected,b.grid); }
@Test public void board_42(){ Board b = board4(); // any arbitrary long can be used; by inspecting what occurs, we can record what is expected. long seed = 42; b.scramble(6,new Random(seed)); Tile[][] expected = new Tile[][] { { new Tile(new String[]{"1 "},1), new Tile(new String[]{"2 "},2), new Tile(new String[]{"3 "},3), new Tile(new String[]{"4 "},4), }, { new Tile(new String[]{"5 "},5), new Tile(new String[]{"6 "},6), new Tile(new String[]{"7 "},7), new Tile(new String[]{"8 "},8), }, { new Tile(new String[]{"9 "}, 9), new Tile(new String[]{"10"},10), new Tile(new String[]{"11"},11), new Tile(new String[]{"12"},12), }, { new Tile(new String[]{"13"},13), new Tile(new String[]{"14"},14), new Tile(new String[]{"15"},15), new Tile(new String[]{"16"},16), }, { new Tile(new String[]{"17"},17), new Tile(new String[]{"18"},18), new Tile(new String[]{"19"},19), new Tile(new String[]{"20"},20), }, { new Tile(new String[]{"21"},21), new Tile(new String[]{"22"},22), new Tile(new String[]{" "},28), new Tile(new String[]{"23"},23), }, { new Tile(new String[]{"25"},25), new Tile(new String[]{"26"},26), new Tile(new String[]{"27"},27), new Tile(new String[]{"24"},24), } };
assertArrayEquals(expected,b.grid); } }
class Board This is the main class of the project. Its state is just a 2D array of tiles and a Boardoptions. fields For selfish reasons (an easier time writing the test cases), I've left the fields as public. public Tile[grid. public Boardoptions opts. It is assumed that its properties will match the grid. Whenever possible, we should access the details via opts, and not navigate through the grid to rediscover e.g. how many columns are in this Board each time we need to know. o Manual Inspection Criteria (5%): Throughout the Board class, you use opts to find the board dimensions, instead of manually inspecting the grid each time. (translation: you should be using opts quite regularly all throughout your Board implementation). methods public Board (Tile[0 grid, Boardoptions opts). Basic constructor that blindly accepts the parameters and stores to the fields. If any of the Boardoptions dimensions (the int fields only) are incompatible with the given grid, you must: throw new RuntimeException ( "incompatible options") o the Board dimensions might not match. o there may be a Tile that doesn't have the given dimensions. o the gap tile's ID may be off-board. O there's nothing that can go wrong with the spacers. public Board (). This constructor creates the "default" board, which uses all default settings of Boardoptions: it's a 4x4 grid, the tiles are 1x2 dimensions, and tile 16 is the gap tile. public Coord findopening). Discovers at which (row,col) the 'gap' tile is, and returns a Coord object to represent that place. When a malformed board is given (which has no tile by that id), your code should complain thus: throw new RuntimeException( "no opening found") public boolean onBoard (Coord loc). Answers if the given row/column position exists on teh board. (Since we can create boards of different sizes, and pieces move around, we keep track of the edges of our board in this one place). public boolean onBoard ( int r, int c). Same idea as before, but this overloaded version accepts two int values instead of a coord. o Manual Inspection Criteria (5%): call the other version. public boolean swap (Coord a, Coord b). Given two locations, if both are on this board, then swap the tiles at each place with each other, and return true. Remember, the gap space is also a Tile. public boolean move (Dir dir). Given a direction, find the opening, and find the piece that can move in the indicated direction (if present), and move it. Return true if the attempt was successful, and return false when we can't move a tile in that direction. Rernernber, there are only four possible Dir objects! Which two places do you want to swap? public boolean issolved (). Are all the tiles in their correct spots? A quick scan should find all the tiles' id values in sequential order, including the gap tile. override public String toString(). This is an involved IIIethod. It must put together all the tiles' icons in the correct current locations of the grid, separating all of them with the horizontal/vertical/intersection spacer* characters. The answer should be a multi-line string. Here is an example of our default settings: class Board This is the main class of the project. Its state is just a 2D array of tiles and a Boardoptions. fields For selfish reasons (an easier time writing the test cases), I've left the fields as public. public Tile[grid. public Boardoptions opts. It is assumed that its properties will match the grid. Whenever possible, we should access the details via opts, and not navigate through the grid to rediscover e.g. how many columns are in this Board each time we need to know. o Manual Inspection Criteria (5%): Throughout the Board class, you use opts to find the board dimensions, instead of manually inspecting the grid each time. (translation: you should be using opts quite regularly all throughout your Board implementation). methods public Board (Tile[0 grid, Boardoptions opts). Basic constructor that blindly accepts the parameters and stores to the fields. If any of the Boardoptions dimensions (the int fields only) are incompatible with the given grid, you must: throw new RuntimeException ( "incompatible options") o the Board dimensions might not match. o there may be a Tile that doesn't have the given dimensions. o the gap tile's ID may be off-board. O there's nothing that can go wrong with the spacers. public Board (). This constructor creates the "default" board, which uses all default settings of Boardoptions: it's a 4x4 grid, the tiles are 1x2 dimensions, and tile 16 is the gap tile. public Coord findopening). Discovers at which (row,col) the 'gap' tile is, and returns a Coord object to represent that place. When a malformed board is given (which has no tile by that id), your code should complain thus: throw new RuntimeException( "no opening found") public boolean onBoard (Coord loc). Answers if the given row/column position exists on teh board. (Since we can create boards of different sizes, and pieces move around, we keep track of the edges of our board in this one place). public boolean onBoard ( int r, int c). Same idea as before, but this overloaded version accepts two int values instead of a coord. o Manual Inspection Criteria (5%): call the other version. public boolean swap (Coord a, Coord b). Given two locations, if both are on this board, then swap the tiles at each place with each other, and return true. Remember, the gap space is also a Tile. public boolean move (Dir dir). Given a direction, find the opening, and find the piece that can move in the indicated direction (if present), and move it. Return true if the attempt was successful, and return false when we can't move a tile in that direction. Rernernber, there are only four possible Dir objects! Which two places do you want to swap? public boolean issolved (). Are all the tiles in their correct spots? A quick scan should find all the tiles' id values in sequential order, including the gap tile. override public String toString(). This is an involved IIIethod. It must put together all the tiles' icons in the correct current locations of the grid, separating all of them with the horizontal/vertical/intersection spacer* characters. The answer should be a multi-line string. Here is an example of our default settingsStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started