def preprocess3(state):
    input_layer = np.zeros([4, 14], dtype=float)
    i = 0
    for direction in ['up', 'down', 'left', 'right']:
        board, mergeCount, maxMerging, highestMerg, moves = bc.slide(direction, copy(state))
        if board != state:
            input_layer[i] = calculate_heuristics(board, mergeCount, maxMerging, highestMerg, moves)
        # else: keep these values at 0
        i += 1
    return input_layer.flatten()
def moveRandom(b):
    bestDirection = 'none'
    valid_moves = []
    for direction in ['up', 'down', 'left', 'right']:
        nextBoard, nofMerges, maxMerging, highestMerg, moves = bc.slide( direction, copy(b.board) )
        # count = bc.slide( direction, copy(b.board) )
        if nextBoard != b.board:
            bestHeuristic = 1
            valid_moves.append(direction)
    if len(valid_moves)!=0:
        b.move( random.choice(valid_moves) )
        return 0
    else:
        # game_over(b)
        score = 2**max(b.board)
        print(score)
        return score
def logic():
    bestHeuristic = -1
    bestDirection = "none"

    for direction in ["up", "down", "left", "right"]:
        # for direction in ['down', 'left', 'right']:
        nextBoard, nofMerges, maxMerging, highestMerg = bc.slide(direction, copy(b.board))
        if nextBoard != b.board:

            # heuristic = expectimax( nextBoard, 6, 'board', nofMerges, maxMerging, highestMerg)
            nofEmpty = emptyTiles(nextBoard)
            if nofEmpty >= 10:
                heuristic = expectimax(nextBoard, 4, "board", nofMerges, maxMerging, highestMerg)
            elif nofEmpty >= 5:
                heuristic = expectimax(nextBoard, 5, "board", nofMerges, maxMerging, highestMerg)
            else:
                heuristic = expectimax(nextBoard, 5, "board", nofMerges, maxMerging, highestMerg)
            if heuristic > bestHeuristic:
                bestHeuristic = heuristic
                bestDirection = direction

    if bestHeuristic != -1:
        b.move(bestDirection)
    else:
        # orig_stdout = sys.stdout
        # f = file('testResults.txt', 'w')
        # sys.stdout = f

        print "----------------------------------"
        print "game over"
        # stop =  float(time.clock())
        # minutes = (stop - t0)/60
        # seconds = (stop - t0)%60
        print "Running time: ", time.clock()
        print "depth = ", depth
        print 2 ** max(b.board)
        print nearness, smooth, merge, gradient, edge, opencell

        # sys.stdout = orig_stdout
        # f.close()

        while True:
            b.window.update_view(b.board)
def generateMAXSuccessors(board):
    """
    Generate the boards that happen
    when pressing arrow up, down, left, right.
    Do not insert a new tile, only merge.
    """

    successors = []
    merges = []
    maxMergings = []
    highestMerges = []
    #directions = ['up', 'down', 'left', 'right']
    for direction in directions:
        succ = deepcopy(board)
        succ, nofMerges, maxMerging, highestMerg = bc.slide(direction, succ)

        # if succ == parent means no move, no changes after sliding therfore don't append as successor
        if succ != board:
            successors.append(succ)
            merges.append(nofMerges)
            maxMergings.append(maxMerging)
            highestMerges.append(highestMerg)

    return successors, merges, maxMergings, highestMerges