Ангел обнови решението на 29.11.2022 17:22 (преди почти 2 години)
+# don't leave comments I do not want to see this code again
Say no more!
+
+PIECES_COST = {
+ 'r': 5,
+ 'n': 3,
+ 'b': 3,
+ 'q': 9,
+ 'k': 4,
+ 'p': 1
+}
+
+EMPTY_SQUARE = '-'
+
+
+def fen_to_matrix(fen):
+ rows = fen.split('/')
+
+ matrix = []
+ for row in rows:
+ result = ''
+ for charecter in row:
+ if charecter.isdigit():
+ result += EMPTY_SQUARE * int(charecter)
+ else:
+ result += charecter
+
+ matrix.append(result)
+
+ return matrix
+
+
+def locate_king(matrix, king):
+ for idx, str in enumerate(matrix):
+ res = str.find(king)
+
+ if res != -1:
+ return (idx, res)
+
+ return -1
+
+
+class ChessException(Exception):
+ def __init__(self, msg):
+ self.msg = msg
+ super().__init__(self.msg)
+
+
+class ChessPosition:
+ def __init__(self, fen):
+ if fen.count('k') != 1 or fen.count('K') != 1:
+ raise ChessException('kings')
+
+ matrix = fen_to_matrix(fen)
+
+ white_king_pos = locate_king(matrix, 'k')
+ black_king_pos = locate_king(matrix, 'K')
+
+ if (white_king_pos[0] == black_king_pos[0] and white_king_pos[1] in (black_king_pos[1] - 1, black_king_pos[1] + 1)) or (white_king_pos[1] == black_king_pos[1] and white_king_pos[0] in (black_king_pos[0] - 1, black_king_pos[0] + 1)) or (white_king_pos[0] in (black_king_pos[0] + 1, black_king_pos[0] - 1) and white_king_pos[1] in (black_king_pos[1] + 1, black_king_pos[1] - 1)):
+ raise ChessException('kings')
+
+ if 'p' in matrix[0] or 'P' in matrix[0] or 'p' in matrix[7] or 'P' in matrix[7]:
+ raise ChessException('pawns')
+
+ self.fen = fen
+ self.matrix = matrix
+
+ def get_white_score(self):
+ result = ''
+ for figure in self.fen:
+ if figure in 'RNBQKP':
+ result += figure
+
+ return ChessScore(result)
+
+ def get_black_score(self):
+ result = ''
+ for figure in self.fen:
+ if figure in 'rnbqkp':
+ result += figure
+
+ return ChessScore(result)
+
+ def white_is_winning(self):
+ return self.get_black_score() < self.get_white_score()
+
+ def black_is_winning(self):
+ return self.get_black_score() > self.get_white_score()
+
+ def is_equal(self):
+ return self.get_black_score() == self.get_white_score()
+
+ def __len__(self):
+ length = 0
+ for character in self.fen:
+ if character.lower() in 'rnbqkp':
+ length += 1
+
+ return length
+
+ def __str__(self):
+ return self.fen
+
+ def __getitem__(self, position):
+ figure = self.matrix[int(position[1]) - 1][ord(position[0]) - ord('A')]
+
+ if figure == EMPTY_SQUARE:
+ return None
+
+ return figure
+
+
+FIGURE_SCORE_MAPPING = {
+ 'r': 5,
+ 'n': 3,
+ 'b': 3,
+ 'q': 9,
+ 'k': 4,
+ 'p': 1
+}
+
+
+class ChessScore:
+ def __init__(self, figure):
+ self._score = 0
+
+ for item in figure:
+ self._score += FIGURE_SCORE_MAPPING[item.lower()]
+
+ def __int__(self):
+ return self._score
+
+ def __gt__(self, other):
+ return self._score > other._score
+
+ def __ge__(self, other):
+ return self._score >= other._score
+
+ def __lt__(self, other):
+ return self._score < other._score
+
+ def __le__(self, other):
+ return self._score <= other._score
+
+ def __ne__(self, other):
+ return self._score != other._score
+
+ def __eq__(self, other):
+ return self._score == other._score
+
+ def __add__(self, other):
+ return self._score + other._score
+
+ def __sub__(self, other):
+ return self._score - other._score
+
+
+
Say no more!