Example #1
0
 def setUp(self):
     self.eightInitial = PuzzleState((3, 3), [2, 8, 3, 1, 6, 4, 7, None, 5],
                                     None, None)
     self.eightGoal = PuzzleState((3, 3), [None, 2, 3, 1, 8, 6, 7, 5, 4],
                                  None, None)
     self.eightSolver = PuzzleSolver(self.eightInitial, self.eightGoal)
     self.redundantSolver = PuzzleSolver(self.eightInitial,
                                         self.eightInitial)
     self.cardinal = {
         "north": "up",
         "south": "down",
         "west": "left",
         "east": "right",
     }
Example #2
0
    def setUp(self):

        initialGamestate = [
            1, 2, 3, 4, 5, None, 6, 8, 9, 10, 7, 12, 13, 14, 11, 15
        ]

        goalGamestate = [
            1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, None
        ]

        self.fifteenInitial = PuzzleState((4, 4), initialGamestate, None, None)
        self.fifteenGoal = PuzzleState((4, 4), goalGamestate, None, None)
        self.fifteenSolver = PuzzleSolver(self.fifteenInitial,
                                          self.fifteenGoal)
        self.redundantSolver = PuzzleSolver(self.fifteenInitial,
                                            self.fifteenInitial)
        self.cardinal = {
            "north": "up",
            "south": "down",
            "west": "left",
            "east": "right",
        }
Example #3
0
    def setUp(self):

        initialGamestate = [
            1, 2, 3, 4,
            5, None, 6, 8,
            9, 10, 7, 12,
            13, 14, 11, 15]

        goalGamestate = [
            1, 2, 3, 4,
            5, 6, 7, 8,
            9, 10, 11, 12,
            13, 14, 15, None]

        self.fifteenInitial = PuzzleState((4, 4), initialGamestate, None, None)
        self.fifteenGoal = PuzzleState((4, 4), goalGamestate, None, None)
        self.fifteenSolver = PuzzleSolver(self.fifteenInitial, self.fifteenGoal)
        self.redundantSolver = PuzzleSolver(self.fifteenInitial, self.fifteenInitial)
        self.cardinal = {
            "north": "up",
            "south": "down",
            "west": "left",
            "east": "right",
        }
