Цветан обнови решението на 28.11.2022 23:57 (преди почти 2 години)
+empty_spaces = ["1", "2", "3", "4", "5", "6", "7", "8", "9"]
+white_figures = ['R', 'N', 'B', 'Q', 'K', 'P']
+black_figures = ['r', 'n', 'b', 'q', 'k', 'p']
+points_for_figures = { 'R': 5, 'r': 5, 'K': 4, 'k': 4, 'B': 3,'b': 3,
+ 'Q': 9, 'q': 9, 'N': 3, 'n': 3, 'P': 1, 'p': 1}
+board_cord = {'A': 0, 'B': 1, 'C': 2, 'D': 3, 'E': 4, 'F': 5, 'G': 6, 'H': 7}
+
+class ChessException (Exception):
+ pass
+
+
+def make_board(fen):
+ fen = list(map(list, fen))
+
+ for row in fen:
+ for x in row:
+ if x in empty_spaces:
+ for i in range(int(x)):
+ row.insert(row.index(x),"x")
+ row.remove(x)
+ return fen
+
+def check_kings_position(fen):
+ for row in fen:
+ for x in row:
+ if x == "K":
+ king_pos = [fen.index(row), row.index(x)]
+
+ y,x = king_pos
+ places_around_king = [[y - 1,x - 1], [y - 1, x], [y - 1, x + 1], [y, x - 1], [y, x + 1], [y + 1, x - 1], [y + 1, x], [y + 1, x + 1]]
+
+ for pos in places_around_king:
+ y_,x_ = pos
+ if(0<=y_<=7 and 0<=x_<=7):
+ if fen[y_][x_] == "k":
+ raise ChessException("kings")
+
+def amount_of_kings(fen):
+ K_amount = 0
+ k_amount = 0
+
+ for row in fen:
+ for x in row:
+ if x == "K":
+ K_amount += 1
+ elif x == "k":
+ k_amount += 1
+
+ if K_amount < 1 or K_amount > 1 or k_amount < 1 or k_amount > 1:
+ raise ChessException("kings")
+
+def check_pawn_position(fen):
+ if "p" in fen[0] or "P" in fen[0] or "p" in fen[7] or "P" in fen[7]:
+ raise ChessException("pawns")
+
+
+class ChessPosition:
+ def __init__ (self, fen):
+ self.original_fen = fen
+ fen = fen.split("/")
+ fen = list(map(list, fen))
+ fen = make_board(fen)
+
+ amount_of_kings(fen)
+
+ check_kings_position(fen)
+
+ check_pawn_position(fen)
+
+ self.fen = fen
+
+ def print_(self):
+ print(self.fen)
+
+ def get_white_score(self):
+ all_white_fig = []
+ white_fig = [item for row in self.fen for item in row]
+ for fig in white_fig:
+ if fig in white_figures:
+ all_white_fig.append(fig)
+
+ return ChessScore(all_white_fig)
+
+ def get_black_score(self):
+ all_black_fig = []
+ black_fig = [item for row in self.fen for item in row]
+ for fig in black_fig:
+ if fig in black_figures:
+ all_black_fig.append(fig)
+
+ return ChessScore(all_black_fig)
+
+ def white_is_winning(self):
+ white_score = self.get_white_score()
+ black_score = self.get_black_score()
+ return int(white_score) > int(black_score)
+
+ def black_is_winning(self):
+ white_score = self.get_white_score()
+ black_score = self.get_black_score()
+ return int(white_score) < int(black_score)
+
+ def is_equal(self):
+ white_score = self.get_white_score()
+ black_score = self.get_black_score()
+ return int(white_score) == int(black_score)
+
+ def __repr__(self):
+ return self.original_fen
+
+ def __len__ (self):
+ lenght = 0
+ fig = [item for row in self.fen for item in row]
+ for x in fig:
+ if x != 'x':
+ lenght += 1
+
+ return lenght
+
+ def __getitem__ (self, position):
+ x,y = position[0],position[1]
+ y = int(y)
+ if self.fen[8 - y][board_cord[x]] != 'x':
+ return self.fen[8 - y][board_cord[x]]
+ else:
+ return None
+
+
+class ChessScore:
+ def __init__(self,figures):
+ points = 0
+ for x in figures:
+ x.lower()
+ for fig in figures:
+ points += points_for_figures[fig]
+ self.points = points
+
+ def __int__(self):
+ return self.points
+
+ def __lt__(self, other):
+ return self.points < other.points
+
+ def __eq__(self, other):
+ return self.points == other.points
+
+ def __le__(self, other):
+ if self.points <= other.points:
+ return True
+
+ def __ne__(self, other):
+ return self.points != other.points
+
+ def __gt__(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
+
+
Интервалът преди скобата е излишен.
Добре е това да бъде в класа, който го използва (както и следващите подобни функции).
Имената тук са доста сходни и трудно се следи логиката:
all_white_fig
white_fig
white_figures