simplified moves, made them algebraic
This commit is contained in:
parent
13e3675665
commit
87e8e75c04
@ -2,9 +2,17 @@
|
|||||||
from logic.position import Position
|
from logic.position import Position
|
||||||
from enum import Enum
|
from enum import Enum
|
||||||
|
|
||||||
|
class CastleSide(Enum):
|
||||||
|
Neither = ""
|
||||||
|
King = "O-O"
|
||||||
|
Queen = "O-O-O"
|
||||||
|
|
||||||
class Move:
|
class Move:
|
||||||
def __init__(self, is_capturing: bool) -> None:
|
def __init__(self, piece: "Piece", pos: Position,/, is_capturing: bool = False, castle_side: CastleSide = CastleSide.Neither) -> None:
|
||||||
|
self.piece = piece
|
||||||
|
self.pos = pos
|
||||||
self.is_capturing = is_capturing
|
self.is_capturing = is_capturing
|
||||||
|
self.castle_side = castle_side
|
||||||
|
|
||||||
def to_algebraic(self) -> str:
|
def to_algebraic(self) -> str:
|
||||||
raise NotImplementedError("The move can't be translated to algbraic notation, as it was not implemented")
|
raise NotImplementedError("The move can't be translated to algbraic notation, as it was not implemented")
|
||||||
@ -13,13 +21,18 @@ class Move:
|
|||||||
def from_algebraic(move: str) -> "Move":
|
def from_algebraic(move: str) -> "Move":
|
||||||
raise NotImplementedError("The move can't be translated from algbraic notation, as it was not implemented")
|
raise NotImplementedError("The move can't be translated from algbraic notation, as it was not implemented")
|
||||||
|
|
||||||
|
def __str__(self) -> str:
|
||||||
|
if self.castle_side == CastleSide.King:
|
||||||
|
return "O-O"
|
||||||
|
if self.castle_side == CastleSide.Queen:
|
||||||
|
return "O-O-O"
|
||||||
|
|
||||||
class PieceMove(Move):
|
ret = ""
|
||||||
def __init__(self, piece: "Piece", pos: Position,/, is_capturing: bool = False) -> None:
|
if type(self.piece).__name__ != "Pawn":
|
||||||
super().__init__(is_capturing)
|
ret += self.piece.letter().upper()
|
||||||
self.piece = piece
|
|
||||||
self.pos = pos
|
|
||||||
|
|
||||||
class Castle(Move, Enum):
|
ret += str(self.pos)
|
||||||
KING_SIDE_CASTLE = False
|
return ret
|
||||||
QUEEN_SIDE_CASTLE = False
|
|
||||||
|
def __repr__(self) -> str:
|
||||||
|
return str(self)
|
||||||
|
@ -1,6 +1,9 @@
|
|||||||
from .piece import Piece
|
from .piece import Piece
|
||||||
|
|
||||||
class Knight(Piece):
|
class Knight(Piece):
|
||||||
|
def letter(self):
|
||||||
|
return "n"
|
||||||
|
|
||||||
def legal_moves(self, board: "Board") -> list["Move"]:
|
def legal_moves(self, board: "Board") -> list["Move"]:
|
||||||
ret = []
|
ret = []
|
||||||
for dx, dy in [
|
for dx, dy in [
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
from logic.move import Move, PieceMove
|
from logic.move import Move
|
||||||
from logic.pieces.piece import Colour, Piece
|
from logic.pieces.piece import Colour, Piece
|
||||||
from logic.position import Position
|
from logic.position import Position
|
||||||
|
|
||||||
@ -13,7 +13,7 @@ class Pawn(Piece):
|
|||||||
(self.colour == Colour.BLACK and (capturable_piece := board.piece_at(self.pos.x - 1, self.pos.y - 1)))
|
(self.colour == Colour.BLACK and (capturable_piece := board.piece_at(self.pos.x - 1, self.pos.y - 1)))
|
||||||
):
|
):
|
||||||
if capturable_piece.colour != self.colour:
|
if capturable_piece.colour != self.colour:
|
||||||
ret.append(PieceMove(self, capturable_piece.pos, is_capturing = True))
|
ret.append(Move(self, capturable_piece.pos, is_capturing = True))
|
||||||
|
|
||||||
# can we capture to the right?
|
# can we capture to the right?
|
||||||
if self.pos.x < 7 and (
|
if self.pos.x < 7 and (
|
||||||
@ -22,19 +22,19 @@ class Pawn(Piece):
|
|||||||
(self.colour == Colour.BLACK and (capturable_piece := board.piece_at(self.pos.x + 1, self.pos.y - 1)))
|
(self.colour == Colour.BLACK and (capturable_piece := board.piece_at(self.pos.x + 1, self.pos.y - 1)))
|
||||||
):
|
):
|
||||||
if capturable_piece.colour != self.colour:
|
if capturable_piece.colour != self.colour:
|
||||||
ret.append(PieceMove(self, capturable_piece.pos, is_capturing = True))
|
ret.append(Move(self, capturable_piece.pos, is_capturing = True))
|
||||||
|
|
||||||
if self.colour == Colour.WHITE:
|
if self.colour == Colour.WHITE:
|
||||||
for dy in range(1, 3 if self.pos.y == 1 else 2):
|
for dy in range(1, 3 if self.pos.y == 1 else 2):
|
||||||
if self.pos.y + dy > 7 or board.piece_at(self.pos.x, self.pos.y + dy):
|
if self.pos.y + dy > 7 or board.piece_at(self.pos.x, self.pos.y + dy):
|
||||||
break
|
break
|
||||||
pos = Position(self.pos.x, self.pos.y + dy)
|
pos = Position(self.pos.x, self.pos.y + dy)
|
||||||
ret.append(PieceMove(self, pos))
|
ret.append(Move(self, pos))
|
||||||
else:
|
else:
|
||||||
for dy in range(1, 3 if self.pos.y == 6 else 2):
|
for dy in range(1, 3 if self.pos.y == 6 else 2):
|
||||||
if self.pos.y - dy < 0 or board.piece_at(self.pos.x, self.pos.y - dy):
|
if self.pos.y - dy < 0 or board.piece_at(self.pos.x, self.pos.y - dy):
|
||||||
break
|
break
|
||||||
pos = Position(self.pos.x, self.pos.y - dy)
|
pos = Position(self.pos.x, self.pos.y - dy)
|
||||||
ret.append(PieceMove(self, pos))
|
ret.append(Move(self, pos))
|
||||||
|
|
||||||
return ret
|
return ret
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
from logic.move import Move, PieceMove
|
from logic.move import Move
|
||||||
from logic.position import Position
|
from logic.position import Position
|
||||||
from enum import Enum
|
from enum import Enum
|
||||||
|
|
||||||
@ -13,6 +13,9 @@ class Piece:
|
|||||||
assert colour == Colour.WHITE or colour == Colour.BLACK, "The colour of the piece must be either Piece.WHITE or Piece.BLACK"
|
assert colour == Colour.WHITE or colour == Colour.BLACK, "The colour of the piece must be either Piece.WHITE or Piece.BLACK"
|
||||||
self.colour = colour
|
self.colour = colour
|
||||||
|
|
||||||
|
def letter(self):
|
||||||
|
return type(self).__name__[0].lower()
|
||||||
|
|
||||||
def _look_direction(self, board: "Board", mult_dx: int, mult_dy: int):
|
def _look_direction(self, board: "Board", mult_dx: int, mult_dy: int):
|
||||||
ret = []
|
ret = []
|
||||||
for d in range(1, 8):
|
for d in range(1, 8):
|
||||||
@ -34,10 +37,10 @@ class Piece:
|
|||||||
piece = board.piece_at(x, y)
|
piece = board.piece_at(x, y)
|
||||||
|
|
||||||
if piece is None:
|
if piece is None:
|
||||||
return PieceMove(self, Position(x, y))
|
return Move(self, Position(x, y))
|
||||||
|
|
||||||
if piece.colour != self.colour:
|
if piece.colour != self.colour:
|
||||||
return PieceMove(self, Position(x, y), is_capturing=True)
|
return Move(self, Position(x, y), is_capturing=True)
|
||||||
return None
|
return None
|
||||||
|
|
||||||
def position(self) -> Position:
|
def position(self) -> Position:
|
||||||
|
@ -29,8 +29,7 @@ class Position:
|
|||||||
return hash((self.x, self.y))
|
return hash((self.x, self.y))
|
||||||
|
|
||||||
def __str__(self) -> str:
|
def __str__(self) -> str:
|
||||||
return f"{self.x, self.y}"
|
return f"{Position._FILES[self.x]}{Position._RANKS[self.y]}"
|
||||||
|
|
||||||
def __repr__(self) -> str:
|
def __repr__(self) -> str:
|
||||||
return str(self)
|
return str(self)
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user