diff --git a/search/__pycache__/movement_logic.cpython-38.pyc b/search/__pycache__/movement_logic.cpython-38.pyc index a3ebab1dd81cbec0f07ea7082efc74db478d0ed9..9f65da9ba42fb18bf302ce704b42b221a4821d45 100644 Binary files a/search/__pycache__/movement_logic.cpython-38.pyc and b/search/__pycache__/movement_logic.cpython-38.pyc differ diff --git a/search/__pycache__/search_algorithm.cpython-38.pyc b/search/__pycache__/search_algorithm.cpython-38.pyc index 8b71a1b293e250952f20fa14b089c534806b984d..1b11c6f9f79a7d13a4f32d0ff668958ccc674c1b 100644 Binary files a/search/__pycache__/search_algorithm.cpython-38.pyc and b/search/__pycache__/search_algorithm.cpython-38.pyc differ diff --git a/search/movement_logic.py b/search/movement_logic.py index 4f3dd18da49b414ac0ed9648e481d95ebc11c3f5..164ada92aa85a9a500c2bef21977b7ce9d7dd702 100644 --- a/search/movement_logic.py +++ b/search/movement_logic.py @@ -1,6 +1,6 @@ """ -:argument coordinate - a tuple contain 2 number (r, q) -:return coordinate of the token after the action is complete +:pram 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. @@ -19,12 +19,14 @@ Slide action logic: # '-._.-'-._.-' # """ +from typing import Tuple + ROW = 0 COLUMN = 1 # 1 -def slide_left(coordinate: tuple[int, int]) -> tuple[int, int]: +def slide_left(coordinate: Tuple[int, int]) -> Tuple[int, int]: if coordinate: new_pos = (coordinate[ROW], coordinate[COLUMN] - 1) @@ -32,13 +34,13 @@ def slide_left(coordinate: tuple[int, int]) -> tuple[int, int]: return coordinate else: print("Empty coordinate") - return + return -10, -10 return new_pos # 2 -def slide_right(coordinate: tuple[int, int]) -> tuple[int, int]: +def slide_right(coordinate: Tuple[int, int]) -> Tuple[int, int]: if coordinate: new_pos = (coordinate[ROW], coordinate[COLUMN] + 1) @@ -47,14 +49,13 @@ def slide_right(coordinate: tuple[int, int]) -> tuple[int, int]: else: print("Empty coordinate") - return + return -10, -10 return new_pos # 3 -def slide_up_left(coordinate: tuple[int, int]) -> tuple[int, int]: - new_pos = () +def slide_up_left(coordinate: Tuple[int, int]) -> Tuple[int, int]: if coordinate: new_pos = (coordinate[ROW] + 1, coordinate[COLUMN] - 1) @@ -63,13 +64,13 @@ def slide_up_left(coordinate: tuple[int, int]) -> tuple[int, int]: else: print("Empty coordinate") - return + return -10, -10 return new_pos # 4 -def slide_up_right(coordinate: tuple[int, int]) -> tuple[int, int]: +def slide_up_right(coordinate: Tuple[int, int]) -> Tuple[int, int]: if coordinate: new_pos = (coordinate[ROW] + 1, coordinate[COLUMN]) @@ -78,13 +79,13 @@ def slide_up_right(coordinate: tuple[int, int]) -> tuple[int, int]: else: print("Empty coordinate") - return + return -10, -10 return new_pos # 5 -def slide_down_left(coordinate: tuple[int, int]) -> tuple[int, int]: +def slide_down_left(coordinate: Tuple[int, int]) -> Tuple[int, int]: if coordinate: new_pos = (coordinate[ROW] - 1, coordinate[COLUMN]) @@ -93,13 +94,13 @@ def slide_down_left(coordinate: tuple[int, int]) -> tuple[int, int]: else: print("Empty coordinate") - return + return -10, -10 return new_pos # 6 -def slide_down_right(coordinate: tuple[int, int]) -> tuple[int, int]: +def slide_down_right(coordinate: Tuple[int, int]) -> Tuple[int, int]: if coordinate: new_pos = (coordinate[ROW] - 1, coordinate[COLUMN] + 1) @@ -108,7 +109,7 @@ def slide_down_right(coordinate: tuple[int, int]) -> tuple[int, int]: else: print("Empty coordinate") - return + return -10, -10 return new_pos @@ -159,7 +160,7 @@ Swing action logic: """ -def swing_to_tile_1(piece: tuple[int, int], x: tuple[int, int]) -> tuple[int, int]: +def swing_to_tile_1(piece: Tuple[int, int], x: Tuple[int, int]) -> Tuple[int, int]: position = get_relative_position(piece, x) new_position = () if position == LEFT: @@ -188,7 +189,7 @@ def swing_to_tile_1(piece: tuple[int, int], x: tuple[int, int]) -> tuple[int, in return new_position -def swing_to_tile_2(piece: tuple[int, int], x: tuple[int, int]) -> tuple[int, int]: +def swing_to_tile_2(piece: Tuple[int, int], x: Tuple[int, int]) -> Tuple[int, int]: position = get_relative_position(piece, x) new_position = () @@ -218,7 +219,7 @@ def swing_to_tile_2(piece: tuple[int, int], x: tuple[int, int]) -> tuple[int, in return new_position -def swing_to_tile_3(piece: tuple[int, int], x: tuple[int, int]) -> tuple[int, int]: +def swing_to_tile_3(piece: Tuple[int, int], x: Tuple[int, int]) -> Tuple[int, int]: position = get_relative_position(piece, x) new_position = () @@ -248,7 +249,7 @@ def swing_to_tile_3(piece: tuple[int, int], x: tuple[int, int]) -> tuple[int, in return new_position -def get_relative_position(piece: tuple[int, int], x: tuple[int, int]) -> tuple[int, int]: +def get_relative_position(piece: Tuple[int, int], x: Tuple[int, int]) -> int: """ :param x: the adjacent game object :argument piece: the selected game object that is presented by its position on board { (row, column)} @@ -281,17 +282,16 @@ def get_relative_position(piece: tuple[int, int], x: tuple[int, int]) -> tuple[i return DOWN_RIGHT -def compare_tile(tile1: tuple[int, int], tile2: tuple[int, int]) -> bool: +def compare_tile(tile1: Tuple[int, int], tile2: Tuple[int, int]) -> bool: """ :param tile2: compose of (row, column) :param tile1: compose of (row, column) :return true if both of the row and column are matches between 2 tiles """ - return tile1[ROW] == tile2[ROW] \ - and tile1[COLUMN] == tile2[COLUMN] + return tile1[ROW] == tile2[ROW] and tile1[COLUMN] == tile2[COLUMN] -def check_within_board(tile: tuple[int, int]) -> bool: +def check_within_board(tile: Tuple[int, int]) -> bool: """ ensure that the tile is exist within the board :param tile: coordinate of the tile @@ -310,7 +310,7 @@ def check_within_board(tile: tuple[int, int]) -> bool: return True -def distance_between(piece: tuple[int, int], target: tuple[int, int]) -> bool: +def distance_between(piece: Tuple[int, int], target: Tuple[int, int]) -> bool: """ :argument piece - compose of (row, column) :argument target - compose of (row, column) diff --git a/search/search_algorithm.py b/search/search_algorithm.py index 9bfa2de6c3b462f3240a46203391983e75efd69b..f970d67ca1f56b452dc9e8a504f735181709eff8 100644 --- a/search/search_algorithm.py +++ b/search/search_algorithm.py @@ -1,6 +1,8 @@ """ This module is hold all method relate to Search algorithm """ +from typing import Dict, Set, List + from search.movement_logic import * from search.search_algo import piece_collision @@ -21,7 +23,7 @@ ROW = 0 COLUMN = 1 A_WIN = 1 B_WIN = 2 -DRAW = 1 +DRAW = 0 MAX_DISTANCE = 10 FIRST_ENTRY = 1 RE_ENTRY = 1 @@ -29,9 +31,9 @@ HIGHEST_RANK = 0 # Global variable: # All the dictionary is made into global variable since all method is interact with them -upperPiecesDict: dict[str, tuple] = {} -lowerPiecesDict: dict[str, tuple] = {} -setBlocks: set[tuple, ...] = set() +upperPiecesDict: Dict[str, tuple] = {} +lowerPiecesDict: Dict[str, tuple] = {} +setBlocks = set() targetsDict = {} # keep track all the lower player's piece <- to ensure no two Upper piece can target only one distinguish lower piece @@ -41,7 +43,7 @@ targetedPiece = set() # this help rank the action. positionHistory = {} -board: dict[tuple:str] = {} +board: Dict[Tuple[int, int], str] = {} ''' METHOD ''' @@ -89,7 +91,7 @@ def make_board(): board[block] = BLOCK -def check_valid_action(piece: str, new_position: tuple) -> bool: +def check_valid_action(piece: str, new_position: Tuple[int, int]) -> bool: """ check if the action is resolved successfully. -> no piece should defeat any other piece unless the one whom it defeated is its Target. @@ -116,7 +118,7 @@ def check_valid_action(piece: str, new_position: tuple) -> bool: return True -def check_in(position: tuple, piece: str): +def check_in(position: Tuple[int, int], piece: str): """ log each time piece visit a tile. -> if the piece comeplete its journey {arrived at its Target's location} @@ -131,7 +133,7 @@ def check_in(position: tuple, piece: str): positionHistory[piece][position] += RE_ENTRY -def rank(position: tuple, piece: str) -> int: +def rank(position: Tuple[int, int], piece: str) -> int: """ rank is base on how far of interested position to piece's target position -> how ever position is will be rank lower if it is visit before @@ -161,7 +163,7 @@ def result_of_collision(pieceA: str, pieceB: str) -> int: """ pieceA = pieceA[TYPE].lower() pieceB = pieceB[TYPE].lower() - if pieceA is LOWER_ROCK: + if pieceA == LOWER_ROCK: if pieceB == LOWER_SCISSOR: return A_WIN elif pieceB == LOWER_PAPER: @@ -199,7 +201,7 @@ def slide_action(piece: str) -> list: return action_list -def swing_action(piece: str) -> list: +def swing_action(piece: str) -> List[Tuple[int, int]]: """ for each adjacent piece there will be at most 3 swing move. -> check those move to ensure they are valid @@ -257,7 +259,7 @@ def find_target(piece: str): targetedPiece.add(target) -def make_ranked_move_list(piece: str) -> list: +def make_ranked_move_list(piece: str) -> List[Tuple[int, int]]: """ add all the possible position after perform an action. sort them according to their rank{low to high}