Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ADD: piece moves tests #21

Merged
merged 2 commits into from
Oct 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 49 additions & 42 deletions python/engine/material.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,29 @@ def piece_move_authorized(self, start, end):
bool
Whether the movement is authorized by the piece possibilities or not.
"""
if start.get_x() == end.get_x() and start.get_y() == end.get_y():
x_start = start.get_x()
y_start = start.get_y()
x_end = end.get_x()
y_end = end.get_y()

# If material is already on the landing cell
if end.get_piece() is not None:
if end.get_piece().is_white() == self.is_white():
return False

if x_start == x_end and y_start == y_end:
return False
else:
return True
if x_start < 0 or x_end < 0:
return False
elif x_start > 7 or x_end > 7:
return False
elif y_start < 0 or y_end < 0:
return False
elif y_start > 7 or y_end > 7:
return False
else:
return True

def can_move(self, board, move):
"""Method to verify if is a move is authorized in terms of movements.
Expand Down Expand Up @@ -237,6 +256,8 @@ def piece_move_authorized(self, start, end):
Whether the movement is authorized by the piece possibilities or not.
"""
# Check if there is a piece on the landing cell
if not super().piece_move_authorized(start=start, end=end):
return False
if end.get_piece() is not None:
# check if there is not another piece of same color
if end.get_piece().is_white() == self.is_white():
Expand Down Expand Up @@ -482,20 +503,16 @@ def piece_move_authorized(self, start, end):
bool
Whether the movement is authorized by the piece possibilities or not.
"""
if start.get_x() == end.get_x() and start.get_y() == end.get_y():
if not super().piece_move_authorized(start=start, end=end):
return False

# Checking movemement
dx = end.get_x() - start.get_x()
dy = end.get_y() - start.get_y()
if abs(dx) == abs(dy):
return True
else:
# If material is already on the landing cell
if end.get_piece() is not None:
if end.get_piece().is_white() == self.is_white():
return False
# Checking movemement
dx = end.get_x() - start.get_x()
dy = end.get_y() - start.get_y()
if abs(dx) == abs(dy):
return True
else:
return False
return False

def can_move(self, board, move):
"""Method to verify if a move is authorized within a board.
Expand Down Expand Up @@ -656,21 +673,16 @@ def piece_move_authorized(self, start, end):
bool
Whether the movement is authorized by the piece possibilities or not.
"""
if start.get_x() == end.get_x() and start.get_y() == end.get_y():
if not super().piece_move_authorized(start=start, end=end):
return False
else:
# Checking if material is already on the landing cell
if end.get_piece() is not None:
if end.get_piece().is_white() == self.is_white():
return False

# Checking movement
dx = end.get_x() - start.get_x()
dy = end.get_y() - start.get_y()
if dx == 0 or dy == 0:
return True
else:
return False
# Checking movement
dx = end.get_x() - start.get_x()
dy = end.get_y() - start.get_y()
if dx == 0 or dy == 0:
return True
else:
return False

def can_move(self, board, move):
"""Method to verify if a move is authorized within a board.
Expand Down Expand Up @@ -826,9 +838,8 @@ def piece_move_authorized(self, start, end):
bool
Whether the movement is authorized by the piece possibilities or not.
"""
if end.get_piece() is not None:
if end.get_piece().is_white() == self.is_white():
return False
if not super().piece_move_authorized(start=start, end=end):
return False

dx = start.get_x() - end.get_x()
dy = start.get_y() - end.get_y()
Expand Down Expand Up @@ -959,16 +970,14 @@ def piece_move_authorized(self, start, end):
bool
Whether the movement is authorized by the piece possibilities or not.
"""
if start.get_x() == end.get_x() and start.get_y() == end.get_y():

if not super().piece_move_authorized(start=start, end=end):
return False
else:
if end.get_piece() is not None:
if end.get_piece().is_white() == self.is_white():
return False
dx = end.get_x() - start.get_x()
dy = end.get_y() - start.get_y()

dx = end.get_x() - start.get_x()
dy = end.get_y() - start.get_y()

return (dx == 0) or (dy == 0) or (abs(dx) == abs(dy))
return (dx == 0) or (dy == 0) or (abs(dx) == abs(dy))

def can_move(self, board, move):
"""Method to verify if a move is authorized within a board.
Expand Down Expand Up @@ -1188,11 +1197,9 @@ def piece_move_authorized(self, start, end):
bool
Whether the movement is authorized by the piece possibilities or not.
"""
if start.get_x() == end.get_x() and start.get_y() == end.get_y():

if not super().piece_move_authorized(start=start, end=end):
return False
if end.get_piece() is not None:
if end.get_piece().is_white() == self.is_white():
return False
dx = end.get_x() - start.get_x()
dy = end.get_y() - start.get_y()

Expand Down
4 changes: 2 additions & 2 deletions tests/unit_test/engine_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ def test_en_passant():

def test_blocked_by_mat():
"""Tests that if the king is checked cannot move unless it unchecks the king."""
game = engine.Game(automatic_draw=True)
game = engine.Game(automatic_draw=False)
game.move_from_coordinates(game.player1, 1, 4, 3, 4)
game.move_from_coordinates(game.player2, 6, 5, 4, 5)
game.move_from_coordinates(game.player1, 0, 3, 4, 7)
Expand All @@ -132,7 +132,7 @@ def test_blocked_by_mat():

def test_end_game():
"""Tests what happens when check & mat happens."""
game = engine.Game(automatic_draw=True)
game = engine.Game(automatic_draw=False)
game.move_from_coordinates(game.player1, 1, 4, 3, 4)
game.move_from_coordinates(game.player2, 6, 5, 4, 5)
game.move_from_coordinates(game.player1, 0, 3, 4, 7)
Expand Down
Loading
Loading