Esempio n. 1
0
def h2(n, goalState):
    state = n.state

    count = 0

    while state != goalState:
        emptyPos = getSpacePosition(state)
        emptyGoalPos = getSpacePosition(goalState)
        if emptyPos != emptyGoalPos:
            # Move right value in the position of the space
            value = goalState[emptyPos[0]][emptyPos[1]]
            rightValuePos = getValuePosition(state, value)
            state = swapValuesInPuzzle(emptyPos, rightValuePos, state)
        else:
            # Get wrong value position
            wrongValuePos = []
            for i in range(len(state)):
                for j in range(len(state[i])):
                    if state[i][j] != 0 and state[i][j] != goalState[i][j]:
                        wrongValuePos = [i, j]
                        break
            # Swap with empty position
            state = swapValuesInPuzzle(emptyPos, wrongValuePos, state)

        count += 1

    return count
Esempio n. 2
0
def h2(n, goalState):
    state = n.state

    count = 0

    while state != goalState:
        emptyPos = getSpacePosition(state)
        emptyGoalPos = getSpacePosition(goalState)
        if emptyPos != emptyGoalPos:
            # Move right value in the position of the space
            value = goalState[emptyPos[0]][emptyPos[1]]
            rightValuePos = getValuePosition(state, value)
            state = swapValuesInPuzzle(emptyPos, rightValuePos, state)
        else:
            # Get wrong value position
            wrongValuePos = []
            for i in range(len(state)):
                for j in range(len(state[i])):
                    if state[i][j] != 0 and state[i][j] != goalState[i][j]:
                        wrongValuePos = [i, j]
                        break
            # Swap with empty position
            state = swapValuesInPuzzle(emptyPos, wrongValuePos, state)

        count += 1

    return count
def makeRandomMove(state, n, lastAction) : 
    spacePos = getSpacePosition(state)
    validActions = getValidActions(spacePos, n)
    action = random.choice(validActions)
    
    # Do not go to previous position just yet.
    while True:
        if lastAction == "up":
            if action == "down":
                validActions.remove(action)
                action = random.choice(validActions)
            else:
                break
        elif lastAction == "down":
            if action == "up":
                validActions.remove(action)
                action = random.choice(validActions)
            else:
                break
        elif lastAction == "left":
            if action == "right":
                validActions.remove(action)
                action = random.choice(validActions)
            else:
                break
        elif lastAction == "right":
            if action == "left":
                validActions.remove(action)
                action = random.choice(validActions)
            else:
                break
        else:
            break

    lastAction = action
    
    spacePosition = getSpacePosition(state)
                 
    if action == "up" :
        targetPosition = [spacePosition[0] - 1, spacePosition[1]]
    elif action == "down" :
        targetPosition = [spacePosition[0] + 1, spacePosition[1]]
    elif action == "left" :
        targetPosition = [spacePosition[0], spacePosition[1] - 1]
    elif action == "right" :
        targetPosition = [spacePosition[0], spacePosition[1] + 1]
    else :
        print "ERROR: invalid action"
 
 
    retState = swapValuesInPuzzle(spacePosition, targetPosition, state)	
    return (retState, lastAction)
Esempio n. 4
0
def makeRandomMove(state, n, lastAction):
    spacePos = getSpacePosition(state)
    validActions = getValidActions(spacePos, n)
    action = random.choice(validActions)

    # Do not go to previous position just yet.
    while True:
        if lastAction == "up":
            if action == "down":
                validActions.remove(action)
                action = random.choice(validActions)
            else:
                break
        elif lastAction == "down":
            if action == "up":
                validActions.remove(action)
                action = random.choice(validActions)
            else:
                break
        elif lastAction == "left":
            if action == "right":
                validActions.remove(action)
                action = random.choice(validActions)
            else:
                break
        elif lastAction == "right":
            if action == "left":
                validActions.remove(action)
                action = random.choice(validActions)
            else:
                break
        else:
            break

    lastAction = action

    spacePosition = getSpacePosition(state)

    if action == "up":
        targetPosition = [spacePosition[0] - 1, spacePosition[1]]
    elif action == "down":
        targetPosition = [spacePosition[0] + 1, spacePosition[1]]
    elif action == "left":
        targetPosition = [spacePosition[0], spacePosition[1] - 1]
    elif action == "right":
        targetPosition = [spacePosition[0], spacePosition[1] + 1]
    else:
        print "ERROR: invalid action"

    retState = swapValuesInPuzzle(spacePosition, targetPosition, state)
    return (retState, lastAction)
Esempio n. 5
0
    def result(self, state, action):
        """ The state as a result of given action on given state"""

        spacePosition = getSpacePosition(state)

        if action == "up":
            targetPosition = [spacePosition[0] - 1, spacePosition[1]]
        elif action == "down":
            targetPosition = [spacePosition[0] + 1, spacePosition[1]]
        elif action == "left":
            targetPosition = [spacePosition[0], spacePosition[1] - 1]
        elif action == "right":
            targetPosition = [spacePosition[0], spacePosition[1] + 1]
        else:
            print "ERROR: invalid action"

        retState = swapValuesInPuzzle(spacePosition, targetPosition, state)

        # printState(retState)
        # print

        return retState
Esempio n. 6
0
    def result(self, state, action):
        """ The state as a result of given action on given state"""

        spacePosition = getSpacePosition(state)

        if action == "up":
            targetPosition = [spacePosition[0] - 1, spacePosition[1]]
        elif action == "down":
            targetPosition = [spacePosition[0] + 1, spacePosition[1]]
        elif action == "left":
            targetPosition = [spacePosition[0], spacePosition[1] - 1]
        elif action == "right":
            targetPosition = [spacePosition[0], spacePosition[1] + 1]
        else:
            print "ERROR: invalid action"

        retState = swapValuesInPuzzle(spacePosition, targetPosition, state)

        # printState(retState)
        # print

        return retState