Ejemplo n.º 1
0
def bestMove(state, player, lookahead):
    bestMoveScore = score(state, player)
    bestMoves = []
    for move in MoveUtils.getLegalMoves(state, player):
        state.applyMove(move, player)
        if lookahead == 0:
            moveScore = score(state, player)
        else:
            # List of (move, player)
            otherPlayerMoves = []
            for i in range(3):
                otherPlayer = (player + i + 1) % 4
                otherPlayerMove, _ = bestMove(state, otherPlayer, 0)
                otherPlayerMoves.append((otherPlayerMove, otherPlayer))
                state.applyMove(otherPlayerMove, otherPlayer)
            moveScore = bestMove(state, player, lookahead - 1)[1]
            for otherPlayerMove, otherPlayer in reversed(otherPlayerMoves):
                state.undoMove(otherPlayerMove, otherPlayer)
        state.undoMove(move, player)
        if moveScore >= bestMoveScore:
            if moveScore > bestMoveScore:
                bestMoves = []
                bestMoveScore = moveScore
            bestMoves.append(move)

    toReturn = None
    if len(bestMoves) > 0:
        toReturn = random.choice(bestMoves)

    return (toReturn, bestMoveScore)
Ejemplo n.º 2
0
def getMove(state, player):
    bestMoveScore = None
    bestMove = None
    for move in MoveUtils.getLegalMoves(state, player):
        state.applyMove(move, player)
        moveScore = score(state, player)
        state.undoMove(move, player)
        if (not bestMoveScore) or (moveScore > bestMoveScore):
            bestMove = move
            bestMoveScore = moveScore
    return bestMove
Ejemplo n.º 3
0
def getMove(state, player):
    bestMoveScore = weightedScore(state, player)
    bestMoves = []
    for move in MoveUtils.getLegalMoves(state, player):
        state.applyMove(move, player)
        moveScore = weightedScore(state, player)
        state.undoMove(move, player)
        if moveScore >= bestMoveScore:
            if moveScore > bestMoveScore:
                bestMoves = []
                bestMoveScore = moveScore
            bestMoves.append(move)
    if len(bestMoves) > 0:
        return random.choice(bestMoves)
    else:
        return None