Skip to content

Commit

Permalink
Merge pull request #8 from VincentAuriau/actions/black
Browse files Browse the repository at this point in the history
Format Python code with psf/black push
  • Loading branch information
VincentAuriau authored Oct 21, 2023
2 parents 8e28aff + 6ce7e54 commit 01ef266
Show file tree
Hide file tree
Showing 13 changed files with 545 additions and 316 deletions.
2 changes: 1 addition & 1 deletion python/engine/color.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class Color:
GREEN = "\x1b[32m"
WHITE = '\033[0m'
WHITE = "\033[0m"
RED = "\x1b[31m"
237 changes: 149 additions & 88 deletions python/engine/engine.py

Large diffs are not rendered by default.

138 changes: 95 additions & 43 deletions python/engine/material.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,7 @@ def is_killed(self):
return self.killed

def set_killed(self):
"""Sets the piece status to killed.
"""
"""Sets the piece status to killed."""
self.killed = True

@abstractmethod
Expand Down Expand Up @@ -149,7 +148,7 @@ def get_str(self):
str
String representation of the piece
"""
return ' '
return " "

def draw(self):
"""Method to represent the piece as a colored string in order to draw a board.
Expand Down Expand Up @@ -204,7 +203,9 @@ def __init__(self, *args, **kwargs):
"""
super().__init__(*args, **kwargs)
self.has_moved = False # if the pawn has yet been moved or not to keep ?
self.last_move_is_double = False # check for en passant, if last move was a double tap
self.last_move_is_double = (
False # check for en passant, if last move was a double tap
)

def piece_deepcopy(self):
"""Method to create an uncorrelated clone of the piece.
Expand Down Expand Up @@ -237,7 +238,6 @@ def piece_move_authorized(self, start, end):
"""
# Check if there is a piece on the landing cell
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():
return False
Expand Down Expand Up @@ -266,7 +266,9 @@ def piece_move_authorized(self, start, end):
# Initial move authorized to be two cells at once. Should check self.has_moved here ?
if start.get_x() == 1 and dx == 2 and dy == 0 and self.is_white():
return True
elif start.get_x() == 6 and dx == -2 and dy == 0 and not self.is_white():
elif (
start.get_x() == 6 and dx == -2 and dy == 0 and not self.is_white()
):
return True
else:
return False
Expand All @@ -293,7 +295,10 @@ def can_move(self, board, move):
crossed_cell = board.get_cell(move.start.get_x(), move.end.get_y())
crossed_piece = crossed_cell.get_piece()
if isinstance(crossed_piece, Pawn):
if crossed_piece.last_move_is_double and crossed_piece.is_white() != self.is_white():
if (
crossed_piece.last_move_is_double
and crossed_piece.is_white() != self.is_white()
):
# Revoir comment on update cet attribut last_move_is_double
authorized_move = True
move.complementary_passant = crossed_cell
Expand All @@ -302,10 +307,20 @@ def can_move(self, board, move):
dx = move.end.get_x() - move.start.get_x()

if dx > 1:
if board.get_cell(move.start.get_x()+1, move.start.get_y()).get_piece() is not None:
if (
board.get_cell(
move.start.get_x() + 1, move.start.get_y()
).get_piece()
is not None
):
return False
elif dx < -1:
if board.get_cell(move.start.get_x()-1, move.start.get_y()).get_piece() is not None:
if (
board.get_cell(
move.start.get_x() - 1, move.start.get_y()
).get_piece()
is not None
):
return False
"""
if move.end.get_x() == 7 and self.is_white():
Expand Down Expand Up @@ -371,7 +386,7 @@ def get_str(self):
str
String representation of the piece
"""
return ' P '
return " P "


class Bishop(Piece):
Expand Down Expand Up @@ -541,7 +556,7 @@ def get_str(self):
str
String representation of the piece
"""
return ' B '
return " B "


class Rook(Piece):
Expand Down Expand Up @@ -715,7 +730,7 @@ def get_str(self):
str
String representation of the piece
"""
return ' R '
return " R "


class Knight(Piece):
Expand Down Expand Up @@ -813,7 +828,7 @@ def get_str(self):
str
String representation of the piece
"""
return ' N '
return " N "

def get_potential_moves(self, x, y):
"""Method to list all the possible moves from coordinates. Only uses authorized movements, no other pieces on a
Expand All @@ -834,10 +849,19 @@ def get_potential_moves(self, x, y):
possible_moves = []

# All difference position that a knight can move to
combos = [(2, 1), (1, 2), (-2, 1), (2, -1), (-2, -1), (-1, 2), (1, -2), (-1, -2)]
combos = [
(2, 1),
(1, 2),
(-2, 1),
(2, -1),
(-2, -1),
(-1, 2),
(1, -2),
(-1, -2),
]
for nx, ny in combos:
if 0 <= nx+x <= 7 and 0 <= ny+y <= 7:
possible_moves.append((x+nx, y+ny))
if 0 <= nx + x <= 7 and 0 <= ny + y <= 7:
possible_moves.append((x + nx, y + ny))

return possible_moves

Expand Down Expand Up @@ -943,13 +967,19 @@ def can_move(self, board, move):
for i in range(1, abs(dx)):
x_trajectory = i * int(dx / abs(dx)) + move.start.get_x()
y_trajectory = move.start.get_y()
if board.get_cell(x_trajectory, y_trajectory).get_piece() is not None:
if (
board.get_cell(x_trajectory, y_trajectory).get_piece()
is not None
):
return False
# Along Y-axis
for i in range(1, abs(dy)):
x_trajectory = move.start.get_x()
y_trajectory = i * int(dy / abs(dy)) + move.start.get_y()
if board.get_cell(x_trajectory, y_trajectory).get_piece() is not None:
if (
board.get_cell(x_trajectory, y_trajectory).get_piece()
is not None
):
return False
return True

Expand All @@ -958,7 +988,10 @@ def can_move(self, board, move):
for i in range(1, abs(dx)):
x_trajectory = i * int(dx / abs(dx)) + move.start.get_x()
y_trajectory = i * int(dy / abs(dy)) + move.start.get_y()
if board.get_cell(x_trajectory, y_trajectory).get_piece() is not None:
if (
board.get_cell(x_trajectory, y_trajectory).get_piece()
is not None
):
return False
return True
else:
Expand Down Expand Up @@ -1048,7 +1081,7 @@ def get_str(self):
str
String representation of the piece
"""
return ' Q '
return " Q "


class King(Piece):
Expand Down Expand Up @@ -1161,22 +1194,29 @@ def can_move(self, board, move):
return True
# If move is not authorized it could mean that player is trying to do a special move, i.e. castling
else:

# Checking castling conditions on the right then on the left
if not self.castling_done and not self.has_moved and (move.end.y == 6 or move.end.y == 2):
if (
not self.castling_done
and not self.has_moved
and (move.end.y == 6 or move.end.y == 2)
):
if move.end.y == 6: # Roque vers la droite

# Getting the rook for castling
rook_to_move = board.get_cell(move.start.x, 7).get_piece()
rook_starting_coordinates = (move.start.x, 7)
rook_ending_coordinates = (move.start.x, 5)

# Listing cells that must not have material on
if isinstance(rook_to_move, Rook):
must_be_empty_cells = [board.get_cell(move.start.x, 5), board.get_cell(move.start.x, 6)]
must_not_be_threatened_cells = [board.get_cell(move.start.x, 4),
board.get_cell(move.start.x, 5),
board.get_cell(move.start.x, 6)]
must_be_empty_cells = [
board.get_cell(move.start.x, 5),
board.get_cell(move.start.x, 6),
]
must_not_be_threatened_cells = [
board.get_cell(move.start.x, 4),
board.get_cell(move.start.x, 5),
board.get_cell(move.start.x, 6),
]
else:
return False

Expand All @@ -1187,13 +1227,17 @@ def can_move(self, board, move):

# Getting the rook
if isinstance(rook_to_move, Rook):

# Listing cells that must not have material on
must_be_empty_cells = [board.get_cell(move.start.x, 1), board.get_cell(move.start.x, 2),
board.get_cell(move.start.x, 3)]
must_not_be_threatened_cells = [board.get_cell(move.start.x, 2),
board.get_cell(move.start.x, 3),
board.get_cell(move.start.x, 4)]
# Listing cells that must not have material on
must_be_empty_cells = [
board.get_cell(move.start.x, 1),
board.get_cell(move.start.x, 2),
board.get_cell(move.start.x, 3),
]
must_not_be_threatened_cells = [
board.get_cell(move.start.x, 2),
board.get_cell(move.start.x, 3),
board.get_cell(move.start.x, 4),
]
else:
return False

Expand All @@ -1212,17 +1256,26 @@ def can_move(self, board, move):

# Verify that all conditions are met and completes the move so that it has the full castling information
# to operate all the movements
conditions_to_castling = [not rook_to_move.has_moved, empty_cells_check, not_threatened_cells]
conditions_to_castling = [
not rook_to_move.has_moved,
empty_cells_check,
not_threatened_cells,
]
if all(conditions_to_castling):
move.complementary_castling = rook_to_move, board.get_cell(rook_starting_coordinates[0],
rook_starting_coordinates[1]), \
board.get_cell(rook_ending_coordinates[0], rook_ending_coordinates[1])
move.complementary_castling = (
rook_to_move,
board.get_cell(
rook_starting_coordinates[0], rook_starting_coordinates[1]
),
board.get_cell(
rook_ending_coordinates[0], rook_ending_coordinates[1]
),
)
return True
else:
return False
return False


def get_potential_moves(self, x, y):
"""Method to list all the possible moves from coordinates. Only uses authorized movements, no other pieces on a
board.
Expand All @@ -1244,8 +1297,8 @@ def get_potential_moves(self, x, y):
# All possible moves
combos = [(1, 0), (1, 1), (-1, 1), (1, -1), (-1, -1), (0, 1), (0, -1), (-1, 0)]
for nx, ny in combos:
if 0 <= x+nx <= 7 and 0 <= y+ny <= 7:
possible_moves.append((nx+x, ny+y))
if 0 <= x + nx <= 7 and 0 <= y + ny <= 7:
possible_moves.append((nx + x, ny + y))

# Add castling as potential moves if not done yet
if not self.has_moved:
Expand All @@ -1262,7 +1315,7 @@ def get_str(self):
str
String representation of the piece
"""
return ' K '
return " K "

def is_checked(self, board):
"""Method to verify that the king at its current position is not threatened / checked by opponent material.
Expand Down Expand Up @@ -1296,4 +1349,3 @@ def is_checked(self, board):
#
# if verified_move:
# copied_board = board.copy()

Loading

0 comments on commit 01ef266

Please sign in to comment.