Ejemplo n.º 1
0
def replaySequence(graphicalDisplay, sequence):
    ##########################################
    gameBoard = core.Board(8, 16)

    while 1:

        moveOptions = gameBoard.getMoveOptions()

        if len(moveOptions) == 0:
            return gameBoard.score, gameBoard.splitRecord
            break

        try:
            nextMove = sequence[len(gameBoard.splitRecord)]
        except IndexError:
            pass

        print("Move {0} of {1}: Split box #{2}".format(
            len(gameBoard.splitRecord), len(sequence), nextMove))

        #determine best moves
        best_indices = [
            i for i, x in enumerate(gameBoard.weights)
            if x == max(gameBoard.weights)
        ]

        graphicalDisplay.HighlightNextBox(nextMove)
        graphicalDisplay.HighlightAlgoBoxes(best_indices)
        graphicalDisplay.Update(gameBoard)

        userinput = input("Press any key to continue")
        if nextMove not in moveOptions:
            print("------ IMPOSSIBLE MOVE REQUESTED -----")
            break

        if gameBoard.weights[nextMove] != max(gameBoard.weights):
            print("~~~Made move not recommended by algorithm.~~~")
            indices = [
                i for i, x in enumerate(gameBoard.weights)
                if x == max(gameBoard.weights)
            ]
            print("Box(es) recommended:")
            print(indices)
            print("Weight of box(es) recommended:")
            print(max(gameBoard.weights))
            print("Weight of box chosen:")
            print(gameBoard.weights[nextMove])
            # print(gameBoard.weights)

        if userinput == "w":
            findWeights(gameBoard, weightverbose=1)
            userinput = input("Press any key to continue")

        core.makeMove(gameBoard, nextMove)
Ejemplo n.º 2
0
def getBoardFromSequence(sequence):
    # Recreate a game board by replaying the sequence that leads to it
    ##########################################
    gameBoard = core.Board(8, 16)
    tempSequence = sequence[:]

    while 1:
        if len(tempSequence) == 0:
            return gameBoard

        chosenBox = tempSequence.pop(0)
        core.makeMove(gameBoard, chosenBox)
Ejemplo n.º 3
0
def playSplitRandomly(gameBoard):
    #Play a single game of SPL-T
    ##########################################
    while 1:

        moveOptions = gameBoard.getMoveOptions()

        if len(moveOptions) == 0:
            return gameBoard.score, gameBoard.splitRecord

        nextMove = random.choice(moveOptions)

        core.makeMove(gameBoard, nextMove)
Ejemplo n.º 4
0
def TimeCalc():

    i = 0
    num_games = 100

    total_pre_move_time = 0
    total_move_time = 0
    total_cluster_time = 0
    total_weight_time = 0

    while i < num_games:
        splits = 0
        game_pre_move_time = 0
        game_move_time = 0
        game_cluster_time = 0
        game_weight_time = 0

        gameBoard = core_weight.Board()
        while 1:
            start = time.time()

            moveOptions = gameBoard.getMoveOptions()

            if len(moveOptions) == 0:
                break

            bestSplit = max(range(len(gameBoard.weights)),
                            key=gameBoard.weights.__getitem__)

            end = time.time()
            game_pre_move_time += end - start

            move_time, cluster_time, weight_time = core_weight.makeMove(
                gameBoard, bestSplit)
            game_move_time += move_time
            game_cluster_time += cluster_time
            game_weight_time += weight_time

            splits += 1

        total_pre_move_time += game_pre_move_time / splits
        total_move_time += game_move_time / splits
        total_cluster_time += game_cluster_time / splits
        total_weight_time += game_weight_time / splits

        print('Game ' + str(i), end='\r')
        i += 1

    total_pre_move_time = total_pre_move_time / num_games
    total_move_time = total_move_time / num_games
    total_cluster_time = total_cluster_time / num_games
    total_weight_time = total_weight_time / num_games

    print('Average time to execute weight selection/get move options = ' +
          str(total_pre_move_time))
    print('Average time to execute move = ' + str(total_move_time))
    print('Average time to calculate clusters = ' + str(total_cluster_time))
    print('Average time to calculate weights = ' + str(total_weight_time))
Ejemplo n.º 5
0
def playSplitWeighted(gameBoard):
    #Play a single game of SPL-T
    ##########################################
    while 1:

        moveOptions = gameBoard.getMoveOptions()

        if len(moveOptions) == 0:
            return gameBoard.score, gameBoard.splitRecord

        remainingDistance = random.random() * sum(gameBoard.weights)

        move = -1

        for i, weight in enumerate(gameBoard.weights):
            remainingDistance -= weight
            if remainingDistance < 0:
                move = i
                break

        core.makeMove(gameBoard, move)
