Beispiel #1
0
    def __init__(self, name, path):
        self._name = name
        self._tiles = []
        self._rootState = Position((0, 0))
        self._goalState = Position((0, 0))

        self.readFromFile(path)
        self._image = TileWorldImage(16*len(self._tiles[0]), 16*len(self._tiles))
Beispiel #2
0
class TileWorld(Environment):
    """
    Defines the 2D world
    """
    def __init__(self, name, path):
        self._name = name
        self._tiles = []
        self._rootState = Position((0, 0))
        self._goalState = Position((0, 0))

        self.readFromFile(path)
        self._image = TileWorldImage(16*len(self._tiles[0]), 16*len(self._tiles))

    def draw(self, path, closedStates, openStates):
        """
        Draws the 2D world to a Pillow image
        """
        self._image.draw(self._tiles, path, closedStates, openStates)

    def showImage(self):
        self._image.show()

    def save(self, path):
        """
        Saves the image to path
        """
        self._image.save(path)

    def name(self):
        return self._name

    def readFromFile(self, path):
        with open(path) as boardString:
            for line in boardString:
                self._tiles.append(line.strip('\n'))


        for y in range(0, len(self._tiles)):
            for x in range(0, len(self._tiles[y])):
                if self._tiles[y][x] == 'A':
                    self._rootState = Position((x, y))

                elif self._tiles[y][x] == 'B':
                    self._goalState = Position((x, y))

    def goalCost(self, state):
        return abs(self._goalState.x() - state.x()) + abs(self._goalState.y() - state.y())

    def root(self):
        return self._rootState

    def actions(self, state):
        """
        Returns the set of possible movements with associated costs
        """
        actions = set()
        costs = { 
                    '.' : 1,
                    'w' : 100,
                    'm' : 50,
                    'f' : 10,
                    'g' : 5,
                    'r' : 1,
                    'A' : 1,
                    'B' : 1
                    }

                    

        if state.x() > 0:
            if self._tiles[state.y()][state.x()-1] != '#':
                actions.add(Move('left', costs[self._tiles[state.y()][state.x()-1]]))

        if state.x() < len(self._tiles[state.y()])-1:
            if self._tiles[state.y()][state.x()+1] != '#':
                actions.add(Move('right', costs[self._tiles[state.y()][state.x()+1]]))

        if state.y() > 0:
            if self._tiles[state.y()-1][state.x()] != '#':
                actions.add(Move('up', costs[self._tiles[state.y()-1][state.x()]]))

        if state.y() < len(self._tiles)-1:
            if self._tiles[state.y()+1][state.x()] != '#':
                actions.add(Move('down', costs[self._tiles[state.y()+1][state.x()]]))

        return actions

    def goalTest(self, state):
        if self._goalState == state:
            return True
        return False

    def successor(self, state, action):
        if action.ID() == 'left':
            return Position((state.x()-1, state.y()))
        elif action.ID() == 'right':
            return Position((state.x()+1, state.y()))
        elif action.ID() == 'up':
            return Position((state.x(), state.y()-1))
        elif action.ID() == 'down':
            return Position((state.x(), state.y()+1))

    def __str__(self):
        result = ''
        yCount = 0
        xCount = 0
        result += '  '
        for i in range(0, len(self._tiles[0])):
            result += str(xCount) + ' '
            xCount += 1

        result += '\n'

        for line in self._tiles:
            result += str(yCount) + ' '
            yCount += 1
            for char in line:
                result += char + ' '

            result += '\n'


        return result