Skip to content
Snippets Groups Projects
Commit 714a9746 authored by Xuan Trinh's avatar Xuan Trinh
Browse files

When process over the priority list of all possible moves of every piece,...

When process over the priority list of all possible moves of  every piece, prioritise the one with the least option, XUAN 11:30PM 27 Mar 2021
parent c62ba7bb
No related branches found
No related tags found
No related merge requests found
No preview for this file type
...@@ -136,11 +136,17 @@ def make_priority_list_of_action(upperDict, piece, targetDict, board): ...@@ -136,11 +136,17 @@ def make_priority_list_of_action(upperDict, piece, targetDict, board):
# add all the swing action to queue # add all the swing action to queue
position_list.extend(add_swing_action(upperDict, piece, board)) position_list.extend(add_swing_action(upperDict, piece, board))
print(visit_record)
# sort the list base on the how close it is to target # sort the list base on the how close it is to target
if piece in targetDict.keys(): if piece in targetDict.keys():
position_list.sort(key=(lambda x: rank_by_appeal(x, targetDict,piece))) position_list.sort(key=(lambda x: rank_by_appeal(x, targetDict,piece)))
if position_list[0] in visit_record[piece].keys():
if visit_record[piece][position_list[0]] > 4 and piece in targetDict.keys():
# check all the move in the visit list, if all above 4, then this piece cant reach the target.
for position in position_list[1::]:
if visit_record[piece][position] < 4:
return position_list
del targetDict[piece]
return position_list return position_list
...@@ -179,6 +185,7 @@ def choose_optimal_combination(possible_action, upperPieces, board, targetDict): ...@@ -179,6 +185,7 @@ def choose_optimal_combination(possible_action, upperPieces, board, targetDict):
:return: :return:
""" """
finished = set() finished = set()
priority_list = []
for piece in possible_action.keys(): for piece in possible_action.keys():
if piece in possible_action.keys(): if piece in possible_action.keys():
...@@ -191,14 +198,19 @@ def choose_optimal_combination(possible_action, upperPieces, board, targetDict): ...@@ -191,14 +198,19 @@ def choose_optimal_combination(possible_action, upperPieces, board, targetDict):
board[targetDict[piece]] = piece board[targetDict[piece]] = piece
# we've finished with this piece. # we've finished with this piece.
finished.add(piece) finished.add(piece)
else:
priority_list.append(piece)
priority_list.sort(key=(lambda x: len(possible_action[x])))
# each piece is moved once and only once. # each piece is moved once and only once.
for piece in possible_action: for piece in priority_list:
if piece in finished: if piece in finished:
continue continue
index = 0 index = 0
moved = False moved = False
while not moved: while not moved:
if index >= len(possible_action[piece]):
break;
# check if the move exist in the board # check if the move exist in the board
if possible_action[piece][index] not in board.keys(): if possible_action[piece][index] not in board.keys():
# perform swap and update board # perform swap and update board
...@@ -418,5 +430,5 @@ def rank_by_appeal(x, targetDict, piece): ...@@ -418,5 +430,5 @@ def rank_by_appeal(x, targetDict, piece):
reduce_factor = visit_record[piece][x] reduce_factor = visit_record[piece][x]
adjust_score = base + reduce_factor adjust_score = base + reduce_factor
print(str(x) + ": " + str(adjust_score))
return adjust_score return adjust_score
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment