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

In movement logic: add the constraint to ensure new move need to be in the...

In movement logic: add the constraint to ensure new move need to be in the board. handling details are added to the method comment - XUAN TRINH2021 3 21
parent 60858f5f
Branches
No related tags found
No related merge requests found
File added
File added
""" """
:argument coordinate - a tuple contain 2 number (r, q) :argument coordinate - a tuple contain 2 number (r, q)
:return coordinate of the token after the action is complete :return coordinate of the token after the action is complete
if the new position is out of the board:
return the token'
s position instead.
row = r row = r
column = q column = q
...@@ -16,16 +19,17 @@ Slide action logic: ...@@ -16,16 +19,17 @@ Slide action logic:
# '-._.-'-._.-' # '-._.-'-._.-'
# #
""" """
row = 1 row = 0
column = 2 column = 1
# 1 # 1
def slide_left(coordinate): def slide_left(coordinate):
new_pos = ()
if coordinate: if coordinate:
new_pos.append(coordinate[row]) new_pos = (coordinate[row], coordinate[column] - 1)
new_pos.append(coordinate[column] - 1)
if not check_within_board(new_pos):
return coordinate
else: else:
print("Empty coordinate") print("Empty coordinate")
return new_pos return new_pos
...@@ -33,10 +37,12 @@ def slide_left(coordinate): ...@@ -33,10 +37,12 @@ def slide_left(coordinate):
# 2 # 2
def slide_right(coordinate): def slide_right(coordinate):
new_pos = ()
if coordinate: if coordinate:
new_pos.append(coordinate[row]) new_pos = (coordinate[row], coordinate[column] + 1)
new_pos.append(coordinate[column] + 1)
if not check_within_board(new_pos):
return coordinate
else: else:
print("Empty coordinate") print("Empty coordinate")
return new_pos return new_pos
...@@ -46,8 +52,11 @@ def slide_right(coordinate): ...@@ -46,8 +52,11 @@ def slide_right(coordinate):
def slide_up_left(coordinate): def slide_up_left(coordinate):
new_pos = () new_pos = ()
if coordinate: if coordinate:
new_pos.append(coordinate[row] + 1) new_pos = (coordinate[row] + 1, coordinate[column] - 1)
new_pos.append(coordinate[column] - 1)
if not check_within_board(new_pos):
return coordinate
else: else:
print("Empty coordinate") print("Empty coordinate")
return new_pos return new_pos
...@@ -55,10 +64,12 @@ def slide_up_left(coordinate): ...@@ -55,10 +64,12 @@ def slide_up_left(coordinate):
# 4 # 4
def slide_up_right(coordinate): def slide_up_right(coordinate):
new_pos = ()
if coordinate: if coordinate:
new_pos.append(coordinate[row] + 1) new_pos = (coordinate[row] + 1, coordinate[column])
new_pos.append(coordinate[column])
if not check_within_board(new_pos):
return coordinate
else: else:
print("Empty coordinate") print("Empty coordinate")
return new_pos return new_pos
...@@ -66,10 +77,12 @@ def slide_up_right(coordinate): ...@@ -66,10 +77,12 @@ def slide_up_right(coordinate):
# 5 # 5
def slide_down_left(coordinate): def slide_down_left(coordinate):
new_pos = ()
if coordinate: if coordinate:
new_pos.append(coordinate[row] - 1) new_pos = (coordinate[row] - 1, coordinate[column])
new_pos.append(coordinate[column])
if not check_within_board(new_pos):
return coordinate
else: else:
print("Empty coordinate") print("Empty coordinate")
return new_pos return new_pos
...@@ -77,10 +90,12 @@ def slide_down_left(coordinate): ...@@ -77,10 +90,12 @@ def slide_down_left(coordinate):
# 6 # 6
def slide_down_right(coordinate): def slide_down_right(coordinate):
new_pos = ()
if coordinate: if coordinate:
new_pos.append(coordinate[row] - 1) new_pos = (coordinate[row] - 1, coordinate[column] + 1)
new_pos.append(coordinate[column] + 1)
if not check_within_board(new_pos):
return coordinate
else: else:
print("Empty coordinate") print("Empty coordinate")
return new_pos return new_pos
...@@ -93,10 +108,12 @@ UP_LEFT = 3 ...@@ -93,10 +108,12 @@ UP_LEFT = 3
UP_RIGHT = 4 UP_RIGHT = 4
DOWN_LEFT = 5 DOWN_LEFT = 5
DOWN_RIGHT = 6 DOWN_RIGHT = 6
OUT_OF_BOARD = 0
""" """
:argument token - the selected game object that is presented by its position on board { (row, column)} :argument token - the selected game object that is presented by its position on board { (row, column)}
x - the adjacent game object x - the adjacent game object
:return new position after the action is complete :return new position after the action is complete
if the new position is out of the board return the token's position instead
Swing action logic: Swing action logic:
x is the another token that current token swing from x is the another token that current token swing from
...@@ -132,67 +149,88 @@ Swing action logic: ...@@ -132,67 +149,88 @@ Swing action logic:
def swing_to_tile_1(token, x): def swing_to_tile_1(token, x):
position = get_relative_position(token, x) position = get_relative_position(token, x)
if position == LEFT: if position == LEFT:
return slide_down_left(slide_left(token)) new_postion = slide_down_left(slide_left(token))
if position == RIGHT: if position == RIGHT:
return slide_up_right(slide_right(token)) new_postion = slide_up_right(slide_right(token))
if position == UP_RIGHT: if position == UP_RIGHT:
return slide_up_left(slide_up_right(token)) new_postion = slide_up_left(slide_up_right(token))
if position == UP_LEFT: if position == UP_LEFT:
return slide_left(slide_up_left(token)) new_postion = slide_left(slide_up_left(token))
if position == DOWN_LEFT: if position == DOWN_LEFT:
return slide_down_right(slide_down_left(token)) new_postion = slide_down_right(slide_down_left(token))
if position == DOWN_RIGHT: if position == DOWN_RIGHT:
return slide_right(slide_down_right(token)) new_postion = slide_right(slide_down_right(token))
#if the position of the token after the action complete is out of board, return the token position in
#stead
if compare_tile(new_postion, x):
return token
return new_postion
def swing_to_tile_2(token, x): def swing_to_tile_2(token, x):
position = get_relative_position(token, x) position = get_relative_position(token, x)
if position == LEFT: if position == LEFT:
return slide_left(slide_left(token)) new_postion = slide_left(slide_left(token))
if position == RIGHT: if position == RIGHT:
return slide_right(slide_right(token)) new_postion = slide_right(slide_right(token))
if position == UP_RIGHT: if position == UP_RIGHT:
return slide_up_right(slide_up_right(token)) new_postion = slide_up_right(slide_up_right(token))
if position == UP_LEFT: if position == UP_LEFT:
return slide_up_left(slide_up_left(token)) new_postion = slide_up_left(slide_up_left(token))
if position == DOWN_LEFT: if position == DOWN_LEFT:
return slide_down_left(slide_down_left(token)) new_postion = slide_down_left(slide_down_left(token))
if position == DOWN_RIGHT: if position == DOWN_RIGHT:
return slide_down_right(slide_down_right(token)) new_postion = slide_down_right(slide_down_right(token))
# if the position of the token after the action complete is out of board, return the token position in
# stead
if compare_tile(new_postion, x):
return token
return new_postion
def swing_to_tile_3(token, x): def swing_to_tile_3(token, x):
position = get_relative_position(token, x) position = get_relative_position(token, x)
if position == LEFT: if position == LEFT:
return slide_up_left(slide_left(token)) new_postion = slide_up_left(slide_left(token))
if position == RIGHT: if position == RIGHT:
return slide_down_right(slide_right(token)) new_postion = slide_down_right(slide_right(token))
if position == UP_RIGHT: if position == UP_RIGHT:
return slide_right(slide_up_right(token)) new_postion = slide_right(slide_up_right(token))
if position == UP_LEFT: if position == UP_LEFT:
return slide_up_right(slide_up_left(token)) new_postion = slide_up_right(slide_up_left(token))
if position == DOWN_LEFT: if position == DOWN_LEFT:
return slide_left(slide_down_left(token)) new_postion = slide_left(slide_down_left(token))
if position == DOWN_RIGHT: if position == DOWN_RIGHT:
return slide_down_left(slide_down_right(token)) new_postion = slide_down_left(slide_down_right(token))
# if the position of the token after the action complete is out of board, return the token position in
# stead
if compare_tile(new_postion, x):
return token
return new_postion
def get_relative_position(token, x): def get_relative_position(token, x):
...@@ -236,3 +274,22 @@ def compare_tile(tile1, tile2): ...@@ -236,3 +274,22 @@ def compare_tile(tile1, tile2):
""" """
return tile1[row] == tile2[row] \ return tile1[row] == tile2[row] \
and tile1[column] == tile2[column] and tile1[column] == tile2[column]
def check_within_board(tile):
"""
ensure that the tile is exist within the board
:param tile: coordinate of the tile
:return: true if the tile exist within the board, else false
"""
if tile[row] < -4 or tile[row] > 4:
return False
if tile[column] < -4 or tile[column] > 4:
return False
if tile[column] in [-4, -3, -2, -1] and tile[row] < -4 - tile[column]:
return False
if tile[column] in [4, 3, 2, 1] and tile[row] > 4 - tile[column]:
return False
return True
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment