From ff03d520085fb615a724b7256677a117192bc69e Mon Sep 17 00:00:00 2001 From: VincentAURIAU Date: Sat, 28 Oct 2023 15:57:55 +0200 Subject: [PATCH 1/2] ENH: pieces share now some elements of move_authorized through parent class --- python/engine/material.py | 91 +++++++++++++++++++++------------------ 1 file changed, 49 insertions(+), 42 deletions(-) diff --git a/python/engine/material.py b/python/engine/material.py index 72463f5..6fe4f10 100644 --- a/python/engine/material.py +++ b/python/engine/material.py @@ -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. @@ -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(): @@ -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. @@ -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. @@ -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() @@ -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. @@ -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() From 7a95a2c4bc2aeea8faff539c02575ca018d363e3 Mon Sep 17 00:00:00 2001 From: VincentAURIAU Date: Sat, 28 Oct 2023 15:58:21 +0200 Subject: [PATCH 2/2] ADD: pieces move test --- tests/unit_test/engine_test.py | 4 +- tests/unit_test/test_material.py | 352 +++++++++++++++++++++++++++++++ 2 files changed, 354 insertions(+), 2 deletions(-) create mode 100644 tests/unit_test/test_material.py diff --git a/tests/unit_test/engine_test.py b/tests/unit_test/engine_test.py index 51840fd..b9ec56d 100644 --- a/tests/unit_test/engine_test.py +++ b/tests/unit_test/engine_test.py @@ -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) @@ -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) diff --git a/tests/unit_test/test_material.py b/tests/unit_test/test_material.py new file mode 100644 index 0000000..395c88e --- /dev/null +++ b/tests/unit_test/test_material.py @@ -0,0 +1,352 @@ +import sys + +sys.path.append("../../python") +sys.path.append("python") + +import engine.engine as engine +import engine.material as material + +# Add verifications about own color of piece on end cell +# + +def test_pawn_moves(): + x_start = 1 + y_start = 0 + pawn = material.Pawn(white=True, x=x_start, y=y_start) + start_cell = engine.Cell(x=x_start, y=y_start, piece=pawn) + + # Assert can go forward + x_end = 2 + y_end = 0 + end_cell = engine.Cell(x=x_end, y=y_end, piece=None) + assert pawn.piece_move_authorized(start_cell, end_cell) + + # Assert can go forward by two cells + x_end = 3 + y_end = 0 + end_cell = engine.Cell(x=x_end, y=y_end, piece=None) + assert pawn.piece_move_authorized(start_cell, end_cell) + + # Assert cann go diagonal if adversary piece + x_end = 2 + y_end = 1 + end_cell = engine.Cell(x=x_end, y=y_end, piece=material.Pawn(False, x_end, y_end)) + assert pawn.piece_move_authorized(start_cell, end_cell) + + # Assert cannot go forward by three cells + x_end = 4 + y_end = 0 + end_cell = engine.Cell(x=x_end, y=y_end, piece=None) + assert not pawn.piece_move_authorized(start_cell, end_cell) + + # Assert cannot go backward + x_end = 0 + y_end = 0 + end_cell = engine.Cell(x=x_end, y=y_end, piece=None) + assert not pawn.piece_move_authorized(start_cell, end_cell) + + # Assert cannot go on the side + x_end = 1 + y_end = 1 + end_cell = engine.Cell(x=x_end, y=y_end, piece=None) + assert not pawn.piece_move_authorized(start_cell, end_cell) + + # Assert cannot go diagonal without piece + x_end = 2 + y_end = 1 + end_cell = engine.Cell(x=x_end, y=y_end, piece=None) + assert not pawn.piece_move_authorized(start_cell, end_cell) + + # Assert cannot further than the board + x_end = 1 + y_end = -1 + end_cell = engine.Cell(x=x_end, y=y_end, piece=material.Pawn(False, x_end, y_end)) + assert not pawn.piece_move_authorized(start_cell, end_cell) + + +def test_bishop_moves(): + x_start = 1 + y_start = 2 + bishop = material.Bishop(white=True, x=x_start, y=y_start) + start_cell = engine.Cell(x=x_start, y=y_start, piece=bishop) + + # Assert can go diagonals + x_end = 0 + y_end = 1 + end_cell = engine.Cell(x=x_end, y=y_end, piece=None) + assert bishop.piece_move_authorized(start_cell, end_cell) + x_end = 0 + y_end = 3 + end_cell = engine.Cell(x=x_end, y=y_end, piece=None) + assert bishop.piece_move_authorized(start_cell, end_cell) + x_end = 3 + y_end = 0 + end_cell = engine.Cell(x=x_end, y=y_end, piece=None) + assert bishop.piece_move_authorized(start_cell, end_cell) + x_end = 6 + y_end = 7 + end_cell = engine.Cell(x=x_end, y=y_end, piece=None) + assert bishop.piece_move_authorized(start_cell, end_cell) + + # Assert cannot go differently than diagonals + x_end = 4 + y_end = 0 + end_cell = engine.Cell(x=x_end, y=y_end, piece=None) + assert not bishop.piece_move_authorized(start_cell, end_cell) + x_end = 1 + y_end = 5 + end_cell = engine.Cell(x=x_end, y=y_end, piece=None) + assert not bishop.piece_move_authorized(start_cell, end_cell) + x_end = 0 + y_end = 2 + end_cell = engine.Cell(x=x_end, y=y_end, piece=None) + assert not bishop.piece_move_authorized(start_cell, end_cell) + x_end = 4 + y_end = 4 + end_cell = engine.Cell(x=x_end, y=y_end, piece=None) + + # Assrt cannot go futher than the board + assert not bishop.piece_move_authorized(start_cell, end_cell) + x_end = -1 + y_end = 0 + end_cell = engine.Cell(x=x_end, y=y_end, piece=None) + assert not bishop.piece_move_authorized(start_cell, end_cell) + x_end = 7 + y_end = 8 + end_cell = engine.Cell(x=x_end, y=y_end, piece=None) + assert not bishop.piece_move_authorized(start_cell, end_cell) + + +def test_knight_moves(): + x_start = 2 + y_start = 2 + knight = material.Knight(white=True, x=x_start, y=y_start) + start_cell = engine.Cell(x=x_start, y=y_start, piece=knight) + + # Assert can go everywhere it is supposed to + x_end = 0 + y_end = 3 + end_cell = engine.Cell(x=x_end, y=y_end, piece=None) + assert knight.piece_move_authorized(start_cell, end_cell) + x_end = 0 + y_end = 1 + end_cell = engine.Cell(x=x_end, y=y_end, piece=None) + assert knight.piece_move_authorized(start_cell, end_cell) + x_end = 1 + y_end = 0 + end_cell = engine.Cell(x=x_end, y=y_end, piece=None) + assert knight.piece_move_authorized(start_cell, end_cell) + x_end = 3 + y_end = 0 + end_cell = engine.Cell(x=x_end, y=y_end, piece=None) + assert knight.piece_move_authorized(start_cell, end_cell) + x_end = 4 + y_end = 1 + end_cell = engine.Cell(x=x_end, y=y_end, piece=None) + assert knight.piece_move_authorized(start_cell, end_cell) + x_end = 4 + y_end = 3 + end_cell = engine.Cell(x=x_end, y=y_end, piece=None) + assert knight.piece_move_authorized(start_cell, end_cell) + x_end = 3 + y_end = 4 + end_cell = engine.Cell(x=x_end, y=y_end, piece=None) + assert knight.piece_move_authorized(start_cell, end_cell) + x_end = 1 + y_end = 4 + end_cell = engine.Cell(x=x_end, y=y_end, piece=None) + assert knight.piece_move_authorized(start_cell, end_cell) + + # Assert cannot go differently + x_end = 4 + y_end = 0 + end_cell = engine.Cell(x=x_end, y=y_end, piece=None) + assert not knight.piece_move_authorized(start_cell, end_cell) + x_end = 2 + y_end = 2 + end_cell = engine.Cell(x=x_end, y=y_end, piece=None) + assert not knight.piece_move_authorized(start_cell, end_cell) + x_end = 2 + y_end = 1 + end_cell = engine.Cell(x=x_end, y=y_end, piece=None) + assert not knight.piece_move_authorized(start_cell, end_cell) + x_end = 1 + y_end = 2 + end_cell = engine.Cell(x=x_end, y=y_end, piece=None) + assert not knight.piece_move_authorized(start_cell, end_cell) + x_end = 4 + y_end = 7 + end_cell = engine.Cell(x=x_end, y=y_end, piece=None) + assert not knight.piece_move_authorized(start_cell, end_cell) + + +def test_rook_moves(): + x_start = 2 + y_start = 2 + rook = material.Rook(white=True, x=x_start, y=y_start) + start_cell = engine.Cell(x=x_start, y=y_start, piece=rook) + + # Assert can go everywhere it is supposed to + x_end = 2 + y_end = 7 + end_cell = engine.Cell(x=x_end, y=y_end, piece=None) + assert rook.piece_move_authorized(start_cell, end_cell) + x_end = 2 + y_end = 0 + end_cell = engine.Cell(x=x_end, y=y_end, piece=None) + assert rook.piece_move_authorized(start_cell, end_cell) + x_end = 7 + y_end = 2 + end_cell = engine.Cell(x=x_end, y=y_end, piece=None) + assert rook.piece_move_authorized(start_cell, end_cell) + x_end = 0 + y_end = 2 + end_cell = engine.Cell(x=x_end, y=y_end, piece=None) + assert rook.piece_move_authorized(start_cell, end_cell) + + # Assert cannot go differently + x_end = 4 + y_end = 0 + end_cell = engine.Cell(x=x_end, y=y_end, piece=None) + assert not rook.piece_move_authorized(start_cell, end_cell) + x_end = 0 + y_end = 4 + end_cell = engine.Cell(x=x_end, y=y_end, piece=None) + assert not rook.piece_move_authorized(start_cell, end_cell) + x_end = 2 + y_end = -1 + end_cell = engine.Cell(x=x_end, y=y_end, piece=None) + assert not rook.piece_move_authorized(start_cell, end_cell) + x_end = 8 + y_end = 2 + end_cell = engine.Cell(x=x_end, y=y_end, piece=None) + assert not rook.piece_move_authorized(start_cell, end_cell) + + +def test_queen_moves(): + x_start = 2 + y_start = 2 + queen = material.Queen(white=True, x=x_start, y=y_start) + start_cell = engine.Cell(x=x_start, y=y_start, piece=queen) + + # Assert can go everywhere it is supposed to + x_end = 2 + y_end = 7 + end_cell = engine.Cell(x=x_end, y=y_end, piece=None) + assert queen.piece_move_authorized(start_cell, end_cell) + x_end = 7 + y_end = 2 + end_cell = engine.Cell(x=x_end, y=y_end, piece=None) + assert queen.piece_move_authorized(start_cell, end_cell) + x_end = 2 + y_end = 0 + end_cell = engine.Cell(x=x_end, y=y_end, piece=None) + assert queen.piece_move_authorized(start_cell, end_cell) + x_end = 0 + y_end = 2 + end_cell = engine.Cell(x=x_end, y=y_end, piece=None) + assert queen.piece_move_authorized(start_cell, end_cell) + x_end = 0 + y_end = 0 + end_cell = engine.Cell(x=x_end, y=y_end, piece=None) + assert queen.piece_move_authorized(start_cell, end_cell) + x_end = 7 + y_end = 7 + end_cell = engine.Cell(x=x_end, y=y_end, piece=None) + assert queen.piece_move_authorized(start_cell, end_cell) + x_end = 0 + y_end = 4 + end_cell = engine.Cell(x=x_end, y=y_end, piece=None) + assert queen.piece_move_authorized(start_cell, end_cell) + x_end = 4 + y_end = 0 + end_cell = engine.Cell(x=x_end, y=y_end, piece=None) + assert queen.piece_move_authorized(start_cell, end_cell) + + + # Assert cannot go differently + x_end = 4 + y_end = 1 + end_cell = engine.Cell(x=x_end, y=y_end, piece=None) + assert not queen.piece_move_authorized(start_cell, end_cell) + x_end = 1 + y_end = 4 + end_cell = engine.Cell(x=x_end, y=y_end, piece=None) + assert not queen.piece_move_authorized(start_cell, end_cell) + x_end = 3 + y_end = 6 + end_cell = engine.Cell(x=x_end, y=y_end, piece=None) + assert not queen.piece_move_authorized(start_cell, end_cell) + x_end = 6 + y_end = 3 + end_cell = engine.Cell(x=x_end, y=y_end, piece=None) + assert not queen.piece_move_authorized(start_cell, end_cell) + x_end = 2 + y_end = -2 + end_cell = engine.Cell(x=x_end, y=y_end, piece=None) + assert not queen.piece_move_authorized(start_cell, end_cell) + x_end = 8 + y_end = 2 + end_cell = engine.Cell(x=x_end, y=y_end, piece=None) + assert not queen.piece_move_authorized(start_cell, end_cell) + + +def test_king_moves(): + x_start = 2 + y_start = 2 + king = material.King(white=True, x=x_start, y=y_start) + start_cell = engine.Cell(x=x_start, y=y_start, piece=king) + + # Assert can go everywhere it is supposed to + x_end = 2 + y_end = 1 + end_cell = engine.Cell(x=x_end, y=y_end, piece=None) + assert king.piece_move_authorized(start_cell, end_cell) + x_end = 1 + y_end = 2 + end_cell = engine.Cell(x=x_end, y=y_end, piece=None) + assert king.piece_move_authorized(start_cell, end_cell) + x_end = 2 + y_end = 3 + end_cell = engine.Cell(x=x_end, y=y_end, piece=None) + assert king.piece_move_authorized(start_cell, end_cell) + x_end = 3 + y_end = 2 + end_cell = engine.Cell(x=x_end, y=y_end, piece=None) + assert king.piece_move_authorized(start_cell, end_cell) + x_end = 1 + y_end = 3 + end_cell = engine.Cell(x=x_end, y=y_end, piece=None) + assert king.piece_move_authorized(start_cell, end_cell) + x_end = 1 + y_end = 1 + end_cell = engine.Cell(x=x_end, y=y_end, piece=None) + assert king.piece_move_authorized(start_cell, end_cell) + x_end = 3 + y_end = 1 + end_cell = engine.Cell(x=x_end, y=y_end, piece=None) + assert king.piece_move_authorized(start_cell, end_cell) + x_end = 3 + y_end = 3 + end_cell = engine.Cell(x=x_end, y=y_end, piece=None) + assert king.piece_move_authorized(start_cell, end_cell) + + + # Assert cannot go differently + x_end = 4 + y_end = 1 + end_cell = engine.Cell(x=x_end, y=y_end, piece=None) + assert not king.piece_move_authorized(start_cell, end_cell) + x_end = 1 + y_end = 4 + end_cell = engine.Cell(x=x_end, y=y_end, piece=None) + assert not king.piece_move_authorized(start_cell, end_cell) + x_end = 2 + y_end = 2 + end_cell = engine.Cell(x=x_end, y=y_end, piece=None) + assert not king.piece_move_authorized(start_cell, end_cell) + x_end = 4 + y_end = 4 + end_cell = engine.Cell(x=x_end, y=y_end, piece=None) + assert not king.piece_move_authorized(start_cell, end_cell) + \ No newline at end of file