Example #4
0
class TestPuzzleSolver(unittest.TestCase):

    def setUp(self):

        initialGamestate = [
            1, 2, 3, 4,
            5, None, 6, 8,
            9, 10, 7, 12,
            13, 14, 11, 15]

        goalGamestate = [
            1, 2, 3, 4,
            5, 6, 7, 8,
            9, 10, 11, 12,
            13, 14, 15, None]

        self.fifteenInitial = PuzzleState((4, 4), initialGamestate, None, None)
        self.fifteenGoal = PuzzleState((4, 4), goalGamestate, None, None)
        self.fifteenSolver = PuzzleSolver(self.fifteenInitial, self.fifteenGoal)
        self.redundantSolver = PuzzleSolver(self.fifteenInitial, self.fifteenInitial)
        self.cardinal = {
            "north": "up",
            "south": "down",
            "west": "left",
            "east": "right",
        }

    def testSolve(self):
        expectedElements = (self.fifteenInitial.gamestate, self.fifteenGoal.gamestate)
        observedStates = self.fifteenSolver.solve()
        observedElements = [ x.gamestate for x in observedStates ]

        m_solution = '''PuzzleSolver ultimately arrives at the wrong solution! Check your solve() method.

Input:
{}

Observed last element of solution chain
{}

Expected last element of solution chain:
{}'''
        expectedPrintable = '\n'.join([ str(x) for x in expectedElements ])
        observedPrintable = '\n'.join([ str(x) for x in observedElements ])

        message = m_solution.format(self.fifteenInitial, observedPrintable, expectedPrintable)
        self.assertEquals(observedElements[-1], expectedElements[-1], message)

    def testMovesToSolve(self):
        m = '''I couldn't recognise the moves PuzzleSolver returns in movesToSolve() as English directions. Hint: these tests take north/south/east/west or up/down/left/right and don't care about capitalization.

Observed moves list:
{}'''
        rawMoves = self.fifteenSolver.movesToSolve()

        # default to x.lower() if x.lower() isn't in self.cardinal.keys()
        try:
            observedDirections = [ self.cardinal[x.lower()] for x in rawMoves ]
        except KeyError:
            observedDirections = [ x.lower() for x in rawMoves ]

        message = m.format(rawMoves)
        goodDirections = all([x in ['left', 'right', 'up', 'down'] for x in observedDirections])
        self.assertTrue(goodDirections, message)

    def testDoMovesWork(self):
        m = '''Following PuzzleSolver's reported moves doesn't actually solve the puzzle! Check your movesToSolve() and solve() methods.

Input:
{}

Reported moves from movesToSolve():
{}

After following the moves above, we got:
{}

The actual solution:
{}'''
        rawMoves = self.fifteenSolver.movesToSolve()

        # default to x.lower() if x.lower() isn't in self.cardinal.keys()
        moves = [ self.cardinal.get(x.lower(), x.lower()) for x in rawMoves ]

        initialVisual = str(self.fifteenInitial)
        for move in moves:

            # table-driven testing is hip now, just ask Nigel Tao (or ken)
            # https://code.google.com/p/go-wiki/wiki/TableDrivenTests
            moveFunctions = {
                "up": self.fifteenInitial.moveUp,
                "down": self.fifteenInitial.moveDown,
                "left": self.fifteenInitial.moveLeft,
                "right": self.fifteenInitial.moveRight,
            }

            # execute the tabled function
            self.fifteenInitial = moveFunctions[move]()

        message = m.format(initialVisual, rawMoves, self.fifteenInitial, self.fifteenGoal)
        self.assertEquals(self.fifteenInitial, self.fifteenGoal, message)

    def testAlreadySolvedMoves(self):
        m = '''PuzzleSolver did something unexpected when the initial and goal PuzzleStates are the same.

These tests expect an empty list of moves or None to be returned from movesToSolve() in this case.

Observed moves list:
{}'''

        expectedMoves = ([], None)
        observedMoves = self.redundantSolver.movesToSolve()
        message = m.format(observedMoves)
        self.assertIn(observedMoves, expectedMoves, message)

    def testAlreadySolvedSolve(self):
        m = '''PuzzleSolver did something unexpected when the initial and goal PuzzleStates are the same.

These tests expect solve() to return a list containing only one item: the initial/goal state.

Observed solution:
{}'''
        expectedSoln = [ self.fifteenInitial ]
        observedSoln = self.redundantSolver.solve()
        message = m.format("\n".join([ str(x) for x in observedSoln ]))
        self.assertEquals(observedSoln, expectedSoln, message)
Example #5
0
 def setUp(self):
     self.eightInitial = PuzzleState((3, 3), [2, 8, 3, 1, 6, 4, 7, None, 5], None, None)
     self.eightGoal = PuzzleState((3, 3), [None, 2, 3, 1, 8, 6, 7, 5, 4], None, None)
     self.eightSolver = PuzzleSolver(self.eightInitial, self.eightGoal)
     self.redundantSolver = PuzzleSolver(self.eightInitial, self.eightInitial)
     self.cardinal = {"north": "up", "south": "down", "west": "left", "east": "right"}
