diff --git a/search/search_algorithm.py b/search/search_algorithm.py index f970d67ca1f56b452dc9e8a504f735181709eff8..c9c494335acef957132af036ac550be500fbe389 100644 --- a/search/search_algorithm.py +++ b/search/search_algorithm.py @@ -287,48 +287,51 @@ def perform_optimal_combination(move_list: dict): -> then choose action from the remained piece in a way that not cause collision :param move_list: contain all the pottential moved for all piece """ - # perform all the action that directly lead the piece to its target. - priority_list = [] + priority_list = [] + # For each piece who has a target, move all the piece that after take this action will lead directly to its target. + # Keep the rest in the another list {so that we can sort them and , move each of them} for piece in move_list: if piece in targetsDict.keys() and compare_tile(move_list[piece][HIGHEST_RANK], targetsDict[piece]): - # perform swap and update board + # found a piece !!!perform swap and update board del board[upperPiecesDict[piece]] upperPiecesDict[piece] = targetsDict[piece] - board[targetsDict[piece]] = piece + else: + # this piece is not matching. priority_list.append(piece) - # sort the list base on number of action. + # sort the list of remained piece base on the number of option they have {a.k.a move(s)} priority_list.sort(key=(lambda key: len(move_list[key]))) - # perform an action that does not result in a collision. + # from those remained piece, move the least option first. for piece in priority_list: index = HIGHEST_RANK moved = False + # each piece have to move once and only once while not moved: if index >= len(move_list[piece]): break + # if current action lead to that piece collide with another piece, abandon it and go examine next action if move_list[piece][index] in board.keys() and\ result_of_collision(board[move_list[piece][index]], piece) != DRAW: index += 1 continue - # perform the action + # Found a move that does not cause collision if upperPiecesDict[piece] in board.keys(): del board[upperPiecesDict[piece]] upperPiecesDict[piece] = move_list[piece][index] board[move_list[piece][index]] = piece - moved = True + # log that this piece had visit this tile <- help with ranking its option. check_in(move_list[piece][index], piece) - - + moved = True def take_turn(): """