예제 #1
0
    def getAdjacentStateList(self, currentState):
        adjacentStateList = list()

        blueCell, aliveCellList = currentState.blueCell, currentState.aliveCellList

        aliveCellSet = set(aliveCellList)
        for directionTag, (du, dv) in self.allowedMovementCodes.items():
            u, v = blueCell
            newBlueCell = u + du, v + dv

            if self.isCellOutsideGrid(
                    newBlueCell) or newBlueCell in aliveCellSet:
                continue

            aliveCellSet.add(newBlueCell)
            nextGenerationAliveCells = self._nextGeneration(aliveCellSet)
            aliveCellSet.remove(newBlueCell)

            if newBlueCell in nextGenerationAliveCells:
                nextGenerationAliveCells.remove(newBlueCell)
                move = Move(moveCode=directionTag, moveDistance=None)
                newSearchState = MazeOfLifeSearchState(
                    newBlueCell,
                    list(nextGenerationAliveCells),
                    previousMove=move,
                    previousState=currentState)
                adjacentStateList.append(newSearchState)

        return adjacentStateList
예제 #2
0
    def getAdjacentStateList(self, currentState):
        adjacentStateList = list()

        row, col = currentState.cell
        currentPiece = self.mazeLayout.getRaw(row, col)
        if currentPiece == self.pawnToken:
            assert currentState.previousMove is not None
            currentPiece = currentState.previousMove.moveCode
        movementCodeDict = self.chessMovementCode[currentPiece]

        unit = movementCodeDict['unit']
        cellDeltaList = movementCodeDict['cellList']

        exploreList = list()

        for du, dv in cellDeltaList:
            adjacentCell = u, v = row + du, col + dv
            if self.isCellOutsideGrid(adjacentCell):
                continue
            if self.mazeLayout.getRaw(u, v) == self.emptyCell:
                exploreList.append((du, dv))
                continue
            move = Move(moveCode=currentPiece, moveDistance=None)
            newSearchState = SearchState(adjacentCell,
                                         previousMove=move,
                                         previousState=currentState)
            adjacentStateList.append(newSearchState)

        if unit is not None:
            for du, dv in exploreList:
                u, v = row, col
                while True:
                    adjacentCell = u, v = u + du, v + dv
                    if self.isCellOutsideGrid(adjacentCell):
                        break
                    if self.mazeLayout.getRaw(u, v) != self.emptyCell:
                        move = Move(moveCode=currentPiece, moveDistance=None)
                        newSearchState = SearchState(
                            adjacentCell,
                            previousMove=move,
                            previousState=currentState)
                        adjacentStateList.append(newSearchState)
                        break
        return filter(
            functools.partial(self.adjacentStateFilterFunc, currentState),
            adjacentStateList)
예제 #3
0
    def getAdjacentStateList(self, currentState):
        adjacentStateList = list()

        occupiedCellDict = dict()
        for cell in currentState.redMarbleCellList:
            occupiedCellDict[cell] = 'RED'
        for cell in currentState.blackMarbleCellList:
            occupiedCellDict[cell] = 'BLACK'

        for directionTag, (du, dv) in self.allowedMovementCodes.items():
            newOccupiedCellDict = dict()

            for row, col in self.scanOrderDict[directionTag]:
                if (row, col) not in occupiedCellDict:
                    continue
                # A marble is present in (row, col). Move it as far as possible in the direction specified by directionTag.
                # Also, check whether the marble reaches the target cell.
                marbleInTargetCell = False
                newCell = u, v = row, col

                while True:
                    nextCell = u + du, v + dv
                    if self.isCellOutsideGrid(nextCell) or (
                            newCell, nextCell
                    ) in self.blockedMoves or nextCell in newOccupiedCellDict:
                        break
                    newCell = u, v = nextCell
                    if newCell == self.targetCell:
                        marbleInTargetCell = True
                        break

                # newCell is the cell to which we have to move the marble at (row, col). If marbleInTargetCell
                # is True, then it disappears and is not added to newOccupiedCellDict.
                if not marbleInTargetCell:
                    newOccupiedCellDict[newCell] = occupiedCellDict[(row, col)]

            newRedMarbleCellList = list()
            newBlackMarbleCellList = list()
            for cell, color in newOccupiedCellDict.items():
                cellList = newRedMarbleCellList if color == 'RED' else newBlackMarbleCellList
                cellList.append(cell)

            move = Move(moveCode=directionTag, moveDistance=None)
            newSearchState = MarbleMazeSearchState(newRedMarbleCellList,
                                                   newBlackMarbleCellList,
                                                   previousMove=move,
                                                   previousState=currentState)
            adjacentStateList.append(newSearchState)
        return adjacentStateList
