Никол обнови решението на 29.11.2022 16:42 (преди почти 3 години)
+import collections
+
+
+class ChessException(Exception):
Можеш да минеш и само с един pass в тялото на класа, защото този инициализатор реално не прави нищо.
+    def __init__(self, message):
+        self.message = message
+        super().__init__(self.message)
+
+    def __str__(self):
+        return f'{self.message}'
+
+
+class ChessPosition:
+    def __init__(self, FEN_string):
Прието е аргументите към функция да са само с малки букви.
+        self.FEN_string = FEN_string
+        eight_row = self.FEN_string.split('/')[0]
+        first_row = self.FEN_string[::-1]
+        first_row = first_row.split('/')[0]
+        rows = first_row + eight_row
+        pawns = ['p', 'P']
+        for pawn in pawns:
Ако имаш повече от един проблем, kings грешките са с приоритет, така че трябва да се проверят първи.
+            if pawn in rows:
+                raise ChessException("pawns")
+
+        d = collections.defaultdict(int)
+        for c in self.FEN_string:
+            d[c] += 1
+        if d["k"] != 1 or d["K"] != 1:
+            raise ChessException("kings")
+
+    def get_white_score(self):
+        white_pieces = []
+        for _ in self.FEN_string:
Долната черта се използва тогава, когато не искаш да реферираш към тази променлива, а ти реферираш.
+            if _ in ['R', 'N', 'B', 'K', 'Q', 'P']:
+                white_pieces += _
+        return ChessScore(white_pieces)
+
+    def get_black_score(self):
+        black_pieces = []
+        for _ in self.FEN_string:
+            if _ in ['r', 'n', 'b', 'k', 'q', 'p']:
+                black_pieces += _
+        return ChessScore(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()
+
+    def __len__(self):
+        lenn = 0
+        for _ in self.FEN_string:
+            if _ in ['r', 'n', 'b', 'k', 'q', 'p', 'R', 'N', 'B', 'K', 'Q', 'P']:
+                lenn += 1
+        return lenn
+
+    def __str__(self):
+        return str(self.FEN_string)
+
+
+class ChessScore:
+    scores = dict(r=5, R=5, n=3, N=3, b=3, B=3, q=9, Q=9, k=4, K=4, p=1, P=1)
Рядко срещаш начин да дефинираш речник, но е ок.
+
+    def __init__(self, pieces):
+        self.__chess_score = 0
+        for _ in pieces:
+            self.__chess_score += self.scores[_]
+
+    def __int__(self):
+        return self.__chess_score
+
+    def __add__(self, other):
+        return self.__chess_score + other.__chess_score
+
+    def __sub__(self, other):
+        return self.__chess_score - other.__chess_score
+
+    def __gt__(self, other):
+        if self.__chess_score > other.__chess_score:
Просто return self.__chess_score > other.__chess_score
+            return True
+        else:
+            return False
+
+    def __lt__(self, other):
+        if self.__chess_score < other.__chess_score:
+            return True
+        else:
+            return False
+
+    def __ge__(self, other):
+        if self.__chess_score >= other.__chess_score:
+            return True
+        else:
+            return False
+
+    def __le__(self, other):
+        if self.__chess_score <= other.__chess_score:
+            return True
+        else:
+            return False
+
+    def __eq__(self, other):
+        if self.__chess_score == other.__chess_score:
+            return True
+        else:
+            return False
+
+    def __ne__(self, other):
+        if self.__chess_score != other.__chess_score:
+            return True
+        else:
+            return False

Можеш да минеш и само с един
passв тялото на класа, защото този инициализатор реално не прави нищо.Прието е аргументите към функция да са само с малки букви.
Ако имаш повече от един проблем,
kingsгрешките са с приоритет, така че трябва да се проверят първи.Долната черта се използва тогава, когато не искаш да реферираш към тази променлива, а ти реферираш.
Рядко срещаш начин да дефинираш речник, но е ок.
Просто
return self.__chess_score > other.__chess_score