def test_verify_initial_state_correctedness(self):
        b = np.array([[1, 1, 1, 0, 1],
                      [1, 1, 1, 0, 1],
                      [1, 1, 1, 0, 1],
                      [1, 1, 1, 0, 1],
                      [1, 1, 1, 0, 1]])
        p = vv.Puzzle(b)
        p.verify_initial_state_correctedness()

        b = np.array([[1, 1, 1, 0, 1],
                      [1, 1, 1, 0, 1],
                      [1, 0, 1, 0, 1],
                      [1, 1, 1, 0, 1],
                      [1, 1, 1, 0, 1]])
        with self.assertRaises(Exception) as cm:
            p = vv.Puzzle(b, [0] * 5)
            p.verify_initial_state_correctedness()
        err = cm.exception
        self.assertEqual(str(err), "The starting number of plants must be divisible by 5.\n"
                         "Check both the board and the buffer.")

        with self.assertRaises(Exception) as cm:
            p = vv.Puzzle(b, [1] * 5)
            p.verify_initial_state_correctedness()
        err = cm.exception
        self.assertEqual(str(err), "Buffer cannot be full at the beginning of the puzzle.")

        with self.assertRaises(Exception) as cm:
            p = vv.Puzzle(b, [0, 0, 0, 1, 0])
            p.verify_initial_state_correctedness()
        err = cm.exception
        self.assertEqual(str(err), "Buffer has to be filled from left to right without any gaps")
Example #2
0
    def test_theo_list_optimal_theo_solutions(self):
        self.assertTrue(
            len(
                vv.theo_list_optimal_theo_solutions(
                    vv.Puzzle(np.zeros([5, 5], dtype=int)))) == 0)
        p = vv.Puzzle(
            np.array([[1, 1, 1, 5, 5], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0],
                      [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]), [0] * 5)

        self.assertTrue(len(vv.theo_list_optimal_theo_solutions(p)) == 1)
 def test_buf_begins_recipe(self):
     b = np.zeros([5, 5], dtype=int)
     p = vv.Puzzle(b, [1, 0, 0, 0, 0])
     n, the_list = p.buf_begins_recipe()
     self.assertTrue(n == 1)
     self.assertTrue([0] == the_list)
     p = vv.Puzzle(b, [3, 2, 2, 3, 4])
     n, the_list = p.buf_begins_recipe()
     self.assertTrue(n == 5)
     self.assertTrue([4] == the_list)
     p = vv.Puzzle(b, [3, 0, 0, 0, 0])
     n, the_list = p.buf_begins_recipe()
     self.assertTrue(n == 1)
     self.assertTrue([3, 4] == the_list)
Example #4
0
    def test_solve_puzzle(self):
        board = np.array([[1, 1, 5, 0, 0], [5, 1, 0, 0, 0], [0, 0, 0, 0, 0],
                          [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]])
        solutions = vv.solve_puzzle(vv.Puzzle(board))
        self.assertTrue(solutions[0].recipes_achieved == [1, 0, 0, 0, 0])

        board = np.array([[5, 1, 4, 4, 5], [5, 1, 3, 1, 1], [2, 1, 5, 5, 1],
                          [1, 2, 1, 2, 1], [3, 1, 1, 1, 5]])
        solutions = vv.solve_puzzle(vv.Puzzle(board))
        # got this from an early implementation, never validated
        solutions2 = [{
            'recipes': [3, 0, 0, 0, 1],
            'moves': [(2, 4), (1, 3), (2, 2), (1, 1), (0, 0), (1, 2), (3, 1),
                      (3, 3), (4, 0), (0, 2), (0, 1), (1, 4), (0, 4), (2, 1),
                      (1, 0), (3, 0), (3, 2), (2, 3), (3, 4), (4, 4)]
        }, {
            'recipes': [2, 0, 1, 0, 0],
            'moves': [(2, 4), (1, 3), (2, 2), (1, 1), (0, 0), (1, 2), (3, 1),
                      (3, 3), (0, 1), (1, 4), (2, 1), (3, 0), (0, 4), (3, 2),
                      (1, 0), (2, 3), (0, 2), (4, 4), (0, 3), (3, 4)]
        }, {
            'recipes': [3, 0, 0, 1, 0],
            'moves': [(2, 4), (1, 3), (2, 2), (1, 1), (0, 0), (1, 2), (3, 1),
                      (3, 3), (0, 1), (1, 4), (2, 1), (3, 0), (0, 4), (3, 2),
                      (1, 0), (3, 4), (4, 1), (2, 3), (4, 2), (4, 4), (4, 0),
                      (4, 3), (0, 2), (2, 0), (0, 3)]
        }, {
            'recipes': [1, 1, 1, 0, 0],
            'moves': [(2, 4), (1, 3), (2, 2), (1, 1), (0, 0), (1, 2), (0, 1),
                      (0, 4), (1, 4), (2, 1), (3, 0), (3, 2), (3, 4), (4, 1),
                      (4, 2), (2, 0), (3, 1), (3, 3), (4, 0), (1, 0), (2, 3),
                      (0, 2), (4, 4), (0, 3), (4, 3)]
        }, {
            'recipes': [2, 1, 0, 0, 0],
            'moves': [(2, 4), (1, 3), (2, 2), (1, 1), (0, 2), (1, 2), (3, 2),
                      (0, 3), (3, 4), (4, 1), (0, 1), (1, 4), (0, 0), (2, 1),
                      (0, 4), (3, 0), (4, 2), (1, 0), (4, 3), (2, 3), (2, 0),
                      (3, 1), (3, 3), (4, 0), (4, 4)]
        }]
        for s in solutions:
            found = False
            for s2 in solutions2:
                if s.recipes_achieved == s2['recipes']:
                    found = True
                    break
            if not found:
                self.assertTrue(False)
 def test_constructor(self):
     b = np.array([[1, 0, 1, 0, 1],
                   [1, 1, 1, 0, 1],
                   [1, 1, 1, 0, 1],
                   [1, 1, 1, 0, 1],
                   [1, 0, 1, 0, 1]])
     p = vv.Puzzle(b)
     p2 = vv.Puzzle.from_puzzle(p)
    def test_is_every_square_reachable(self):
        p = vv.Puzzle(np.array([[1, 0, 1, 1, 1],
                                [1, 1, 1, 1, 1],
                                [1, 1, 1, 1, 1],
                                [1, 1, 1, 1, 1],
                                [1, 0, 1, 1, 1]]))
        self.assertFalse(p.is_every_square_reachable())
        p = vv.Puzzle(np.array([[1, 1, 1, 1, 1],
                                [1, 0, 1, 1, 0],
                                [1, 1, 0, 0, 1],
                                [1, 0, 1, 1, 0],
                                [1, 1, 1, 1, 1]]))
        self.assertTrue(p.is_every_square_reachable())

        p = vv.Puzzle(np.array([[1, 1, 1, 1, 1],
                                [1, 0, 1, 1, 0],
                                [1, 1, 0, 0, 1],
                                [1, 0, 1, 1, 1],
                                [1, 1, 1, 1, 1]]))
        self.assertFalse(p.is_every_square_reachable())
 def test_is_square_reachable(self):
     p = vv.Puzzle(np.array([[1, 0, 1, 0, 1],
                             [1, 1, 1, 0, 1],
                             [1, 1, 1, 0, 1],
                             [1, 1, 1, 0, 1],
                             [1, 0, 1, 0, 1]]))
     self.assertTrue(p.is_square_reachable(1, 1))
     self.assertTrue(p.is_square_reachable(0, 0))
     self.assertTrue(p.is_square_reachable(1, 1))
     self.assertTrue(p.is_square_reachable(4, 3))
     self.assertTrue(p.is_square_reachable(2, 2))
     self.assertFalse(p.is_square_reachable(2, 1))
     self.assertFalse(p.is_square_reachable(2, 0))
     p = vv.Puzzle(np.array([[1, 0, 1, 1, 1],
                             [1, 1, 1, 1, 1],
                             [1, 1, 1, 1, 1],
                             [1, 1, 1, 1, 1],
                             [1, 0, 1, 1, 1]]))
     self.assertTrue(p.is_square_reachable(0, 1))
     self.assertTrue(p.is_square_reachable(1, 4))
     self.assertTrue(p.is_square_reachable(2, 4))
     self.assertTrue(p.is_square_reachable(3, 4))
     self.assertFalse(p.is_square_reachable(4, 4))
     self.assertFalse(p.is_square_reachable(2, 2))
Example #8
0
 def test_theo_solve_puzzle(self):
     p = vv.Puzzle(
         np.array([[1, 0, 5, 0, 0], [5, 1, 0, 0, 0], [0, 0, 0, 0, 0],
                   [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]), [1, 0, 0, 0, 0])
     solutions = vv.theo_solve_puzzle(p, vv.Solution(), vv.Solution_Set())
     self.assertTrue(solutions[0].recipes_achieved == [1, 0, 0, 0, 0])
Example #9
0
                  [4, 1, 4, 3, 4],
                  [3, 1, 3, 5, 5]])

#recipes = np.array([[1, 1, 5, 1, 5], # BR - BR - SV - BR - SV
#                    [2, 2, 2, 3, 5], # CP - CP - CP - HB - SV
#                    [5, 4, 5, 4, 1], # SV - RS - SV - RS - BR
#                    [3, 1, 4, 2, 4], # HB - BR - RS - CP - RS
#                    [3, 2, 2, 3, 4]]) # HB - CP - CP - HB - RS



board = np.array([[1, 2, 2, 2, 2],
                  [1, 2, 2, 2, 2],
                  [1, 2, 2, 2, 2],
                  [5, 2, 2, 2, 2],
                  [5, 2, 2, 2, 2]])
attributes = {'have_ENDISWAL': True}
solutions = vv.solve_puzzle(vv.Puzzle(board, [0] * 5, attributes))
print(solutions)


for i in range(50):
    p = vv.Puzzle(np.random.randint(1, 6, size=[5, 5], dtype=int), [0] * 5, attributes)
    p.print_puzzle_state()
    solutions = vv.solve_puzzle(p)
    for s in solutions:
        print(s.recipes_achieved)
    
quit()