Skip to content
Snippets Groups Projects
Commit 7130450f authored by Xuan Trinh's avatar Xuan Trinh
Browse files

modulise the function that read piece into dictionary and block to set. Also...

modulise the function that read piece into dictionary and block to set. Also delete the dedundance function -XUAN TRINH 7PM 22 mar 2021
parent d369f8c4
Branches
No related tags found
No related merge requests found
File added
File deleted
......@@ -26,24 +26,28 @@ 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
keywrite = ""
for piece in initialpiecesupper:
......@@ -72,13 +76,6 @@ def main():
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).
# Our upper pieces are R, P and S, lower pieces are r, p and s
# We will convert both to lower case letters and check each case
......
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)
......@@ -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)
......@@ -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):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment