From d2b2ee8efb963c11a5f06be6c67db39ea868d321 Mon Sep 17 00:00:00 2001 From: Xuan Trinh <xuan.trinh@student.unimelb.edu.au> Date: Sat, 20 Mar 2021 20:36:25 +1100 Subject: [PATCH] add the action logic {for both swing and slide}, function to compare 2 tile --- .idea/Project1.iml | 5 +- .idea/inspectionProfiles/Project_Default.xml | 13 ++ .idea/misc.xml | 4 +- search/method.py | 38 +--- search/movement_logic.py | 223 +++++++++++++++++++ 5 files changed, 240 insertions(+), 43 deletions(-) create mode 100644 .idea/inspectionProfiles/Project_Default.xml create mode 100644 search/movement_logic.py diff --git a/.idea/Project1.iml b/.idea/Project1.iml index 2968bc2..a736684 100644 --- a/.idea/Project1.iml +++ b/.idea/Project1.iml @@ -5,10 +5,9 @@ <configuration sdkName="Python 3.6" /> </facet> </component> - <component name="NewModuleRootManager" inherit-compiler-output="true"> - <exclude-output /> + <component name="NewModuleRootManager"> <content url="file://$MODULE_DIR$" /> - <orderEntry type="jdk" jdkName="Python 3.6" jdkType="Python SDK" /> + <orderEntry type="jdk" jdkName="Python 3.8" jdkType="Python SDK" /> <orderEntry type="sourceFolder" forTests="false" /> <orderEntry type="library" name="Python 3.6 interpreter library" level="application" /> </component> diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml new file mode 100644 index 0000000..db25fb7 --- /dev/null +++ b/.idea/inspectionProfiles/Project_Default.xml @@ -0,0 +1,13 @@ +<component name="InspectionProjectProfileManager"> + <profile version="1.0"> + <option name="myName" value="Project Default" /> + <inspection_tool class="PyPep8NamingInspection" enabled="true" level="WEAK WARNING" enabled_by_default="true"> + <option name="ignoredErrors"> + <list> + <option value="N806" /> + <option value="N803" /> + </list> + </option> + </inspection_tool> + </profile> +</component> \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml index 1763e15..d1e22ec 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,6 +1,4 @@ <?xml version="1.0" encoding="UTF-8"?> <project version="4"> - <component name="ProjectRootManager" version="2" languageLevel="JDK_11" project-jdk-name="11" project-jdk-type="JavaSDK"> - <output url="file://$PROJECT_DIR$/out" /> - </component> + <component name="ProjectRootManager" version="2" project-jdk-name="Python 3.8" project-jdk-type="Python SDK" /> </project> \ No newline at end of file diff --git a/search/method.py b/search/method.py index fdf65b3..4b0a6c8 100644 --- a/search/method.py +++ b/search/method.py @@ -1,5 +1,5 @@ -from src.search.util import print_board +from search.util import print_board import json """ @@ -53,41 +53,5 @@ def distance_between(Upper_token, Lower_token): return dx + max(0, (dy - dx) / 2) -""" -movement logic -""" -row = 1 -column = 2 - -def move_left(coordinate): - if coordinate: - coordinate[column] -= 1 - -def move_right(coordinate): - if coordinate: - coordinate[column] += 1 - -def move_up_left(coordinate): - if coordinate: - coordinate[column] -= 1 - coordinate[row] += 1 - -def move_up_right(coordinate): - if coordinate: - coordinate[row] += 1 - - -def move_down_left(coordinate): - if coordinate: - coordinate[row] -= 1 - -def move_down_right(coordinate): - if coordinate: - coordinate[row] -= 1 - coordinate[column] += 1 - -def transfer_move_to_board(, coordinate, new_coordinate): - if not board[new_coordinate] -print(distance_between([-3, 0], [1, 0])) diff --git a/search/movement_logic.py b/search/movement_logic.py new file mode 100644 index 0000000..eaec4a7 --- /dev/null +++ b/search/movement_logic.py @@ -0,0 +1,223 @@ + +""" +row = r +column = q +Slide action logic: + return the new coordinate after the action complete + + + # .-'-._.-'-. + # | 3 | 4 | + # .-'-._.-'-._.-'-. + # | 1 |token| 2 | + # '-._.-'-._.-'-._.-' + # | 5 | 6 | + # '-._.-'-._.-' + # +""" +row = 1 +column = 2 + +#1 +def slide_left(coordinate): + new_pos = () + if coordinate: + new_pos.append(coordinate[row]) + new_pos.append(coordinate[column] - 1) + else: + print("Empty coordinate") + return new_pos + +#2 +def slide_right(coordinate): + new_pos = () + if coordinate: + new_pos.append(coordinate[row]) + new_pos.append(coordinate[column] + 1) + else: + print("Empty coordinate") + return new_pos + +#3 +def slide_up_left(coordinate): + new_pos = () + if coordinate: + new_pos.append(coordinate[row] + 1) + new_pos.append(coordinate[column] - 1) + else: + print("Empty coordinate") + return new_pos + +#4 +def slide_up_right(coordinate): + new_pos = () + if coordinate: + new_pos.append(coordinate[row] + 1) + new_pos.append(coordinate[column]) + else: + print("Empty coordinate") + return new_pos + +#5 +def slide_down_left(coordinate): + new_pos = () + if coordinate: + new_pos.append(coordinate[row] - 1) + new_pos.append(coordinate[column]) + else: + print("Empty coordinate") + return new_pos + +#6 +def slide_down_right(coordinate): + new_pos =() + if coordinate: + new_pos.append(coordinate[row] - 1) + new_pos.append(coordinate[column] + 1) + else: + print("Empty coordinate") + return new_pos + +#################################################################################################### +""" +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 | + # '-._.-'-._.-' '-._.-' +""" + +def swing_to_tile_1(token, x): + position = get_relative_position(token, x) + + if position == LEFT: + return slide_down_left(slide_left(token)) + + if position == RIGHT: + return slide_up_right(slide_right(token)) + + if position == UP_RIGHT: + return slide_up_left(slide_up_right(token)) + + if position == UP_LEFT: + return slide_left(slide_up_left(token)) + + if position == DOWN_LEFT: + return slide_down_right(slide_down_left(token)) + + if position == DOWN_RIGHT: + return slide_right(slide_down_right(token)) + +def swing_to_tile_2(token, x): + position = get_relative_position(token, x) + + if position == LEFT: + return slide_left(slide_left(token)) + + if position == RIGHT: + return slide_right(slide_right(token)) + + if position == UP_RIGHT: + return slide_up_right(slide_up_right(token)) + + if position == UP_LEFT: + return slide_up_left(slide_up_left(token)) + + if position == DOWN_LEFT: + return slide_down_left(slide_down_left(token)) + + if position == DOWN_RIGHT: + return slide_down_right(slide_down_right(token)) + +def swing_to_tile_2(token, x): + position = get_relative_position(token, x) + + if position == LEFT: + return slide_up_left(slide_left(token)) + + if position == RIGHT: + return slide_down_right(slide_right(token)) + + if position == UP_RIGHT: + return slide_right(slide_up_right(token)) + + if position == UP_LEFT: + return slide_up_right(slide_up_left(token)) + + if position == DOWN_LEFT: + return slide_left(slide_down_left(token)) + + 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): + return LEFT + + #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 + if compare_tile(slide_up_left(token), x): + return UP_LEFT + + # check if x is to the up_right of the tile + if compare_tile(slide_up_right(token), x): + return UP_RIGHT + + # check if x is to the down_left of the tile + if compare_tile(slide_down_left(token), x): + return DOWN_LEFT + + # check if x is to the down_right of the tile + 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): + return tile1[row] == tile2[row] \ + and tile1[column] == tile2[column] -- GitLab