Select Git revision
Piece.cpython-36.pyc
search_algo.py 5.49 KiB
from search.movement_logic import slide_right, slide_left, slide_up_left, slide_up_right, slide_down_left, \
slide_down_right, compare_tile, distance_between
from search.util import print_board
BLOCK = ""
POSITION_CLOSEST_TO_TARGET = 0
def make_board(lowerPieces, upperPieces, setBlocks):
"""
create a board of the current game -> can do a position look up.
:param upperPieces: dictionary contain all the upper piece and its location
:param lowerPieces: dictionary contain all the lower piece and its location
:param setBlocks: all the block
:return: the dictionary represent the board
"""
board = {}
for piece in lowerPieces:
board[lowerPieces[piece]] = piece
for piece in upperPieces:
board[upperPieces[piece]] = piece
for block in setBlocks:
board[block] = BLOCK
return board
def get_stronger_piece(piece_type):
"""
Stronger piece is base on the type, even if the piece are from a player.
:param piece_type: the type of the piece that we are interested
:return: the type of the piece that stronger than the input piece
"""
if piece_type == 'R':
return 'p'
if piece_type == 'S':
return 'r'
return 's'
def add_slide_action(upperDict, piece, board):
"""
add all the valid slide action to a list
:param board: dictionary contain all piece and block
:param upperDict: contain detail about upper piece
:param piece: key of the interested piece
:return: un sorted list of all position as a result of slide action
"""
list = []
# check the right tile
appending_list = add_if_valid(list, piece, slide_right(upperDict[piece]), upperDict[piece], board)
# check the left tile
appending_list = add_if_valid(list, piece, slide_left(upperDict[piece]), upperDict[piece], board)
# check the up_left
appending_list = add_if_valid(list, piece, slide_up_left(upperDict[piece]), upperDict[piece], board)
# check the up_right
appending_list = add_if_valid(list, piece, slide_up_right(upperDict[piece]), upperDict[piece], board)
# check the down_left
appending_list = add_if_valid(list, piece, slide_down_left(upperDict[piece]), upperDict[piece], board)
# check the down_right
appending_list = add_if_valid(list, piece, slide_down_right(upperDict[piece]), upperDict[piece], board)
return appending_list
def add_if_valid(position_list, piece, new_position, piece_position, board):
"""
check if the move is valid.
:param position_list: list contain all added slide action from this turn
:param piece: the interested upper piece
:param new_position: position result of a slide action
:param piece_position: initial position of the piece.
:param board: dictionary contain all piece and block
:return: the list
"""
# check if the new position is result of a out of board action
if compare_tile(new_position, piece_position):
return position_list
if new_position in board.keys():
# check if the tile is occupied by a block
if board[new_position] == BLOCK:
return position_list
# check if the new position is occupied by piece that stronger
elif board[new_position] == get_stronger_piece(piece):
return position_list
# add the new position and return
position_list.append(new_position)
return position_list
def make_priority_list_of_action(upperDict, piece, targetDict, board):
"""
compile all possible action this piece can under go.
sort them base on the result distance relative to the piece's target
:param upperDict: use to read the position of the piece
:param piece: key of the piece
:param targetDict: dictionary contain the target of every upper piece
:param board: the dictionary of board
:return: the sorted list of position result from an action
"""
# add all the adjacent move to queue
position_list = add_slide_action(upperDict, piece, board)
# sort the list base on the how close it is to target
position_list.sort(key=(lambda x: distance_between(x, targetDict[piece])))
return position_list
def update_state(upperPieces, lowerPieces, setBlocks, targetDict):
"""
move the piece in a way that bring all piece closer to its target
# currently only in away that is work for one piece.
:param upperPieces: dictionary contain all the upper piece
:param lowerPieces: dictionary contain all the lower piece
:param setBlocks: dictionary contain all the block on the board
:param targetDict: map each piece to a target
:return: the updated upper piece
"""
# create the board in order to support faster look up
board = make_board(lowerPieces, upperPieces, setBlocks)
print_board(board)
# right now, i assume there only 1 piece, there will be more code combine all the queue.
for piece in upperPieces:
position_list = make_priority_list_of_action(upperPieces,piece, targetDict, board)
upperPieces[piece] = position_list[POSITION_CLOSEST_TO_TARGET]
return upperPieces
def check_if_piece_hit_target(upperPiece, targetDict):
"""
remove the target from the target dictionary if the upper piece is at its target location
:param upperPiece: contain all upper piece
:param targetDict: map upper piece to its lower target
:return: the updated target dictionary
"""
for piece in upperPiece:
if targetDict and compare_tile(upperPiece[piece], targetDict[piece]):
del targetDict[piece]
return targetDict