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

temporary version of getting the position list {i will clean up the code }

parent 0d28c5d0
Branches master
No related tags found
No related merge requests found
File added
...@@ -8,20 +8,29 @@ Read from JSON file: ...@@ -8,20 +8,29 @@ Read from JSON file:
-> ensure format token to distinguish between upper and lower. -> ensure format token to distinguish between upper and lower.
-> handling file IO exception -> handling file IO exception
""" """
def to_board(file): def to_position_list(file):
TYPE = 0 TYPE = 0
X_COORD = 1 ROW = 1
Y_COORD = 2 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): if not isinstance(file, str):
print("Pass invalid argument to to_board()~!!!") print("Pass invalid argument to to_board()~!!!")
exit(-1) exit(-1)
try: try:
# open the file handle by json module # open the file handle by json module
FILE = open(file) initial = open(file)
game = json.load(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. # iterate through the entity who is not empty in order to add it type and coordinate to dictionary.
for entity in game.keys(): for entity in game.keys():
...@@ -29,18 +38,37 @@ def to_board(file): ...@@ -29,18 +38,37 @@ def to_board(file):
for token in game[entity]: for token in game[entity]:
if entity == 'upper': 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: else:
board[(int(token[X_COORD]), int(token[Y_COORD]))] = token[TYPE] 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:
dict[token[TYPE] + str(b)] = (int(token[ROW]), int(token[COLUMN]))
b += 1
FILE.close() initial.close()
return board return dict
except IOError: except IOError:
print("There is an issue when try to open file") print("There is an issue when try to open file")
exit(-1) 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/ taken from https://www.redblobgames.com/grids/hexagons/
under section: double coordinates for doublewidth under section: double coordinates for doublewidth
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment