diff --git a/search/__pycache__/method.cpython-38.pyc b/search/__pycache__/method.cpython-38.pyc new file mode 100644 index 0000000000000000000000000000000000000000..5e38a9f00b59c12ca1756d8f39293229d8a97b76 Binary files /dev/null and b/search/__pycache__/method.cpython-38.pyc differ diff --git a/search/__pycache__/movement_logic.cpython-38.pyc b/search/__pycache__/movement_logic.cpython-38.pyc new file mode 100644 index 0000000000000000000000000000000000000000..624608e075463829c30dedab89a6cbc99f4338d0 Binary files /dev/null and b/search/__pycache__/movement_logic.cpython-38.pyc differ diff --git a/search/movement_logic.py b/search/movement_logic.py index 267eb46a15f128cf330f2345c3d76d07e93b2615..4a5ccc931dc73c2933d5afa4bedfd66a94df54e6 100644 --- a/search/movement_logic.py +++ b/search/movement_logic.py @@ -1,6 +1,9 @@ """ :argument coordinate - a tuple contain 2 number (r, q) :return coordinate of the token after the action is complete + if the new position is out of the board: + return the token' + s position instead. row = r column = q @@ -16,16 +19,17 @@ Slide action logic: # '-._.-'-._.-' # """ -row = 1 -column = 2 +row = 0 +column = 1 # 1 def slide_left(coordinate): - new_pos = () if coordinate: - new_pos.append(coordinate[row]) - new_pos.append(coordinate[column] - 1) + new_pos = (coordinate[row], coordinate[column] - 1) + + if not check_within_board(new_pos): + return coordinate else: print("Empty coordinate") return new_pos @@ -33,10 +37,12 @@ def slide_left(coordinate): # 2 def slide_right(coordinate): - new_pos = () if coordinate: - new_pos.append(coordinate[row]) - new_pos.append(coordinate[column] + 1) + new_pos = (coordinate[row], coordinate[column] + 1) + + if not check_within_board(new_pos): + return coordinate + else: print("Empty coordinate") return new_pos @@ -46,8 +52,11 @@ def slide_right(coordinate): def slide_up_left(coordinate): new_pos = () if coordinate: - new_pos.append(coordinate[row] + 1) - new_pos.append(coordinate[column] - 1) + new_pos = (coordinate[row] + 1, coordinate[column] - 1) + + if not check_within_board(new_pos): + return coordinate + else: print("Empty coordinate") return new_pos @@ -55,10 +64,12 @@ def slide_up_left(coordinate): # 4 def slide_up_right(coordinate): - new_pos = () if coordinate: - new_pos.append(coordinate[row] + 1) - new_pos.append(coordinate[column]) + new_pos = (coordinate[row] + 1, coordinate[column]) + + if not check_within_board(new_pos): + return coordinate + else: print("Empty coordinate") return new_pos @@ -66,10 +77,12 @@ def slide_up_right(coordinate): # 5 def slide_down_left(coordinate): - new_pos = () if coordinate: - new_pos.append(coordinate[row] - 1) - new_pos.append(coordinate[column]) + new_pos = (coordinate[row] - 1, coordinate[column]) + + if not check_within_board(new_pos): + return coordinate + else: print("Empty coordinate") return new_pos @@ -77,10 +90,12 @@ def slide_down_left(coordinate): # 6 def slide_down_right(coordinate): - new_pos = () if coordinate: - new_pos.append(coordinate[row] - 1) - new_pos.append(coordinate[column] + 1) + new_pos = (coordinate[row] - 1, coordinate[column] + 1) + + if not check_within_board(new_pos): + return coordinate + else: print("Empty coordinate") return new_pos @@ -93,10 +108,12 @@ UP_LEFT = 3 UP_RIGHT = 4 DOWN_LEFT = 5 DOWN_RIGHT = 6 +OUT_OF_BOARD = 0 """ :argument token - the selected game object that is presented by its position on board { (row, column)} x - the adjacent game object -:return new position after the action is complete +:return new position after the action is complete + if the new position is out of the board return the token's position instead Swing action logic: x is the another token that current token swing from @@ -132,67 +149,88 @@ Swing action logic: def swing_to_tile_1(token, x): position = get_relative_position(token, x) + if position == LEFT: - return slide_down_left(slide_left(token)) + new_postion = slide_down_left(slide_left(token)) if position == RIGHT: - return slide_up_right(slide_right(token)) + new_postion = slide_up_right(slide_right(token)) if position == UP_RIGHT: - return slide_up_left(slide_up_right(token)) + new_postion = slide_up_left(slide_up_right(token)) if position == UP_LEFT: - return slide_left(slide_up_left(token)) + new_postion = slide_left(slide_up_left(token)) if position == DOWN_LEFT: - return slide_down_right(slide_down_left(token)) + new_postion = slide_down_right(slide_down_left(token)) if position == DOWN_RIGHT: - return slide_right(slide_down_right(token)) + new_postion = slide_right(slide_down_right(token)) + + #if the position of the token after the action complete is out of board, return the token position in + #stead + if compare_tile(new_postion, x): + return token + + return new_postion def swing_to_tile_2(token, x): position = get_relative_position(token, x) if position == LEFT: - return slide_left(slide_left(token)) + new_postion = slide_left(slide_left(token)) if position == RIGHT: - return slide_right(slide_right(token)) + new_postion = slide_right(slide_right(token)) if position == UP_RIGHT: - return slide_up_right(slide_up_right(token)) + new_postion = slide_up_right(slide_up_right(token)) if position == UP_LEFT: - return slide_up_left(slide_up_left(token)) + new_postion = slide_up_left(slide_up_left(token)) if position == DOWN_LEFT: - return slide_down_left(slide_down_left(token)) + new_postion = slide_down_left(slide_down_left(token)) if position == DOWN_RIGHT: - return slide_down_right(slide_down_right(token)) + new_postion = slide_down_right(slide_down_right(token)) + + # if the position of the token after the action complete is out of board, return the token position in + # stead + if compare_tile(new_postion, x): + return token + return new_postion def swing_to_tile_3(token, x): position = get_relative_position(token, x) if position == LEFT: - return slide_up_left(slide_left(token)) + new_postion = slide_up_left(slide_left(token)) if position == RIGHT: - return slide_down_right(slide_right(token)) + new_postion = slide_down_right(slide_right(token)) if position == UP_RIGHT: - return slide_right(slide_up_right(token)) + new_postion = slide_right(slide_up_right(token)) if position == UP_LEFT: - return slide_up_right(slide_up_left(token)) + new_postion = slide_up_right(slide_up_left(token)) if position == DOWN_LEFT: - return slide_left(slide_down_left(token)) + new_postion = slide_left(slide_down_left(token)) if position == DOWN_RIGHT: - return slide_down_left(slide_down_right(token)) + new_postion = slide_down_left(slide_down_right(token)) + + # if the position of the token after the action complete is out of board, return the token position in + # stead + if compare_tile(new_postion, x): + return token + + return new_postion def get_relative_position(token, x): @@ -236,3 +274,22 @@ def compare_tile(tile1, tile2): """ return tile1[row] == tile2[row] \ and tile1[column] == tile2[column] + + +def check_within_board(tile): + """ + ensure that the tile is exist within the board + :param tile: coordinate of the tile + :return: true if the tile exist within the board, else false + """ + if tile[row] < -4 or tile[row] > 4: + return False + if tile[column] < -4 or tile[column] > 4: + return False + + if tile[column] in [-4, -3, -2, -1] and tile[row] < -4 - tile[column]: + return False + + if tile[column] in [4, 3, 2, 1] and tile[row] > 4 - tile[column]: + return False + return True