Ejemplo n.º 6
0
def replaySequence(graphicalDisplay, sequence):
    ##########################################
    gameBoard = core.Board(8, 16)

    while 1:

        moveOptions = gameBoard.getMoveOptions()

        if len(moveOptions) == 0:
            return gameBoard.score, gameBoard.splitRecord
            break

        nextMove = sequence[len(gameBoard.splitRecord)]

        print("Move {0} of {1}: Split box #{2}".format(
            len(gameBoard.splitRecord), len(sequence), nextMove))

        graphicalDisplay.HighlightBox(nextMove)
        graphicalDisplay.Update(gameBoard)

        tempDelay = input("Press any key to continue")
        if nextMove not in moveOptions:
            print("------ IMPOSSIBLE MOVE REQUESTED -----")
            break

        if gameBoard.weights[nextMove] != max(gameBoard.weights):
            print("~~~Made move not recommended by algorithm.~~~")
            indices = [
                i for i, x in enumerate(gameBoard.weights)
                if x == max(gameBoard.weights)
            ]
            print("Box(es) recommended:")
            print(indices)
            print("Weight of box(es) recommended:")
            print(max(gameBoard.weights))
            print("Weight of box chosen:")
            print(gameBoard.weights[nextMove])

        core.makeMove(gameBoard, nextMove)
Ejemplo n.º 7
0
        try:
            userinput = int(userinput)
        except ValueError:
            pass

        if userinput == "w":
            print(gameBoard.splitAction)
            for box, weight in enumerate(gameBoard.weights):
                if weight != 0: print("Weight of box %d is %d" % (box, weight))
            splitRequest = input("Input box to split:")
            try:
                splitRequest = int(splitRequest)
            except ValueError:
                splitRequest = bestSplit

            core.makeMove(gameBoard, splitRequest)
            boards.append(deepcopy(gameBoard))

            newAlgoMaxLength = playUntilEnd(deepcopy(gameBoard))
            print("New algorithm split record: %d" % newAlgoMaxLength)

        elif userinput == "ww":
            findWeights(gameBoard, weightverbose=1)
            splitRequest = input("Input box to split:")
            try:
                splitRequest = int(splitRequest)
            except ValueError:
                splitRequest = bestSplit

            core.makeMove(gameBoard, splitRequest)
            boards.append(deepcopy(gameBoard))
Ejemplo n.º 8
0
		else:
			weights.append(0)

	#to prevent negative weights, we take the lowest weight and add it to the entire list, making the worst block have no shot at being chosen 
	#and other negative blocks with a low chance at being chosen
	minWeight = min(weights)
	if minWeight <=0:
		minWeight = -minWeight
		weights = [x+minWeight+.01 for x in weights]

	for boxind, box in enumerate(gameBoard.box):
		if not box.splitPossible(gameBoard.splitAction):
			weights[boxind] = 0
	
	if weightverbose: 
		print("Final weights vector:") 
		print(weights)

	if timingInfo: 
		end = time.time()
		print("Time to calculate weights until fall for turn {}: {}".format(curr_splits,end-start))
	return weights

if __name__ == '__main__':
	gb = core.Board()
	core.makeMove(gb,0)
	core.makeMove(gb,0)
	core.makeMove(gb,0)
	core.makeMove(gb,0)
	core.makeMove(gb,1)
	findCluster(gb)
Ejemplo n.º 9
0
            weight_diff = game_board.weights[high_weight_indices[
                0]] - game_board.weights[high_weight_indices[1]]
        else:
            weight_diff = 100

        if piggyback and game_num == 0:  #if piggybacking, select moves from sequence at first
            best_split_ind = piggyback_sequence[len(game_board.splitRecord)]

        print("Game {}, Split {} - (restart {}, base {})".format(
            game_num, len(game_board.splitRecord), restart_splits,
            exploring_from),
              end='\r')

        old_game_board = copy.deepcopy(game_board)

        core.makeMove(game_board, best_split_ind)

        #After making the move, if the most recent block has points and
        #is not 1x1, then it made an unecessarily big cluster and we should add it
        #OR if there was another split option within 3 weight but not identical
        most_recent_box = game_board.box[-1]

        split_created_big_cluster = ((most_recent_box.points != 0)
                                     and (most_recent_box.width != 1
                                          or most_recent_box.height != 1))
        another_as_good_option = ((weight_diff < 3) and (weight_diff > 1)
                                  and len(game_board.splitRecord) > 875)

        #don't add all almost as good options because the problem becoms intractible
        add_every_blank_good_options = 2
        if another_as_good_option: