Лиляна обнови решението на 29.11.2022 10:07 (преди почти 2 години)
+Scores = {'r': 5,
+ 'n': 3,
+ 'b': 3,
+ 'k': 4,
+ 'q': 9,
+ 'p': 1}
+
+
+class ChessException(Exception):
+ def __init__(self, message='Chess Exception'):
+ self.message = message
+ super().__init__(self.message)
+
+
+class ChessScore:
+ def __init__(self, figures):
+ for element in figures:
+ if element not in ('r', 'n', 'b', 'k', 'q', 'p'):
+ raise ChessException('Invalid chess figure')
+ else:
+ self.figures = figures
+
+ def __int__(self):
+ sum_scores = 0
+ for key in self.figures:
+ if key in Scores:
+ sum_scores += Scores.get(key)
+ return sum_scores
+
+ def __eq__(self, other):
+ return int(self) == int(other)
+
+ def __le__(self, other):
+ return int(self) <= int(other)
+
+ def __lt__(self, other):
+ return int(self) < int(other)
+
+ def __gt__(self, other):
+ return int(self) > int(other)
+
+ def __ne__(self, other):
+ return not self == other
+
+ def __add__(self, other):
+ return int(self)+int(other)
+
+ def __sub__(self, other):
+ return int(self)-int(other)
+
+
+class ChessPosition:
+ def __init__(self, definition):
+ FEN = definition.split("/")
+ self.matrix = [{'A': ' ', 'B': ' ', 'C': ' ', 'D': ' ',
+ 'E': ' ', 'F': ' ', 'G': ' ', 'H': ' '} for k in range(8)]
+ white = []
+ black = []
+ positions = [*definition]
+
+ for i, s in enumerate(FEN):
+ row = [*s]
+ k = 0
+ for figure in row:
+ low = figure.lower()
+ if low in ('r', 'n', 'b', 'k', 'q', 'p'):
+ self.matrix[7-i][chr(ord('A') + k)] = figure
+ k = k+1
+ elif low in ('1', '2', '3', '4', '5', '6', '7', '8'):
+ k = k + int(low)
+
+ if definition.count('K') != 1 or definition.count('k') != 1:
+ raise ChessException('kings')
+
+ for i, s in enumerate(FEN):
+ row = [*s]
+ k = 0
+ if i == 0 or i == 7:
+ if 'p' in row or 'P' in row:
+ raise ChessException('pawns')
+
+ for pos in positions:
+ if pos in ('r', 'n', 'b', 'k', 'q', 'p'):
+ black.append(pos)
+ elif pos in ('R', 'N', 'B', 'K', 'Q', 'P'):
+ white.append(pos)
+
+ lower_white = [b.lower() for b in white]
+ self.black_figures = ChessScore(black)
+ self.white_figures = ChessScore(lower_white)
+ self.fen = definition
+
+ def get_white_score(self):
+ return self.white_figures
+
+ def get_black_score(self):
+ return self.black_figures
+
+ def white_is_winning(self):
+ return int(self.white_figures) > int(self.black_figures)
+
+ def black_is_winning(self):
+ return int(self.white_figures) < int(self.black_figures)
+
+ def is_equal(self):
+ return int(self.white_figures) == int(self.black_figures)
+
+ def __str__(self):
+ return self.fen
+
+ def __len__(self):
+ size1 = 0
+ size2 = 0
+ for _ in self.white_figures.figures:
+ size1 = size1+1
+ for _ in self.black_figures.figures:
+ size2 = size2+1
+ return size1+size2
+
+ def __getitem__(self, key):
+ if len(key) != 2:
+ return None
+ sp = [*key]
+ word = sp[0]
+ num = int(sp[1])-1
+ result = self.matrix[num][word]
+ if result == ' ':
+ return None
+ return result
Или използвай само малки букви, или само главни, за да покажеш, че е константа. Този формат на имената е запазен за класове.
Можеш да минеш и само с един
pass
в тялото на класа, защото този инициализатор реално не прави нищо.Можеш директно
Scores.get(key, 0)
, за да спестиш проверката дали го има като ключ, или не.Можеш и
low in 'rnbkqp'
Липсват интервали около
+
.Нямаш нужда от две променливи.