Даниела обнови решението на 28.11.2022 17:05 (преди почти 2 години)
+from itertools import repeat
+
+class ChessException(Exception):
+ def __init__(self, message):
В този клас можеш да минеш и само с един pass
Останалото така или иначе не прави нищо.
+ self._message = message
+ super().__init__(self._message)
+
+
+class ChessPosition:
+ _all_white_pieces = 'RNBQKP'
+ _all_black_pieces = 'rnbqkp'
+
+ def __init__(self, FEN_position):
+ self.FEN = FEN_position
+ self._white_pieces = []
+ self._black_pieces = []
+ self._generate_board()
+ self._check_FEN()
+
+ def __str__(self):
+ return self.FEN
+
+ def __len__(self):
+ all_pieces = 0
+ for row_pieces in self.board:
+ all_pieces += len([piece for piece in row_pieces if piece != '-'])
+ return all_pieces
+
+ def __getitem__(self, position):
+ column = ord(position[0].upper()) - ord('A') if position != '' else 0
+ row = ord(position[1]) - ord('1') if position != '' else 0
+ return self.board[row][column] if self.board[row][column] != '-' else None
+
+ def _check_FEN(self):
+ kings = 'Kk'
+ pawns = 'Pp'
+ found_pieces = {'K' : 0, 'k' : 0, pawns : 0}
+ for row, row_pieces in enumerate(self.board):
+ for column, piece in enumerate(row_pieces):
+ if piece in kings:
Би могла да спестиш малко код, ако просто вземеш позицията на двата царя и изчислиш дължината на вектора между тях, но тъй като сме в края на срока за домашното, не го прави, за да не рискуваш грешки в последния момент. Просто препоръка за следващи подобни задачи.
+ '''next to in row'''
+ if 1 <= column and self.board[row][column - 1] in kings:
+ raise ChessException('kings')
+ if column <= 6 and self.board[row][column + 1] in kings:
+ raise ChessException('kings')
+ '''diagonal below'''
+ if 1 <= row and 1 <= column and self.board[row - 1][column - 1] in kings:
+ raise ChessException('kings')
+ if 1 <= row and column <= 6 and self.board[row - 1][column + 1] in kings:
+ raise ChessException('kings')
+ '''diagonal above'''
+ if row <= 6 and 1 <= column and self.board[row + 1][column - 1] in kings:
+ raise ChessException('kings')
+ if row <= 6 and column <= 6 and self.board[row + 1][column + 1] in kings:
+ raise ChessException('kings')
+ '''bolow'''
+ if 1 <= row and self.board[row - 1][column] in kings:
+ raise ChessException('kings')
+ '''above'''
+ if row <= 6 and self.board[row + 1][column] in kings:
+ raise ChessException('kings')
+ found_pieces[piece] += 1
+ if piece in pawns and row in (0, 7):
+ found_pieces[pawns] += 1
+ if found_pieces['K'] != 1 or found_pieces['k'] != 1:
+ raise ChessException('kings')
+ if found_pieces[pawns] != 0:
+ raise ChessException('pawns')
+
+ def _generate_board(self):
+ self.board = []
+ for row in reversed(self.FEN.split('/')):
+ column = []
+ for piece in row:
+ if piece in self._all_white_pieces + self._all_black_pieces:
+ column.append(piece)
+ self._white_pieces.append(piece) if piece in self._all_white_pieces else self._black_pieces.append(piece)
+ elif piece in '12345678':
+ column.extend(list(repeat('-', int(piece))))
+ self.board.append(column)
+
+ def get_white_score(self):
+ return ChessScore(list(map(lambda x : x.lower(), self._white_pieces)))
Моля не слагай интервал преди :
. Освен това, тук можеш директно да използваш lower
:
map(str.lower, some_list)
+
+ def get_black_score(self):
+ return ChessScore(self._black_pieces)
+
+ 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()
+
+
+class ChessScore:
+ def __init__(self, pieces):
+ self._pieces = pieces
+
+ def __int__(self):
+ pieces_to_points = {
+ 'r' : 5,
+ 'n' : 3,
+ 'b' : 3,
+ 'q' : 9,
+ 'k' : 4,
+ 'p' : 1
+ }
+ score = 0
+ for piece in self._pieces:
+ score += pieces_to_points[piece]
+ return score
+
+ 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)
Би могла да спестиш малко код, ако просто вземеш позицията на двата царя и изчислиш дължината на вектора между тях, но тъй като сме в края на срока за домашното, не го прави, за да не рискуваш грешки в последния момент. Просто препоръка за следващи подобни задачи.