Example #6
0
class TestPuzzleSolver(unittest.TestCase):
    def setUp(self):

        initialGamestate = [
            1, 2, 3, 4, 5, None, 6, 8, 9, 10, 7, 12, 13, 14, 11, 15
        ]

        goalGamestate = [
            1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, None
        ]

        self.fifteenInitial = PuzzleState((4, 4), initialGamestate, None, None)
        self.fifteenGoal = PuzzleState((4, 4), goalGamestate, None, None)
        self.fifteenSolver = PuzzleSolver(self.fifteenInitial,
                                          self.fifteenGoal)
        self.redundantSolver = PuzzleSolver(self.fifteenInitial,
                                            self.fifteenInitial)
        self.cardinal = {
            "north": "up",
            "south": "down",
            "west": "left",
            "east": "right",
        }

    def testSolve(self):
        expectedElements = (self.fifteenInitial.gamestate,
                            self.fifteenGoal.gamestate)
        observedStates = self.fifteenSolver.solve()
        observedElements = [x.gamestate for x in observedStates]

        m_solution = '''PuzzleSolver ultimately arrives at the wrong solution! Check your solve() method.

Input:
{}

Observed last element of solution chain
{}

Expected last element of solution chain:
{}'''
        expectedPrintable = '\n'.join([str(x) for x in expectedElements])
        observedPrintable = '\n'.join([str(x) for x in observedElements])

        message = m_solution.format(self.fifteenInitial, observedPrintable,
                                    expectedPrintable)
        self.assertEquals(observedElements[-1], expectedElements[-1], message)

    def testMovesToSolve(self):
        m = '''I couldn't recognise the moves PuzzleSolver returns in movesToSolve() as English directions. Hint: these tests take north/south/east/west or up/down/left/right and don't care about capitalization.

Observed moves list:
{}'''
        rawMoves = self.fifteenSolver.movesToSolve()

        # default to x.lower() if x.lower() isn't in self.cardinal.keys()
        try:
            observedDirections = [self.cardinal[x.lower()] for x in rawMoves]
        except KeyError:
            observedDirections = [x.lower() for x in rawMoves]

        message = m.format(rawMoves)
        goodDirections = all(
            [x in ['left', 'right', 'up', 'down'] for x in observedDirections])
        self.assertTrue(goodDirections, message)

    def testDoMovesWork(self):
        m = '''Following PuzzleSolver's reported moves doesn't actually solve the puzzle! Check your movesToSolve() and solve() methods.

Input:
{}

Reported moves from movesToSolve():
{}

After following the moves above, we got:
{}

The actual solution:
{}'''
        rawMoves = self.fifteenSolver.movesToSolve()

        # default to x.lower() if x.lower() isn't in self.cardinal.keys()
        moves = [self.cardinal.get(x.lower(), x.lower()) for x in rawMoves]

        initialVisual = str(self.fifteenInitial)
        for move in moves:

            # table-driven testing is hip now, just ask Nigel Tao (or ken)
            # https://code.google.com/p/go-wiki/wiki/TableDrivenTests
            moveFunctions = {
                "up": self.fifteenInitial.moveUp,
                "down": self.fifteenInitial.moveDown,
                "left": self.fifteenInitial.moveLeft,
                "right": self.fifteenInitial.moveRight,
            }

            # execute the tabled function
            self.fifteenInitial = moveFunctions[move]()

        message = m.format(initialVisual, rawMoves, self.fifteenInitial,
                           self.fifteenGoal)
        self.assertEquals(self.fifteenInitial, self.fifteenGoal, message)

    def testAlreadySolvedMoves(self):
        m = '''PuzzleSolver did something unexpected when the initial and goal PuzzleStates are the same.

These tests expect an empty list of moves or None to be returned from movesToSolve() in this case.

Observed moves list:
{}'''

        expectedMoves = ([], None)
        observedMoves = self.redundantSolver.movesToSolve()
        message = m.format(observedMoves)
        self.assertIn(observedMoves, expectedMoves, message)

    def testAlreadySolvedSolve(self):
        m = '''PuzzleSolver did something unexpected when the initial and goal PuzzleStates are the same.

These tests expect solve() to return a list containing only one item: the initial/goal state.

Observed solution:
{}'''
        expectedSoln = [self.fifteenInitial]
        observedSoln = self.redundantSolver.solve()
        message = m.format("\n".join([str(x) for x in observedSoln]))
        self.assertEquals(observedSoln, expectedSoln, message)