Николета обнови решението на 28.11.2022 14:14 (преди почти 2 години)
+class ChessException(Exception):
+ pass
+
+
+class ChessPosition:
+
+ @staticmethod
+ def find_count_letters_before_letter(letter, s):
+ i = 0
+ count = 0
+ while s[i] != letter:
+ if s[i].isdigit():
+ count += int(s[i]) - 1
+ i += 1
+ return count
+
+ def __init__(self, fen):
+ self.rows = fen.split("/")
+ self.fen = fen
+ buf = {}
+ for i, row in enumerate(self.rows):
+ if "K" in row.upper():
+ buf[i] = self.find_count_letters_before_letter("K", row.upper())
+
+ for ind, row in enumerate(self.rows):
+ if "k" in row and "K" in row and abs(row.index("k") - row.index("K")) == 1:
+ raise ChessException("kings")
+ elif "K" in row.upper() and ind + 1 < len(self.rows) and "K" in self.rows[ind + 1].upper():
+ if abs((row.upper().index("K") + buf[ind]) - (self.rows[ind + 1].upper().index("K") + buf[ind+1])) in (0, 1):
+ raise ChessException("kings")
+
+ if self.fen.count("k") != 1 or self.fen.count("K") != 1:
+ raise ChessException("kings")
+ if any(x in ("p", "P") for x in (self.rows[7] + self.rows[0])):
+ raise ChessException("pawns")
+
+ def get_white_score(self):
+ lower_chars = ''.join(char for char in self.fen if char.islower() and char.isalpha())
+ letters = [x for x in lower_chars]
+ return ChessScore(letters)
+
+ def get_black_score(self):
+ upper_chars = ''.join(char for char in self.fen if char.isupper() and char.isalpha())
+ letters = [x for x in upper_chars]
+ return ChessScore(letters)
+
+ 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_black_score() == self.get_white_score()
+
+ def __len__(self):
+ count = 0
+ for character in self.fen:
+ if character.isalpha():
+ count = count + 1
+ return count
+
+ def __print__(self):
+ print(self.fen)
+
+ def __getitem__(self, index):
+ col = ord(index[0]) - ord("A")
+ row = self.rows[8-int(index[1])]
+ j = 0
+ while col > 0 and j < len(row):
+ if row[j].isdigit():
+ col -= int(row[j])
+ else:
+ col -= 1
+ j += 1
+
+ if row[j-1].isaplha():
+ return row[j-1]
+ return None
+
+
+
+points_for_figures = {"r":5, "n":3, "b":3, "q":9, "k":4, "p":1}
+
+
+class ChessScore:
+
+ def __init__(self, figures):
+ self.figures = figures
+ self.points = 0
+ for figure in self.figures:
+ self.points += points_for_figures[figure.lower()]
+
+ def __int__(self):
+ return self.points
+
+ def __eq__(self, other):
+ return self.points == other.points
+
+ def __le__(self, other):
+ return self.points <= other.points
+
+ def __lt__(self, other):
+ return self.points < other.points
+
+ def __ne__(self, other):
+ return self.points != other.points
+
+ def __gt__(self, other):
+ return self.points > other.points
+
+ def __ge__(self, other):
+ return self.points >= other.points
+
+ def __add__(self, other):
+ return self.points + other.points
+
+ def __sub__(self, other):
+ return self.points - other.points