diff --git a/search/__pycache__/main.cpython-38.pyc b/search/__pycache__/main.cpython-38.pyc new file mode 100644 index 0000000000000000000000000000000000000000..84aa148a1abe1e015af49a4114970573a6d5286e Binary files /dev/null and b/search/__pycache__/main.cpython-38.pyc differ diff --git a/search/__pycache__/method.cpython-38.pyc b/search/__pycache__/method.cpython-38.pyc deleted file mode 100644 index 0400de112aa4ed4e6f0ff7a2edbb169348170aad..0000000000000000000000000000000000000000 Binary files a/search/__pycache__/method.cpython-38.pyc and /dev/null differ diff --git a/search/main.py b/search/main.py index bee7a5c6deb0a7162d3992cd0db79fdc50457e02..3f9282a9476de9ef173b902209ae09770ba2cb1b 100644 --- a/search/main.py +++ b/search/main.py @@ -26,25 +26,29 @@ def main(): except IndexError: print("usage: python3 -m search path/to/input.json", file=sys.stderr) sys.exit(1) + + extract_initial_object_into_container(data) + print(dictpieces) + + + + # TODO: + # Find and print a solution to the board configuration described + # by `data`. + # Why not start by trying to print this configuration out using the + # `print_board` helper function? (See the `util.py` source code for + # usage information). + +def extract_initial_object_into_container(data): # We can put the code to read the file NOT in the try/except statement because # if the file couldn't be read the process would end anyway # data is a dictionary with keys "upper", "lower" and "block" # pieces in dictionary, blocks in set, we can use if position in setblocks - - # XUAN: please have a look at the read_position_list() in method module. This should be on another abstraction - # level? for readability: else the main method will be too long? I like how u place Block in a set. - # Can we just add the set of block into the dictionary ?since we know for sure that any time we want to do - # anything with the object{take action}.We sure need to know where the block is.like: - # { '' : {(pos1, pos2), ..} - # ....} - # => this way later on we will only need to pass the dictionary around knowing that all the block is there - # in case we need it - # - # e.g: object_dict = read_position_list(sys.argv[1]) ? initialpiecesupper = data["upper"] initialpieceslower = data["lower"] initialblocks = data["block"] - nump, numr, nums = 0,0,0 + + nump, numr, nums = 0, 0, 0 keywrite = "" for piece in initialpiecesupper: if piece[0] == "p": @@ -56,8 +60,8 @@ def main(): else: nums = nums + 1 keywrite = "S" + str(nums) - dictpieces[keywrite] = (piece[1],piece[2]) - nump, numr, nums = 0,0,0 + dictpieces[keywrite] = (piece[1], piece[2]) + nump, numr, nums = 0, 0, 0 for piece in initialpieceslower: if piece[0] == "p": nump = nump + 1 @@ -68,16 +72,9 @@ def main(): else: nums = nums + 1 keywrite = "s" + str(nums) - dictpieces[keywrite] = (piece[1],piece[2]) + dictpieces[keywrite] = (piece[1], piece[2]) for block in initialblocks: - setblocks.add((block[1],block[2])) - - # TODO: - # Find and print a solution to the board configuration described - # by `data`. - # Why not start by trying to print this configuration out using the - # `print_board` helper function? (See the `util.py` source code for - # usage information). + setblocks.add((block[1], block[2])) # Our upper pieces are R, P and S, lower pieces are r, p and s diff --git a/search/method.py b/search/method.py deleted file mode 100644 index 14050d069d617d75b2d562b4ae3375aaa0d5f894..0000000000000000000000000000000000000000 --- a/search/method.py +++ /dev/null @@ -1,107 +0,0 @@ -import json - - -ROCK = 'r' -PAPER = 'p' -TYPE = 0 -ROW = 1 -COLUMN = 2 - -# This isn't reaaly needed, check main file -def read_position_list(file): - """ - :argument file - of the JSON file - :return a dictionary contain all all token and its position - the dictionary's format will be : - position_dict = { - "c#" = (row, column), - ... - } - -> For the key: 'c#' e.g: 'R1' - c -character indicate which type it is {rock -> 'r' - ,paper -> 'p' - ,scissor -> 's' - ,block -> ''} - if character is upper case : it belong to Upper player. - if character is lower case: it is belong to Lower player - # - number to identify the specific token in the set of token with same type. - start from 1. - - :except IOError - in case there are no file with the input path - """ - R = 0 - P = 0 - S = 0 - r = 0 - p = 0 - s = 0 - - if not isinstance(file, str): - print("Pass invalid argument to to_board()~!!!") - exit(1) - try: - # open the file handle by json module - initial = open(file) - game = json.load(initial) - initial.close() - except IOError: - #this is line of code from SCOTT's simmiliar way to handle this exception - print("usage: python3 -m search path/to/input.json") - exit(1) - - dict = {} - dict[''] = set() - # iterate through the entity who is not empty in order to add it type and coordinate to dictionary. - # taken Scott's approach of splitting the read file into 3 subsection: base on which object it is{upper, lower - # block.} - #also: Scott's approach of bringing the except to the load data only and using set instead of list - for entity in game.keys(): - - if game[entity]: - for token in game[entity]: - - if entity == 'upper': - if token[TYPE] == ROCK: - dict[token[TYPE].upper() + str(R + 1)] = (int(token[ROW]), int(token[COLUMN])) - - if token[TYPE] == PAPER: - dict[token[TYPE].upper() + str(P + 1)] = (int(token[ROW]), int(token[COLUMN])) - - else: - dict[token[TYPE].upper() + str(S + 1)] = (int(token[ROW]), int(token[COLUMN])) - - elif entity == 'lower': - if token[TYPE] == ROCK: - dict[token[TYPE] + str(r + 1)] = (int(token[ROW]), int(token[COLUMN])) - - if token[TYPE] == PAPER: - dict[token[TYPE] + str(p + 1)] = (int(token[ROW]), int(token[COLUMN])) - - else: - dict[token[TYPE] + str(s + 1)] = (int(token[ROW]), int(token[COLUMN])) - - else: - dict[''].add((int(token[ROW]), int(token[COLUMN]))) - - - return dict - - -def distance_between(Upper_token, Lower_token): - """ - :argument Upper_token - compose of (row, column) - :argument Lower_token - compose of (row, column) - :return the number of step from upper's token to lower's token - - Taken from https://www.redblobgames.com/grids/hexagons/ - under section: double coordinates for doublewidth - -> - """ - dx = abs(Upper_token[0] - Lower_token[0]) - dy = abs(Upper_token[1] - Lower_token[1]) - - return dx + max(0, (dy - dx) / 2) - - - - diff --git a/search/movement_logic.py b/search/movement_logic.py index fbfd26fe48c6781ab080f0f6f6c66aa5e310939c..0943875610d76ca053af1b879ac208641ebcfbef 100644 --- a/search/movement_logic.py +++ b/search/movement_logic.py @@ -293,3 +293,21 @@ def check_within_board(tile): if tile[column] in [4, 3, 2, 1] and tile[row] > 4 - tile[column]: return False return True + + + +def distance_between(Upper_token, Lower_token): + """ + :argument Upper_token - compose of (row, column) + :argument Lower_token - compose of (row, column) + :return the number of step from upper's token to lower's token + + Taken from https://www.redblobgames.com/grids/hexagons/ + under section: double coordinates for doublewidth + -> + """ + dx = abs(Upper_token[0] - Lower_token[0]) + dy = abs(Upper_token[1] - Lower_token[1]) + + return dx + max(0, (dy - dx) / 2) + diff --git a/search/search_algorithm.py b/search/search_algorithm.py index ffcad3c52daa066636289683a51574f5e48af2f2..3e544625827b782e54eda59c8ac1db3014bd0797 100644 --- a/search/search_algorithm.py +++ b/search/search_algorithm.py @@ -2,9 +2,8 @@ logic: -> prioritise the action which take the token closer to its target """ -from search.method import distance_between from search.movement_logic import slide_right, slide_left, slide_up_left, slide_up_right, slide_down_left, \ - slide_down_right + slide_down_right, distance_between def add_to_queue(record, queue, token_position, target):