Мария обнови решението на 29.11.2022 16:45 (преди почти 2 години)
+class ChessException(Exception):
+ def __init__(self, value):
+ self.value = value
+
+ def __str__(self):
+ return self.value
+
+
+class ChessScore:
+ def __init__(self, figures):
+ self._figures = figures
+ self.values = {'R': 5, 'N': 3, 'B': 3, 'K': 4, 'Q': 9, 'P': 1, 'r': 5, 'n': 3, 'b': 3, 'k': 4, 'q': 9, 'p': 1}
+
+ def __int__(self):
+ figures_sum = 0
+ for figure in self._figures:
+ figures_sum += self.values[figure]
+ return figures_sum
+
+ def __str__(self):
+ return str(int(self))
+
+ def __lt__(self, other):
+ return int(self) < int(other)
+
+ def __le__(self, other):
+ return int(self) <= int(other)
+
+ def __gt__(self, other):
+ return int(self) > int(other)
+
+ def __ge__(self, other):
+ return int(self) >= int(other)
+
+ def __eq__(self, other):
+ return int(self) == int(other)
+
+ def __ne__(self, other):
+ return int(self) != int(other)
+
+ def __add__(self, other):
+ return int(self) + int(other)
+
+ def __sub__(self, other):
+ return int(self) - int(other)
+
+
+class ChessPosition:
+ def __init__(self, fen):
+ self._fen = fen
+ self._split_fen = fen.split("/")
+ for row in self._split_fen:
+ if 'kk' in row.lower():
+ raise ChessException("kings")
+ if self._fen.count('K') != 1 and self._fen.count('k') != 1:
+ raise ChessException("kings")
+ if 'p' in self._split_fen[0].lower() and 'p' in self._split_fen[7].lower():
+ raise ChessException("pawns")
+
+ def __str__(self):
+ return self._fen
+
+ def __len__(self):
+ figures = []
+ for figure in self._fen:
+ if figure in ['R', 'N', 'B', 'K', 'Q', 'P','r', 'n', 'b', 'k', 'q', 'p']:
+ figures.append(figure)
+ figure_score = ChessScore(figures)
+ return int(figure_score)
+
+ def get_white_score(self):
+ white = []
+ for figure in self._fen:
+ if figure in ['R', 'N', 'B', 'K', 'Q', 'P']:
+ white.append(figure)
+ return ChessScore(white)
+
+ def get_black_score(self):
+ black = []
+ for figure in self._fen:
+ if figure in ['r', 'n', 'b', 'k', 'q', 'p']:
+ black.append(figure)
+ return ChessScore(black)
+
+ 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
в тялото на класа, защото този инициализатор реално не прави нищо.