diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml index db25fb7f0197e6d9c0ba94386084cc04e4198214..c1ac4c8943dd0b8cef2aa8fa9907ce11dad789aa 100644 --- a/.idea/inspectionProfiles/Project_Default.xml +++ b/.idea/inspectionProfiles/Project_Default.xml @@ -9,5 +9,13 @@ </list> </option> </inspection_tool> + <inspection_tool class="PyUnresolvedReferencesInspection" enabled="true" level="WARNING" enabled_by_default="true"> + <option name="ignoredIdentifiers"> + <list> + <option value="coordinate" /> + <option value="tuple.append" /> + </list> + </option> + </inspection_tool> </profile> </component> \ No newline at end of file diff --git a/search/method.py b/search/method.py index 517c09aeabed8caae681ee783008efbc7163851d..ed4d633eb340aaf7683e489c960e3b1fba02bb86 100644 --- a/search/method.py +++ b/search/method.py @@ -1,40 +1,42 @@ - -from search.util import print_board import json -""" -Read from JSON file: - -> load each piece into board's dictionary. - -> ensure format token to distinguish between upper and lower. - -> handling file IO exception - - format will be : - position_dict = { - "s1" = (row, column), - ... - } - -> for the key: 'c#' - c -character indicate which type it is {rock -> 'r' - ,paper -> 'p' - ,scissor -> 's' - ,block -> ''} - # - number to identify the specific token in the set of token with same type. - start from 1. -""" -def to_position_list(file): - TYPE = 0 - ROW = 1 - COLUMN = 2 - - lower_s = 1 - lower_r = 1 - lower_p = 1 - upper_s =1 - upper_r =1 - upper_p =1 + +ROCK = 'r' +PAPER = 'p' +TYPE = 0 +ROW = 1 +COLUMN = 2 + + +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 b = 1 - ROCK = 'r' - PAPER = 'p' + if not isinstance(file, str): print("Pass invalid argument to to_board()~!!!") exit(-1) @@ -48,46 +50,48 @@ def to_position_list(file): 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(upper_r)] = (int(token[ROW]), int(token[COLUMN])) - upper_r += 1 + dict[token[TYPE].upper() + str(R + 1)] = (int(token[ROW]), int(token[COLUMN])) + if token[TYPE] == PAPER: - dict[token[TYPE].upper() + str(upper_p)] = (int(token[ROW]), int(token[COLUMN])) - upper_p += 1 + dict[token[TYPE].upper() + str(P + 1)] = (int(token[ROW]), int(token[COLUMN])) + else: - dict[token[TYPE].upper() + str(upper_s)] = (int(token[ROW]), int(token[COLUMN])) - upper_s += 1 + dict[token[TYPE].upper() + str(S + 1)] = (int(token[ROW]), int(token[COLUMN])) + elif entity == 'lower': if token[TYPE] == ROCK: - dict[token[TYPE] + str(lower_r)] = (int(token[ROW]), int(token[COLUMN])) - upper_r += 1 + dict[token[TYPE] + str(r + 1)] = (int(token[ROW]), int(token[COLUMN])) + if token[TYPE] == PAPER: - dict[token[TYPE] + str(lower_p)] = (int(token[ROW]), int(token[COLUMN])) - upper_p += 1 + dict[token[TYPE] + str(p + 1)] = (int(token[ROW]), int(token[COLUMN])) + else: - dict[token[TYPE] + str(lower_s)] = (int(token[ROW]), int(token[COLUMN])) - upper_s += 1 + dict[token[TYPE] + str(s + 1)] = (int(token[ROW]), int(token[COLUMN])) + else: dict[token[TYPE] + str(b)] = (int(token[ROW]), int(token[COLUMN])) - b += 1 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 --> -""" + 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]) diff --git a/search/movement_logic.py b/search/movement_logic.py index 0fe2dfccf6656aa5e6c6489e6f0d07fa0e70bfe6..267eb46a15f128cf330f2345c3d76d07e93b2615 100644 --- a/search/movement_logic.py +++ b/search/movement_logic.py @@ -1,10 +1,11 @@ - """ +:argument coordinate - a tuple contain 2 number (r, q) +:return coordinate of the token after the action is complete row = r column = q -Slide action logic: - return the new coordinate after the action complete +Slide action logic: + token only able to slide to the tile that are numbered {1 -> 6} # .-'-._.-'-. # | 3 | 4 | @@ -18,7 +19,8 @@ Slide action logic: row = 1 column = 2 -#1 + +# 1 def slide_left(coordinate): new_pos = () if coordinate: @@ -28,7 +30,8 @@ def slide_left(coordinate): print("Empty coordinate") return new_pos -#2 + +# 2 def slide_right(coordinate): new_pos = () if coordinate: @@ -38,7 +41,8 @@ def slide_right(coordinate): print("Empty coordinate") return new_pos -#3 + +# 3 def slide_up_left(coordinate): new_pos = () if coordinate: @@ -48,7 +52,8 @@ def slide_up_left(coordinate): print("Empty coordinate") return new_pos -#4 + +# 4 def slide_up_right(coordinate): new_pos = () if coordinate: @@ -58,7 +63,8 @@ def slide_up_right(coordinate): print("Empty coordinate") return new_pos -#5 + +# 5 def slide_down_left(coordinate): new_pos = () if coordinate: @@ -68,9 +74,10 @@ def slide_down_left(coordinate): print("Empty coordinate") return new_pos -#6 + +# 6 def slide_down_right(coordinate): - new_pos =() + new_pos = () if coordinate: new_pos.append(coordinate[row] - 1) new_pos.append(coordinate[column] + 1) @@ -78,37 +85,48 @@ def slide_down_right(coordinate): print("Empty coordinate") return new_pos + #################################################################################################### +LEFT = 1 +RIGHT = 2 +UP_LEFT = 3 +UP_RIGHT = 4 +DOWN_LEFT = 5 +DOWN_RIGHT = 6 """ +: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 + Swing action logic: x is the another token that current token swing from -> token can only swing to adjcent tiles{1,2,3} to [x] that are not adjcent to itself tile 1, 2 and 3 are those token can swing to. - - return the tile after the action is complete - # RIGHT .-'-. UP_RIGHT .-'-._.-'-. - # | 1 | | 1 | 2 | - # .-'-._.-'-._.-'-. '-._.-'-._.-'-. - # |token| [x] | 2 | | [x] | 3 | - # '-._.-'-._.-'-._.-' .-'-._.-'-._.-' - # | 3 | |token| - # '-._.-' '-._.-' - # - #DOWN_RIGHT .-'-. UP_LEFT .-'-._.-'-. - # |token| | 2 | 3 | - # '-._.-'-._.-'-. .-'-._.-'-._.-' - # | [x] | 1 | | 1 | [x] | - # .-'-._.-'-._.-' '-._.-'-._.-'-. - # | 3 | 2 | |token| - # '-._.-'-._.-' '-._.-' - # - #DOWN_LEFT .-'-. LEFT .-'-. - # |token| | 3 | - # .-'-._.-'-._.-' .-'-._.-'-._.-'-. - # | 3 | [x] | | 2 | [x] |token| - # '-._.-'-._.-'-. '-._.-'-._.-'-._.-' - # | 2 | 1 | | 1 | - # '-._.-'-._.-' '-._.-' + + + RIGHT .-'-. UP_RIGHT .-'-._.-'-. + | 1 | | 1 | 2 | + .-'-._.-'-._.-'-. '-._.-'-._.-'-. + |token| [x] | 2 | | [x] | 3 | + '-._.-'-._.-'-._.-' .-'-._.-'-._.-' + | 3 | |token| + '-._.-' '-._.-' + + OWN_RIGHT .-'-. UP_LEFT .-'-._.-'-. + |token| | 2 | 3 | + '-._.-'-._.-'-. .-'-._.-'-._.-' + | [x] | 1 | | 1 | [x] | + .-'-._.-'-._.-' '-._.-'-._.-'-. + | 3 | 2 | |token| + '-._.-'-._.-' '-._.-' + + DOWN_LEFT .-'-. LEFT .-'-. + |token| | 3 | + .-'-._.-'-._.-' .-'-._.-'-._.-'-. + | 3 | [x] | | 2 | [x] |token| + '-._.-'-._.-'-. '-._.-'-._.-'-._.-' + | 2 | 1 | | 1 | + '-._.-'-._.-' '-._.-' """ def swing_to_tile_1(token, x): @@ -132,6 +150,7 @@ def swing_to_tile_1(token, x): if position == DOWN_RIGHT: return slide_right(slide_down_right(token)) + def swing_to_tile_2(token, x): position = get_relative_position(token, x) @@ -153,6 +172,7 @@ def swing_to_tile_2(token, x): if position == DOWN_RIGHT: return slide_down_right(slide_down_right(token)) + def swing_to_tile_3(token, x): position = get_relative_position(token, x) @@ -174,32 +194,24 @@ def swing_to_tile_3(token, x): if position == DOWN_RIGHT: return slide_down_left(slide_down_right(token)) -##################################################################################################################### -""" -Calculate relative position of a token to x - -> using the slide direction as the position defition. - e.g left,right, up_left, up_right,..etc -""" -LEFT = 1 -RIGHT = 2 -UP_LEFT = 3 -UP_RIGHT = 4 -DOWN_LEFT = 5 -DOWN_RIGHT = 6 -""" -return the number represent the location of x relative to the token -""" def get_relative_position(token, x): - #check if x is to the left of tile - if compare_tile(slide_left(token),x): + """ + :argument token - the selected game object that is presented by its position on board { (row, column)} + x - the adjacent game object + :return the position of x relative to token + -> using the slide direction as the position defition. + e.g left,right, up_left, up_right,..etc + """ + # check if x is to the left of tile + if compare_tile(slide_left(token), x): return LEFT - #check if x is to the right of tile + # check if x is to the right of tile if compare_tile(slide_right(token), x): return RIGHT - #check if x is to the up_left of the tile + # check if x is to the up_left of the tile if compare_tile(slide_up_left(token), x): return UP_LEFT @@ -215,9 +227,12 @@ def get_relative_position(token, x): if compare_tile(slide_down_right(token), x): return DOWN_RIGHT -""" -return true if both of the row and column are matches between 2 tiles -""" + def compare_tile(tile1, tile2): + """ + :argument tile1 - compose of (row, column) + tile2 - 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]