diff --git a/search/__pycache__/util.cpython-38.pyc b/search/__pycache__/util.cpython-38.pyc new file mode 100644 index 0000000000000000000000000000000000000000..358077ed01f7ac0cf1338c14ad240f11565c3341 Binary files /dev/null and b/search/__pycache__/util.cpython-38.pyc differ diff --git a/search/method.py b/search/method.py index 4b0a6c8f93f50dafcb1046fef868d1bada042c0d..9180ec1a126561c18b2bb6d6d13a477d59ec55b2 100644 --- a/search/method.py +++ b/search/method.py @@ -8,20 +8,29 @@ Read from JSON file: -> ensure format token to distinguish between upper and lower. -> handling file IO exception """ -def to_board(file): +def to_position_list(file): TYPE = 0 - X_COORD = 1 - Y_COORD = 2 + ROW = 1 + COLUMN = 2 + lower_s = 1 + lower_r = 1 + lower_p = 1 + upper_s =1 + upper_r =1 + upper_p =1 + b = 1 + ROCK = 'r' + PAPER = 'p' if not isinstance(file, str): print("Pass invalid argument to to_board()~!!!") exit(-1) try: # open the file handle by json module - FILE = open(file) - game = json.load(FILE) + initial = open(file) + game = json.load(initial) - board = {} + dict = {} # iterate through the entity who is not empty in order to add it type and coordinate to dictionary. for entity in game.keys(): @@ -29,18 +38,37 @@ def to_board(file): for token in game[entity]: if entity == 'upper': - board[(int(token[X_COORD]), int(token[Y_COORD]))] = token[TYPE].upper() + if token[TYPE] == ROCK: + dict[token[TYPE].upper() + str(upper_r)] = (int(token[ROW]), int(token[COLUMN])) + upper_r += 1 + if token[TYPE] == PAPER: + dict[token[TYPE].upper() + str(upper_p)] = (int(token[ROW]), int(token[COLUMN])) + upper_p += 1 + else: + dict[token[TYPE].upper() + str(upper_s)] = (int(token[ROW]), int(token[COLUMN])) + upper_s += 1 + elif entity == 'lower': + if token[TYPE] == ROCK: + dict[token[TYPE] + str(lower_r)] = (int(token[ROW]), int(token[COLUMN])) + upper_r += 1 + if token[TYPE] == PAPER: + dict[token[TYPE] + str(lower_p)] = (int(token[ROW]), int(token[COLUMN])) + upper_p += 1 + else: + dict[token[TYPE] + str(lower_s)] = (int(token[ROW]), int(token[COLUMN])) + upper_s += 1 else: - board[(int(token[X_COORD]), int(token[Y_COORD]))] = token[TYPE] + dict[token[TYPE] + str(b)] = (int(token[ROW]), int(token[COLUMN])) + b += 1 - FILE.close() - return board + initial.close() + return dict except IOError: print("There is an issue when try to open file") exit(-1) - +print(to_position_list("C:\\Users\\Fizec\\comp30024-project-1\\Test_case\\level_4\\2.json")) """ taken from https://www.redblobgames.com/grids/hexagons/ under section: double coordinates for doublewidth