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

attempt clean up my code, add arguement and return to comment section

parent 7f95db91
No related branches found
No related tags found
No related merge requests found
......@@ -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
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 :
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 = {
"s1" = (row, column),
"c#" = (row, column),
...
}
-> for the key: 'c#'
-> 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.
"""
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
: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"))
def distance_between(Upper_token, Lower_token):
"""
taken from https://www.redblobgames.com/grids/hexagons/
: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
->
"""
def distance_between(Upper_token, Lower_token):
dx = abs(Upper_token[0] - Lower_token[0])
dy = abs(Upper_token[1] - Lower_token[1])
......
"""
: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,6 +19,7 @@ Slide action logic:
row = 1
column = 2
# 1
def slide_left(coordinate):
new_pos = ()
......@@ -28,6 +30,7 @@ def slide_left(coordinate):
print("Empty coordinate")
return new_pos
# 2
def slide_right(coordinate):
new_pos = ()
......@@ -38,6 +41,7 @@ def slide_right(coordinate):
print("Empty coordinate")
return new_pos
# 3
def slide_up_left(coordinate):
new_pos = ()
......@@ -48,6 +52,7 @@ def slide_up_left(coordinate):
print("Empty coordinate")
return new_pos
# 4
def slide_up_right(coordinate):
new_pos = ()
......@@ -58,6 +63,7 @@ def slide_up_right(coordinate):
print("Empty coordinate")
return new_pos
# 5
def slide_down_left(coordinate):
new_pos = ()
......@@ -68,6 +74,7 @@ def slide_down_left(coordinate):
print("Empty coordinate")
return new_pos
# 6
def slide_down_right(coordinate):
new_pos = ()
......@@ -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,23 +194,15 @@ def swing_to_tile_3(token, x):
if position == DOWN_RIGHT:
return slide_down_left(slide_down_right(token))
#####################################################################################################################
def get_relative_position(token, x):
"""
Calculate relative position of a token to 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
"""
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
......@@ -215,9 +227,12 @@ def get_relative_position(token, x):
if compare_tile(slide_down_right(token), x):
return DOWN_RIGHT
def compare_tile(tile1, tile2):
"""
return true if both of the row and column are matches between 2 tiles
: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
"""
def compare_tile(tile1, tile2):
return tile1[row] == tile2[row] \
and tile1[column] == tile2[column]
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment