Решение на Шахматни фенове от Стилиян Иванов

Обратно към всички решения

Към профила на Стилиян Иванов

Резултати

  • 10 точки от тестове
  • 1 бонус точка
  • 11 точки общо
  • 17 успешни тест(а)
  • 0 неуспешни тест(а)

Код

class ChessException(Exception):
"""Chess exception definition class"""
def __init__(self, message):
self.message = message
super().__init__(self.message)
def __str__(self):
return self.message
class ChessPosition:
"""Chess position definition class"""
def __init__(self, string):
self.string = string
self.board = [[figure for pos in row
for figure in ([None] * int(pos) if '1' <= pos <= '8' else [pos])]
for row in self.string.split('/')]
self.__validate_kings()
self.__validate_pawns()
self.white_figures = [pos for pos in self.string if 'A' <= pos <= 'Z']
self.black_figures = [pos for pos in self.string if 'a' <= pos <= 'z']
def __validate_pawns(self):
"""Validate the position of the pawn figures"""
if any(pawn in row for pawn in ['P', 'p'] for row in [self.board[0], self.board[7]]):
raise ChessException('pawns')
def __validate_kings(self):
"""Validate the number and position of the king figures"""
if self.string.count('k') != 1 or self.string.count('K') != 1:
raise ChessException('kings')
x_white, y_white = self.__get_indices('K')
x_black, y_black = self.__get_indices('k')
if abs(x_white - x_black) <= 1 and abs(y_white - y_black) <= 1:
raise ChessException('kings')
def __get_indices(self, figure):
"""Return the indices of the first occurence of a figure"""
for x, row in enumerate(self.board):
if figure in row:
return x, row.index(figure)
def get_white_score(self):
"""Return the chess score of all white figures in the position"""
return ChessScore(self.white_figures)
def get_black_score(self):
"""Return the chess score of all black figures in the position"""
return ChessScore(self.black_figures)
def white_is_winning(self):
"""Determine whether the chess score of the white figures is higher"""
return self.get_white_score() > self.get_black_score()
def black_is_winning(self):
"""Determine whether the chess score of the black figures is higher"""
return self.get_black_score() > self.get_white_score()
def is_equal(self):
"""Determine whether the chess score of the white and of the black figures is equal"""
return self.get_white_score() == self.get_black_score()
def __len__(self):
return len(self.white_figures) + len(self.black_figures)
def __str__(self):
return self.string
def __getitem__(self, chess_index):
return self.board[8 - int(chess_index[1])][ord(chess_index[0]) - ord('A')]
class ChessScore:
"""Chess position definition class"""
__points_per_figures = {'r': 5, 'n': 3, 'b': 3, 'q': 9, 'k': 4, 'p': 1}
def __init__(self, figures):
self.sum = sum([self.__points_per_figures[figure.lower()] for figure in figures])
def __int__(self):
return self.sum
def __add__(self, other):
return self.sum + other.sum
def __sub__(self, other):
return self.sum - other.sum
def __eq__(self, other):
return self.sum == other.sum
def __ne__(self, other):
return self.sum != other.sum
def __gt__(self, other):
return self.sum > other.sum
def __ge__(self, other):
return self.sum >= other.sum
def __lt__(self, other):
return self.sum < other.sum
def __le__(self, other):
return self.sum <= other.sum

Лог от изпълнението

.................
----------------------------------------------------------------------
Ran 17 tests in 0.167s

OK

История (3 версии и 3 коментара)

Стилиян обнови решението на 28.11.2022 12:35 (преди над 1 година)

