From 7130450f8494652f5fd3ae3d687753647d883b93 Mon Sep 17 00:00:00 2001 From: Xuan Trinh <xuan.trinh@student.unimelb.edu.au> Date: Mon, 22 Mar 2021 18:55:35 +1100 Subject: [PATCH] modulise the function that read piece into dictionary and block to set. Also delete the dedundance function -XUAN TRINH 7PM 22 mar 2021 --- search/__pycache__/main.cpython-38.pyc | Bin 0 -> 1976 bytes search/__pycache__/method.cpython-38.pyc | Bin 2696 -> 0 bytes search/main.py | 43 +++++---- search/method.py | 107 ----------------------- search/movement_logic.py | 18 ++++ search/search_algorithm.py | 3 +- 6 files changed, 39 insertions(+), 132 deletions(-) create mode 100644 search/__pycache__/main.cpython-38.pyc delete mode 100644 search/__pycache__/method.cpython-38.pyc delete mode 100644 search/method.py diff --git a/search/__pycache__/main.cpython-38.pyc b/search/__pycache__/main.cpython-38.pyc new file mode 100644 index 0000000000000000000000000000000000000000..84aa148a1abe1e015af49a4114970573a6d5286e GIT binary patch literal 1976 zcmWIL<>g{vU|^Ww6OnM9kAdMah=Yuo85kHG7#J9e;}{qiQW&BbQW#U1au}l+!8B78 zQwn1Wa}IMZOB72kYZPlPTNE23LncEMdn!jWLlkEUOA2cXLljpETMBy%Llk!^PYTBz z#uUyJt`?Rk-W2W>o)q3WOi_F(Ou-DAd{w8poc(<Rj13HoOcWf8N;1<jlQR=@6g=}v zQgd=L(^K=3Q*{)AQ*%>`OHzvz40RNY42%r90*dmpQj<#*0uqZ#6dbJ-f>RTVk~1>% z(z&=oGBS%5ij#{n3rZA{^Ycm)GxLfSN-|OvQu9iRDisRyGxJImO7g)%1x5MkMTxlz z8X)=P{FGFM%sj4y`1st!%)Iz`y@JXFh2+GXoMMFpkcft6f~KBAWPWLpLUDdhX-Q^& zo<ea+Vo^!4LPly)sv_4*P%!#wGT-7ZD9X$$iBHN;EJ}fKigPkkQsJEP%)Inuka8Gi zVqjq4WME)$28DbU0|P^qMhQa+V-`~iqa;H!W4}NxLkaT&mKuhIjJ1s645kc$3_%PL z4B-rU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}2}j5CMwHA}J6{o`Hcu zld(t~L@`CNfx|QrN+&^r6qL=u;d+Z7EFGVmpOceWoSB~&3JPNtP=J6!n30E(hmnPm zhmj2ni-oaB6>JCCDKG#1|Nmc;>E-|b|Nn#2l_pb>EXX+^*J<+KVvmnc$xn`tFX8|R zg7SY9H>42JD=o>)DFTIk5vYPJVgV@tr#$B3)RH145EoQ{fMf3#b7o!%*xo4a;>6s7 zoYZ(wX@=x2gx@%9a`RJ4b5iX<>Ax6MI<atYFmkYpu&^+)F>wfU=<vxgF*5xF0K>S~ AdH?_b literal 0 HcmV?d00001 diff --git a/search/__pycache__/method.cpython-38.pyc b/search/__pycache__/method.cpython-38.pyc deleted file mode 100644 index 0400de112aa4ed4e6f0ff7a2edbb169348170aad..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 2696 zcmWIL<>g{vU|?8uIV?e!kAdMah=Yuo7#J8F7#J9e4Hy_0QW#Pga~Pr+QkbF`Q<$Te zQW&C`Q&^%{Qdpx{Q`l12TNt9)QaDmLTNt9)Qy7C8G`U`a%<{X%SagfA;3Xpi1H(&@ zfyp2iNCt#?7#J8h7#J9wLAF>hF)*Ys)-t9r)-t6q)-tCs*0Q89*0QEB*0Pl_)G(AV zHZv6)l`y3+H8V0YlrX0-gXk)`5|$c<X2x3f64nK5HS7x+YdLDz7qBm6U}Okq$Wviq zDB;NFDk@=Q0KpQb6c&&!nC?i1Jf;YSOom#{63%9Z8qO3}8-^N=1zZamTo@)W#xAMl zs^O^Nl4L0AfzV*lo?7l2t`xRxrU{Hi2{qg$+>#6>JlRY|?@D-UxJ&pX8JZcvEPfCx zjj4tcqGo}>LWUB?8paf6afW6_aRwU(h<J%0*d#r$9zwdA5W4dWh|<A~&{4RkhAV|V zn`r`55g*iU9<bd@@S9Y^Si^!a1*#L`>IVdMvLbZm=@3-ShEQF&2oxWZ3=4#?>srXj z$WXYbu&0JIm_d^xv5}d9flEO_LBT4qD7`c{HLpY=Ei)%oL02I^O`#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b7^7&#c3m^c`@7<m}^m^c{um{=IOSRpKsm;n<XqY<+a z6Bi>NqYe`vqaL#!vl?@eG6MrcGRRXPBS9DxU#zhD+Kz#N0aP!iFiJAiFoH-4P(929 z7Gna7F-bCj#h7cDN*GfZYnW@8Bp6bdr5T!;#2JDaG+7dL7%@t1a6XF%2atlU0w^r< zi&H_V8>M&zCpSb<56(_RYJ!&hpyUh9Wcg_d#U-f)ko*8nU+Trs3|5k_08VKz0Z3j6 zNd(mk;Ovu8Qc_TCrLSLJUanV^nv#^0p9G5VV!h=2T>bQ-%#>pNjMR$6^!&VHeXyfT z^HNfa6pB;9m5!A{N`7flPO3t3etuC(W?o`RYB8u5feM#rra*I+t{qr2ID>)nO%W&} zG?{KOCngo&V$Myh&}50?Mhd$qZlo}YVoIrqVoIq5r#A?p1WKm@DVdP06Q7h?Ql6Tc z7X?Z}Y@oR25@8f!DpF%$V9?|!;$vW7C=vw`k|06^M2Le32@nBFCt#HjlR;@3!UEgD aVUwGmQks)$2PzwiK_+o9!xJYnhd%(Di=*`b diff --git a/search/main.py b/search/main.py index bee7a5c..3f9282a 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 14050d0..0000000 --- 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 fbfd26f..0943875 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 ffcad3c..3e54462 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): -- GitLab