예제 #4
0
	def getAdjacentStateList( self, currentState ):
		adjacentStateList = list()

		for directionTag, (du, dv) in self.allowedMovementCodes.items():
			u, v = currentState.cell
			while True:
				x, y = u + du, v + dv
				if self.isCellOutsideGrid( (x, y) ) or (u, v, x, y) in self.blockedMoves:
					break
				u, v = x, y
			adjacentCell = u, v
			move = Move( moveCode=directionTag, moveDistance=None )
			newSearchState = SearchState( adjacentCell, previousMove=move, previousState=currentState )
			adjacentStateList.append( newSearchState )
		return adjacentStateList
예제 #5
0
    def getAdjacentStateList(self, currentState):
        adjacentStateList = list()

        for directionTag in self.allowedMoves:
            newRollingBlock = currentState.rollingBlock.move(directionTag)
            if all(map(self.isCellEmpty, newRollingBlock.cells())):
                # All cells occupied by newRollingBlock are empty, which indicates that a move is possible.
                move = Move(moveCode=directionTag, moveDistance=None)
                newSearchState = RollingBlockSearchState(
                    previousMove=move,
                    previousState=currentState,
                    rollingBlock=newRollingBlock)
                adjacentStateList.append(newSearchState)
        return filter(
            functools.partial(self.adjacentStateFilterFunc, currentState),
            adjacentStateList)
예제 #6
0
    def getAdjacentStateList(self, currentState):
        adjacentStateList = list()

        row, col = currentState.cell
        accumulator = currentState.accumulator
        for directionTag, (du, dv) in self.allowedMovementCodes.items():
            adjacentCell = x, y = row + du, col + dv
            if self.isCellOutsideGrid(adjacentCell):
                continue
            newAccumulator = self.apply(self.mazeLayout.getRaw(x, y),
                                        accumulator)
            move = Move(moveCode=directionTag, moveDistance=1)
            newSearchState = CalculationSearchState(adjacentCell,
                                                    previousMove=move,
                                                    previousState=currentState,
                                                    accumulator=newAccumulator)
            adjacentStateList.append(newSearchState)

        return filter(
            functools.partial(self.adjacentStateFilterFunc, currentState),
            adjacentStateList)
예제 #7
0
    def getAdjacentStateList(self, currentState):
        adjacentStateList = list()

        allowedMoves = self.getAllowedMoves(currentState)
        try:
            stepCount = self.getStepCount(currentState)
        except UseDefaultImplementation:
            stepCount = self._defaultStepCount(currentState)

        row, col = currentState.cell
        for directionTag, (du, dv) in self.allowedMovementCodes.items():
            if directionTag not in allowedMoves:
                continue
            adjacentCell = row + du * stepCount, col + dv * stepCount
            if self.isCellOutsideGrid(adjacentCell):
                continue
            move = Move(moveCode=directionTag, moveDistance=stepCount)
            newSearchState = SearchState(adjacentCell,
                                         previousMove=move,
                                         previousState=currentState)
            adjacentStateList.append(newSearchState)
        return adjacentStateList
예제 #8
0
 def __init__(self, moveCode, moveDistance, sequenceCount=1):
     Move.__init__(self, moveCode, moveDistance)
     self.sequenceCount = sequenceCount
     self.shape = self.color = None
     self.matchType = None
예제 #9
0
 def __init__(self, moveCode, moveDistance, sequenceCount=1):
     Move.__init__(self, moveCode, moveDistance)
     self.sequenceCount = sequenceCount