+class ChessException(Exception):
+ """Chess exception definition class"""
+
+ def __init__(self, message):
+ self.message = message
+ super().__init__(self.message)
+
+ def __str__(self):
+ return self.message
+
+
+class ChessPosition:
+ """Chess position definition class"""
+
+ def __init__(self, string):
+ self.string = string
+ self.board = [[figure for pos in row for figure in ([None] * int(pos) if '1' <= pos <= '8' else [pos])] for row in self.string.split('/')]
+ self.__validate_kings()
+ self.__validate_pawns()
+ self.white_figures = [pos for pos in self.string if 'A' <= pos <= 'Z']
+ self.black_figures = [pos for pos in self.string if 'a' <= pos <= 'z']
+
+ def __validate_pawns(self):
+ """Validate the position of the pawn figures"""
+ if any(pawn in row for pawn in ['P', 'p'] for row in [self.board[0], self.board[7]]):
+ raise ChessException('pawns')
+
+ def __validate_kings(self):
+ """Validate the number and position of the king figures"""
+ if self.string.count('k') != 1 or self.string.count('K') != 1:
+ raise ChessException('kings')
+ x_white, y_white = self.__get_indices('K')
+ x_black, y_black = self.__get_indices('k')
+ if abs(x_white - x_black) <= 1 and abs(y_white - y_black) <= 1:
+ raise ChessException('kings')
+
+ def __get_indices(self, figure):
+ """Return the indices of the first occurence of a figure"""
+ for x, row in enumerate(self.board):
+ if figure in row:
+ return x, row.index(figure)
+
+ def get_white_score(self):
+ """Return the chess score of all white figures in the position"""
+ return ChessScore(self.white_figures)
+
+ def get_black_score(self):
+ """Return the chess score of all black figures in the position"""
+ return ChessScore(self.black_figures)
+
+ def white_is_winning(self):
+ """Determine whether the chess score of the white figures is higher"""
+ return self.get_white_score() > self.get_black_score()
+
+ def black_is_winning(self):
+ """Determine whether the chess score of the black figures is higher"""
+ return self.get_black_score() > self.get_white_score()
+
+ def is_equal(self):
+ """Determine whether the chess score of the white and of the black figures is equal"""
+ return self.get_white_score() == self.get_black_score()
+
+ def __len__(self):
+ return len(self.white_figures) + len(self.black_figures)
+
+ def __str__(self):
+ return self.string
+
+ def __getitem__(self, chess_index):
+ return self.board[8 - int(chess_index[1])][ord(chess_index[0]) - ord('A')]
+
+
+class ChessScore:
+ """Chess position definition class"""
+
+ __points_per_figures = {'r': 5, 'n': 3, 'b': 3, 'q': 9, 'k': 4, 'p': 1}
+
+ def __init__(self, figures):
+ self.sum = sum([self.__points_per_figure[figure.lower()] for figure in figures])
+
+ def __int__(self):
+ return self.sum
+
+ def __add__(self, other):
+ return self.sum + other.sum
+
+ def __sub__(self, other):
+ return self.sum - other.sum
+
+ def __eq__(self, other):
+ return self.sum == other.sum
+
+ def __ne__(self, other):
+ return self.sum != other.sum
+
+ def __gt__(self, other):
+ return self.sum > other.sum
+
+ def __ge__(self, other):
+ return self.sum >= other.sum
+
+ def __lt__(self, other):
+ return self.sum < other.sum
+
+ def __le__(self, other):
+ return self.sum <= other.sum

Стилиян обнови решението на 28.11.2022 12:37 (преди над 1 година)

class ChessException(Exception):
"""Chess exception definition class"""
def __init__(self, message):
self.message = message
super().__init__(self.message)
def __str__(self):
return self.message
class ChessPosition:
"""Chess position definition class"""
def __init__(self, string):
self.string = string
- self.board = [[figure for pos in row for figure in ([None] * int(pos) if '1' <= pos <= '8' else [pos])] for row in self.string.split('/')]
+ self.board = [[figure for pos in row
+ for figure in ([None] * int(pos) if '1' <= pos <= '8' else [pos])]
+ for row in self.string.split('/')]
self.__validate_kings()
self.__validate_pawns()
self.white_figures = [pos for pos in self.string if 'A' <= pos <= 'Z']
self.black_figures = [pos for pos in self.string if 'a' <= pos <= 'z']
def __validate_pawns(self):
"""Validate the position of the pawn figures"""
if any(pawn in row for pawn in ['P', 'p'] for row in [self.board[0], self.board[7]]):
raise ChessException('pawns')
def __validate_kings(self):
"""Validate the number and position of the king figures"""
if self.string.count('k') != 1 or self.string.count('K') != 1:
raise ChessException('kings')
x_white, y_white = self.__get_indices('K')
x_black, y_black = self.__get_indices('k')
if abs(x_white - x_black) <= 1 and abs(y_white - y_black) <= 1:
raise ChessException('kings')
def __get_indices(self, figure):
"""Return the indices of the first occurence of a figure"""
for x, row in enumerate(self.board):
if figure in row:
return x, row.index(figure)
def get_white_score(self):
"""Return the chess score of all white figures in the position"""
return ChessScore(self.white_figures)
def get_black_score(self):
"""Return the chess score of all black figures in the position"""
return ChessScore(self.black_figures)
def white_is_winning(self):
"""Determine whether the chess score of the white figures is higher"""
return self.get_white_score() > self.get_black_score()
def black_is_winning(self):
"""Determine whether the chess score of the black figures is higher"""
return self.get_black_score() > self.get_white_score()
def is_equal(self):
"""Determine whether the chess score of the white and of the black figures is equal"""
return self.get_white_score() == self.get_black_score()
def __len__(self):
return len(self.white_figures) + len(self.black_figures)
def __str__(self):
return self.string
def __getitem__(self, chess_index):
return self.board[8 - int(chess_index[1])][ord(chess_index[0]) - ord('A')]
class ChessScore:
"""Chess position definition class"""
__points_per_figures = {'r': 5, 'n': 3, 'b': 3, 'q': 9, 'k': 4, 'p': 1}
def __init__(self, figures):
self.sum = sum([self.__points_per_figure[figure.lower()] for figure in figures])
def __int__(self):
return self.sum
def __add__(self, other):
return self.sum + other.sum
def __sub__(self, other):
return self.sum - other.sum
def __eq__(self, other):
return self.sum == other.sum
def __ne__(self, other):
return self.sum != other.sum
def __gt__(self, other):
return self.sum > other.sum
def __ge__(self, other):
return self.sum >= other.sum
def __lt__(self, other):
return self.sum < other.sum
def __le__(self, other):
return self.sum <= other.sum

Стилиян обнови решението на 28.11.2022 16:16 (преди над 1 година)

class ChessException(Exception):
"""Chess exception definition class"""
def __init__(self, message):
self.message = message
super().__init__(self.message)
def __str__(self):
return self.message
class ChessPosition:
"""Chess position definition class"""
def __init__(self, string):
self.string = string
self.board = [[figure for pos in row
for figure in ([None] * int(pos) if '1' <= pos <= '8' else [pos])]
for row in self.string.split('/')]
self.__validate_kings()
self.__validate_pawns()
self.white_figures = [pos for pos in self.string if 'A' <= pos <= 'Z']
self.black_figures = [pos for pos in self.string if 'a' <= pos <= 'z']
def __validate_pawns(self):
"""Validate the position of the pawn figures"""
if any(pawn in row for pawn in ['P', 'p'] for row in [self.board[0], self.board[7]]):
raise ChessException('pawns')
def __validate_kings(self):
"""Validate the number and position of the king figures"""
if self.string.count('k') != 1 or self.string.count('K') != 1:
raise ChessException('kings')
x_white, y_white = self.__get_indices('K')
x_black, y_black = self.__get_indices('k')
if abs(x_white - x_black) <= 1 and abs(y_white - y_black) <= 1:
raise ChessException('kings')
def __get_indices(self, figure):
"""Return the indices of the first occurence of a figure"""
for x, row in enumerate(self.board):
if figure in row:
return x, row.index(figure)
def get_white_score(self):
"""Return the chess score of all white figures in the position"""
return ChessScore(self.white_figures)
def get_black_score(self):
"""Return the chess score of all black figures in the position"""
return ChessScore(self.black_figures)
def white_is_winning(self):
"""Determine whether the chess score of the white figures is higher"""
return self.get_white_score() > self.get_black_score()
def black_is_winning(self):
"""Determine whether the chess score of the black figures is higher"""
return self.get_black_score() > self.get_white_score()
def is_equal(self):
"""Determine whether the chess score of the white and of the black figures is equal"""
return self.get_white_score() == self.get_black_score()
def __len__(self):
return len(self.white_figures) + len(self.black_figures)
def __str__(self):
return self.string
def __getitem__(self, chess_index):
return self.board[8 - int(chess_index[1])][ord(chess_index[0]) - ord('A')]
class ChessScore:
"""Chess position definition class"""
__points_per_figures = {'r': 5, 'n': 3, 'b': 3, 'q': 9, 'k': 4, 'p': 1}
def __init__(self, figures):
- self.sum = sum([self.__points_per_figure[figure.lower()] for figure in figures])
+ self.sum = sum([self.__points_per_figures[figure.lower()] for figure in figures])
def __int__(self):
return self.sum
def __add__(self, other):
return self.sum + other.sum
def __sub__(self, other):
return self.sum - other.sum
def __eq__(self, other):
return self.sum == other.sum
def __ne__(self, other):
return self.sum != other.sum
def __gt__(self, other):
return self.sum > other.sum
def __ge__(self, other):
return self.sum >= other.sum
def __lt__(self, other):
return self.sum < other.sum
def __le__(self, other):
return self.sum <= other.sum