Александра обнови решението на 29.11.2022 17:08 (преди почти 2 години)
+points_per_figure = [ ( "r" , 5),
+ ( "n" , 3 ),
+ ( "b" , 3 ),
+ ( "q" , 9 ),
+ ( "k" , 4 ),
+ ( "p" , 1 )]
+
+positions = "ABCDEFGH"
+
+class ChessException(Exception):
+ def __init__(self, message = "Default message"):
+ self._message = message
+ super().__init__(self._message)
+
+
+class ChessScore:
+ def __init__(self, figures):
+ self.figures = figures
+ points = 0
+ for figure in self.figures:
+ for chess_figure, chess_points in points_per_figure:
+ if figure.lower() == chess_figure:
+ points += chess_points
+ self.points = points
+
+ def __int__(self):
+ return self.points
+
+ def __lt__(self, other):
+ return self.points < other.points
+
+ def __gt__(self, other):
+ return self.points > other.points
+
+ def __le__(self, other):
+ return self.points <= other.points
+
+ def __ge__(self, other):
+ return self.points >= other.points
+
+ def __eq__(self, other):
+ return self.points == other.points
+
+ def __ne__(self, other):
+ return self.points != other.points
+
+ def __add__(self, other):
+ return self.points + other.points
+
+ def __sub__(self, other):
+ return abs(self.points - other.points)
+
+
+class ChessPosition:
+ def __init__(self, fen):
+ self.fen = fen
+
+
+ def fen_easy(self):
+ easy_fen = ""
+ for i in range(len(self.fen)):
+ if self.fen[i].isnumeric():
+ for j in range(int(self.fen[i])):
+ easy_fen += self.fen[i]
+ elif self.fen[i].isalpha():
+ easy_fen += self.fen[i]
+
+ return easy_fen
+
+ def __getitem__(self, subscript):
+ chess_position = abs(int(subscript[1]) - 8 ) * 8 + int(positions.find(subscript[0]))
+ print(self.fen_easy())
+ if not self.fen_easy()[chess_position].isalpha():
+ return None
+ else:
+ return self.fen_easy()[chess_position]
+
+ def __print__(self):
+ print(self.fen)
+
+ def __str__(self):
+ return self.fen
+
+ def __len__(self):
+ lenght = 64
+ for i in range(0, len(self.fen)):
+ if self.fen[i].isnumeric():
+ lenght -= int(self.fen[i])
+ return lenght
+
+ def get_white_score(self):
+ white_figures = []
+ for i in range(0, len(self.fen)):
+ if self.fen[i].isupper():
+ white_figures.append(self.fen[i])
+
+ return ChessScore(white_figures).__int__()
+
+ def get_black_score(self):
+ black_figures = []
+ for i in range(0, len(self.fen)):
+ if not self.fen[i].isupper():
+ black_figures.append(self.fen[i])
+
+ return ChessScore(black_figures).__int__()
+
+ def white_is_winning(self):
+ return self.get_white_score() > self.get_black_score()
+
+ def black_is_winning(self):
+ return self.get_white_score() < self.get_black_score()
+
+ def is_equal(self):
+ return self.get_white_score() == self.get_black_score()
Можеш да минеш и само с един
pass
в тялото на класа, защото този инициализатор реално не прави нищо.Ако
points_per_figure
е речник, можеш директно да вземеш стойност по ключ (фигура) и да спестиш цикъл.Доколкото знам такъв дъндър метод няма.
По-добре направо
for char in self.fen
. Индексът не ти трябва.По-добре
int(ChessScore(white_figures)