Тодор обнови решението на 28.11.2022 03:04 (преди почти 2 години)
+class ChessException(Exception):
+ def __init__(self, message):
Можеш просто да сложиш един pass
в тялото на класа си. Този __init__
така или иначе нищо не прави.
+ self._message = message
+ super().__init__(self._message)
+
+
+chess_figures = "pPrRnNbBqQkK"
+points = {'r': 5, 'n': 3, 'b': 3, 'q': 9, 'k': 4, 'p': 1}
+
+
+class ChessPosition:
+ def __init__(self, fen):
+ self.fen = fen
+ self.board = []
+ self._set_board(fen)
+ self._check_kings()
+ self._check_pawns()
+
+ def _set_board(self, fen):
+ rows = fen.split('/')
+ for row in rows:
+ new_row = ''
+ for figure in row:
+ if figure.isdigit():
+ new_row += ' ' * int(figure)
+ else:
+ new_row += figure
+ self.board.append(new_row)
+
+ def _check_kings(self):
+ white_king_count = 0
+ black_king_count = 0
+ white_king_pos = [-1, -1] # row col
+ black_king_pos = [-1, -1] # row col
+ i, j = 0, 0
+ for row in self.board:
+ j = 0
+ for figure in row:
+ if figure == 'k':
+ black_king_count += 1
+ black_king_pos = [i, j]
+ if black_king_count > 1:
+ raise ChessException("kings")
+ elif figure == 'K':
+ white_king_count += 1
+ white_king_pos = [i, j]
+ if white_king_count > 1:
+ raise ChessException("kings")
+ j += 1
+ i += 1
+
+ if black_king_count * white_king_count == 0:
+ raise ChessException("kings")
+ if white_king_pos[0] == black_king_pos[0] and abs(white_king_pos[1] - black_king_pos[1]) == 1:
+ raise ChessException("kings")
+ if white_king_pos[1] == black_king_pos[1] and abs(white_king_pos[0] - black_king_pos[0]) == 1:
+ raise ChessException("kings")
+ if black_king_pos in [[white_king_pos[0] + 1, white_king_pos[1] + 1],
+ [white_king_pos[0] - 1, white_king_pos[1] - 1],
+ [white_king_pos[0] - 1, white_king_pos[1] + 1],
+ [white_king_pos[0] + 1, white_king_pos[1] - 1]]:
+ raise ChessException("kings")
+
+ def _check_pawns(self):
+ if 'P' in self.board[0].upper() + self.board[7].upper():
+ raise ChessException("pawns")
+
+ def get_white_score(self):
+ return ChessScore([i.lower() for i in self.fen if i.isupper()])
+
+ def get_black_score(self):
+ return ChessScore([i for i in self.fen if i.islower()])
+
+ def white_is_winning(self):
+ return self.get_white_score() > self.get_black_score()
+
+ def black_is_winning(self):
+ return self.get_black_score() > self.get_white_score()
+
+ def is_equal(self):
+ return self.get_white_score() == self.get_black_score()
+
+ def __str__(self):
+ return self.fen
+
+ def __len__(self):
+ figures = 0
+ for figure in self.fen:
+ if figure in chess_figures:
+ figures += 1
+ return figures
+
+ def __getitem__(self, index):
+ i = int(index[1])
+ j = ord(index[0]) - ord('A')
+ i = 8 - i
+ return self.board[i][j] if self.board[i][j] in chess_figures else None
+
+
+class ChessScore:
+ def __init__(self, figures):
+ self.figures = {}
+ for figure in figures:
+ if figure in chess_figures.lower():
+ try:
+ self.figures[figure] += 1
+ except KeyError:
+ self.figures[figure] = 1
Не виждам причина да е 1, По-скоро бих написал нула, но така или иначе не е по-условието, така че няма драма.
+
+ def __int__(self):
+ result = 0
+ for key, value in self.figures.items():
+ result += (points.get(key, 1) * value)
+ return result
+
+ 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)
+
Чисто и ясно решение. Получаваш бонус точка.
Благодаря! :)
Можеш просто да сложиш един
pass
в тялото на класа си. Този__init__
така или иначе нищо не прави.Не виждам причина да е 1, По-скоро бих написал нула, но така или иначе не е по-условието, така че няма драма.