Иван обнови решението на 29.11.2022 00:12 (преди почти 2 години)
+class ChessException(Exception):
+ pass
+
+
+class ChessPosition:
+ def __init__(self, fen: str):
Бих извадил парсването и валидацията от инициализатора.
+ self.values = {"p": 1, "k": 4, "q": 9, "b": 3, "n": 3, "r": 5}
+ self.white_figs = []
+ self.black_figs = []
+ black_result = 0
+ white_result = 0
+ black_kings = 0
+ white_kings = 0
+ black_king_x = 0
За координатите бих използва tuple, защото сега имаш дестина променливи и става претрупано.
+ black_king_y = 0
+ white_king_x = 0
+ white_king_y = 0
+ self.fen = fen.split(" ")[0]
+ self.arr = fen.split("/")
+ self.throwPawn = False
+ for i in range(8):
+ for index, letter in enumerate(self.arr[i]):
+ if letter.isupper():
+ black_result = black_result + self.values[letter.lower()]
+ self.black_figs.append(letter)
+ if letter.islower():
+ white_result = white_result + self.values[letter]
+ self.white_figs.append(letter)
+ if letter == 'k':
+ black_kings = black_kings + 1
+ black_king_x = i
+ black_king_y = index
+ if letter == 'K':
+ white_kings = white_kings + 1
+ black_king_x = i
+ black_king_y = index
+ if letter.lower() == 'p' and (i == 0 or i == 7):
+ self.throwPawn = True
+ if black_kings > 1 or white_kings > 1 or black_kings == 0 or white_kings == 0:
+ raise ChessException("kings")
+ if (black_king_x == white_king_x and abs(black_king_y - white_king_y) < 2) or (
+ black_king_y == white_king_y and abs(black_king_x - white_king_x) < 2) or (
+ black_king_x == white_king_x + 1 and black_king_y == white_king_y + 1) or (
+ black_king_x == white_king_x - 1 and black_king_y == white_king_y - 1) or (
+ black_king_x == white_king_x + 1 and black_king_y == white_king_y - 1) or (
+ black_king_x == white_king_x - 1 and black_king_y == white_king_y + 1):
+ raise ChessException("kings")
+ if self.throwPawn:
+ raise ChessException("pawns")
+
+ def get_white_score(self):
+ return ChessScore(self.white_figs)
+
+ def get_black_score(self):
+ return ChessScore(self.black_figs)
+
+ 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_equals(self):
+ return self.get_black_score() == self.get_white_score()
+
+ def __str__(self):
+ return self.fen
+
+ def __len__(self):
+ return len(self.black_figs) + len(self.white_figs)
+
+ def __getitem__(self, item):
+ width = item[0]
+ height = item[1]
+ for i in range(8):
+ for index, letter in enumerate(self.arr[i]):
+ if ((8 - i) == int(height)) and (index == (ord(width) - ord('A'))):
+ if '0' <= letter <= '9':
+ return None
+ else:
+ return letter
+
+
+class ChessScore:
+ def __init__(self, pieces: list):
+ self.values = {"p": 1, "k": 4, "q": 9, "b": 3, "n": 3, "r": 5}
+ self.result = 0
+ for piece in pieces:
+ self.result = self.result + self.values[piece.lower()]
Можеш и с self.result +=
+
+ def __int__(self):
+ return self.result
+
+ def __gt__(self, other):
+ return self.result > other.result
+
+ def __lt__(self, other):
+ return self.result < other.result
+
+ def __ge__(self, other):
+ return self.result >= other.result
+
+ def __le__(self, other):
+ return self.result <= other.result
+
+ def __eq__(self, other):
+ return self.result == other.result
+
+ def __ne__(self, other):
+ return self.result != other.result
+
+ def __add__(self, other):
+ return self.result + other.result
+
+ def __sub__(self, other):
+ return self.result - other.result
Съветвам те да изтестваш с няколко случайни позиции, за да се убедиш, че работи.
Бих извадил парсването и валидацията от инициализатора.
За координатите бих използва tuple, защото сега имаш дестина променливи и става претрупано.
Можеш и с
self.result +=