Advertisement
Guest User

PuzzleGen.java

a guest
Sep 22nd, 2019
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.41 KB | None | 0 0
  1. package signpost;
  2.  
  3. import org.junit.Test;
  4. import static org.junit.Assert.*;
  5.  
  6. /** Tests of the Model class.
  7.  *  @author P. N. Hilfinger
  8.  */
  9. public class PuzzleGeneratorTests {
  10.  
  11.     /** Check that SOLN is a valid WIDTH x HEIGHT puzzle that has fixed ends
  12.      *  unless ALLOWLOOSE. */
  13.     private void checkPuzzle(int[][] soln,
  14.                              int width, int height, boolean allowLoose) {
  15.         assertTrue("Bad size",
  16.                    soln.length == width && soln[0].length == height);
  17.         int last = width * height;
  18.         for (int x0 = 0; x0 < width; x0 += 1) {
  19.             for (int y0 = 0; y0 < width; y0 += 1) {
  20.                 int v = soln[x0][y0];
  21.                 if (v == last) {
  22.                     continue;
  23.                 }
  24.                 assertTrue("Value out of range", v >= 1 && v <= last);
  25.                 int c;
  26.                 for (int x1 = c = 0; x1 < width; x1 += 1) {
  27.                     for (int y1 = 0; y1 < height; y1 += 1) {
  28.                         if (soln[x1][y1] == v + 1) {
  29.                             assertTrue("Values not in line",
  30.                                        x0 == x1 || y0 == y1
  31.                                        || Math.abs(x0 - x1)
  32.                                        == Math.abs(y0 - y1));
  33.                             c += 1;
  34.                         }
  35.                     }
  36.                 }
  37.                 assertEquals("Duplicate or unconnected values", 1, c);
  38.             }
  39.         }
  40.         if (!allowLoose) {
  41.             assertTrue("End points incorrect",
  42.                        soln[0][height - 1] == 1 && soln[width - 1][0] == last);
  43.         }
  44.     }
  45.  
  46.     @Test
  47.     public void puzzleTest() {
  48.         PuzzleGenerator puzzler = new PuzzleGenerator(314159);
  49.         Model model;
  50.         model = puzzler.getPuzzle(5, 5, false);
  51.         checkPuzzle(model.solution(), 5, 5, false);
  52.         model = puzzler.getPuzzle(4, 6, false);
  53.         checkPuzzle(model.solution(), 4, 6, false);
  54.         model = puzzler.getPuzzle(5, 5, true);
  55.         checkPuzzle(model.solution(), 5, 5, true);
  56.     }
  57.  
  58.     @Test
  59.     public void uniquePuzzleTest() {
  60.         PuzzleGenerator puzzler = new PuzzleGenerator(314159);
  61.         Model model;
  62.         model = puzzler.getPuzzle(1, 2, false);
  63.         checkPuzzle(model.solution(), 1, 2, false);
  64.         model = puzzler.getPuzzle(1, 3, false);
  65.         checkPuzzle(model.solution(), 1, 3, false);
  66.     }
  67.  
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement