Compare commits

...

2 commits

Author SHA1 Message Date
Bruno BELANYI 1646c055fd Use 'from_gdb' constructors in GDB utils
All checks were successful
ci/woodpecker/push/check Pipeline was successful
Makes it much more readable.
2024-04-04 00:55:41 +01:00
Bruno BELANYI fcbcc3cdef Add 'from_gdb' constructors in GDB utils 2024-04-04 00:55:41 +01:00

View file

@ -28,6 +28,10 @@ class Square(object):
val = val._val
self._val = val
@classmethod
def from_gdb(cls, val):
return cls(int(val))
@classmethod
def from_file_rank(cls, file, rank):
return cls(file * 8 + rank)
@ -54,6 +58,10 @@ class Bitboard(object):
val = val._val
self._val = val
@classmethod
def from_gdb(cls, val):
return cls(int(val["__0"]))
def __str__(self):
return "[" + ", ".join(map(str, self.squares)) + "]"
@ -80,6 +88,10 @@ class CastleRights(enum.IntEnum):
QUEEN_SIDE = 2
BOTH_SIDES = 3
@classmethod
def from_gdb(cls, val):
return cls(int(val))
def __str__(self):
return self.name.title().replace("_", "")
@ -93,6 +105,10 @@ class Color(enum.IntEnum):
WHITE = 0
BLACK = 1
@classmethod
def from_gdb(cls, val):
return cls(int(val))
def __str__(self):
return self.name.title()
@ -112,6 +128,10 @@ class File(enum.IntEnum):
G = 6
H = 7
@classmethod
def from_gdb(cls, val):
return cls(int(val))
def __str__(self):
return self.name.title()
@ -131,6 +151,10 @@ class Rank(enum.IntEnum):
Seventh = 6
Eighth = 7
@classmethod
def from_gdb(cls, val):
return cls(int(val))
def __str__(self):
return self.name.title()
@ -148,6 +172,10 @@ class Piece(enum.IntEnum):
KNIGHT = 4
PAWN = 5
@classmethod
def from_gdb(cls, val):
return cls(int(val))
def __str__(self):
return self.name.title()
@ -179,6 +207,10 @@ class Move(object):
def __init__(self, val):
self._val = val
@classmethod
def from_gdb(cls, val):
return cls(int(val))
@property
def piece(self):
return Piece(self._val >> self.PIECE_SHIFT & self.PIECE_MASK)
@ -261,13 +293,13 @@ class ChessBoard(object):
@classmethod
def from_gdb(cls, val):
return cls(
[int(val["piece_occupancy"][p]["__0"]) for p in Piece],
[int(val["color_occupancy"][c]["__0"]) for c in Color],
[int(val["castle_rights"][c]) for c in Color],
[Bitboard.from_gdb(val["piece_occupancy"][p]) for p in Piece],
[Bitboard.from_gdb(val["color_occupancy"][c]) for c in Color],
[CastleRights.from_gdb(val["castle_rights"][c]) for c in Color],
int(val["half_move_clock"]),
int(val["total_plies"]),
Color(int(val["side"])),
optional(int, val["en_passant"]),
Color.from_gdb(val["side"]),
optional(Square.from_gdb, val["en_passant"]),
)
def at(self, square):
@ -317,7 +349,7 @@ class SquarePrinter(object):
"Print a seer::board::square::Square"
def __init__(self, val):
self._val = Square(val)
self._val = Square.from_gdb(val)
def to_string(self):
return str(self._val)
@ -327,7 +359,7 @@ class BitboardPrinter(object):
"Print a seer::board::bitboard::Bitboard"
def __init__(self, val):
self._val = Bitboard(int(val["__0"]))
self._val = Bitboard.from_gdb(val)
def to_string(self):
return "Bitboard{" + str(self._val)[1:-1] + "}"
@ -337,7 +369,7 @@ class CastleRightsPrinter(object):
"Print a seer::board::castle_rights::CastleRights"
def __init__(self, val):
self._val = CastleRights(int(val))
self._val = CastleRights.from_gdb(val)
def to_string(self):
return str(self._val)
@ -347,7 +379,7 @@ class ColorPrinter(object):
"Print a seer::board::color::Color"
def __init__(self, val):
self._val = Color(int(val))
self._val = Color.from_gdb(val)
def to_string(self):
return str(self._val)
@ -357,7 +389,7 @@ class FilePrinter(object):
"Print a seer::board::file::File"
def __init__(self, val):
self._val = File(int(val))
self._val = File.from_gdb(val)
def to_string(self):
return str(self._val)
@ -367,7 +399,7 @@ class RankPrinter(object):
"Print a seer::board::rank::Rank"
def __init__(self, val):
self._val = Rank(int(val))
self._val = Rank.from_gdb(val)
def to_string(self):
return str(self._val)
@ -377,7 +409,7 @@ class PiecePrinter(object):
"Print a seer::board::piece::Piece"
def __init__(self, val):
self._val = Piece(int(val))
self._val = Piece.from_gdb(val)
def to_string(self):
return str(self._val)
@ -387,7 +419,7 @@ class MovePrinter(object):
"Print a seer::board::move::Move"
def __init__(self, val):
self._val = Move(int(val["__0"]))
self._val = Move.from_gdb(val)
def to_string(self):
return str(self._val)