Румен обнови решението на 28.11.2022 23:57 (преди почти 2 години)
+class ChessException(Exception):
+ pass
+
+
+def check_num_of_kings(figures):
Добре е тази функция да е в класа, който я използва.
+ small_king = 0
+ big_king = 0
+ for row in figures:
+ for figure in row:
+ if figure == 'k':
+ small_king += 1
+ elif figure == 'K':
+ big_king += 1
+ if small_king != 1 or big_king != 1:
+ raise ChessException('kings')
+
+
+def check_kings_pos(figures):
+
+ for row in figures:
+ for figure in row:
+ if figure == 'k':
+ small_king = [row.index(figure) + 1, figures.index(row) + 1]
+
+ x = small_king[0]
+ y = small_king[1]
+ possible_palces = [[x + 1, y], [x + 1, y + 1], [x + 1, y - 1],
+ [x - 1, y], [x - 1, y + 1], [x - 1, y + 1],
+ [x, y + 1], [x, y - 1]]
+
+ for pos in possible_palces:
+ if x >= 1 and x <= 8 and y >= 1 and y <= 8 and figures[y][x] == 'K':
+ raise ChessException("kings")
+
+def check_pawn(figures):
+ if 'p' in figures[0] or 'p' in figures[7] or 'P' in figures[0] or 'P' in figures[7]:
+ raise ChessException("pawns")
+
+def convert_to_readable(figures):
+ readable = []
+ for row in figures:
+ new_row = []
+ for figure in row:
+ if figure in ['1', '2', '3', '4', '5', '6', '7', '8', '9']:
Може да минеш и само със стринг - if figure in '12345678'
.
Забележи, че не смъ сложил 9 - това е невалиден инпут. Но ако имаше и девет, дори можеше просто figure.isdigit()
+ for i in range(int(figure)):
+ new_row.append('#')
+ else:
+ new_row.append(figure)
+ readable.append(new_row)
+
+ return readable
+
+
+class ChessPosition:
+
+ def __init__(self, fen):
+
+ self.fen_untouched = fen
+ fen_list = fen.split('/')
+ fen_readable = convert_to_readable(fen_list)
+
+ check_num_of_kings(fen_list)
+ check_kings_pos(fen_readable)
+ check_pawn(fen_readable)
+
+ self.board = fen_readable
+
+ def get_white_score(self):
+ white_fig = ['R','N','B','Q','K','P']
Липсват интервали след запетаите.
+ our_white_fig = []
+ for row in self.board:
+ for figure in row:
+ if figure in white_fig:
+ our_white_fig.append(figure)
+ return ChessScore(our_white_fig)
+
+ def get_black_score(self):
+ black_fig = ['r','n','b','q','k','p']
+ our_black_fig = []
+ for row in self.board:
+ for figure in row:
+ if figure in black_fig:
+ our_black_fig.append(figure)
+ return ChessScore(our_black_fig)
+
+ def white_is_winning(self):
+ white = self.get_white_score()
+ black = self.get_black_score()
+
+ return white > black
+
+ def black_is_winning(self):
+ white = self.get_white_score()
+ black = self.get_black_score()
+
+ return white < black
+
+ def is_equal(self):
+ white = self.get_white_score()
+ black = self.get_black_score()
+
+ return white == black
+
+ def repr(self):
+ return self.fen_untouched
+
+ def __len__ (self):
+ lenght = 0
+ for row in self.board:
+ for figure in row:
+ if figure != '#':
+ lenght += 1
+
+ return lenght
+
+ def __getitem__(self,str):
+
+ row_index = 8 - int(str[1])
+ cow_index = str[0]
+
+ cows_keys = {'A': 0,'B':1,'C':2,'D':3,"E":4,
Липсват интервали след повечето :
+ 'F':5 ,'G':6, 'H':7}
+
+ figure = self.board[row_index][cows_keys[cow_index]]
+ return figure if figure != '#' else None
+
+
+class ChessScore:
+ def __init__(self, figures):
+ figs = [x.lower() for x in figures]
+ values_of_figures = {'r': 5, 'n': 3, 'b': 3, 'q': 9,
+ 'k': 4, 'p': 1}
+ self.score = sum(values_of_figures [f] for f in figs)
Излишен интервал преди [
+
+ def __add__(self, other):
+ return self.score + other.score
+
+ def __sub__(self, other):
+ return self.score - other.score
+
+ def __lt__(self, other):
+ return self.score < other.score
+
+ def __gt__(self, other):
+ return self.score > other.score
+
+ def __le__(self, other):
+ return self.score <= other.score
+
+ def __ge__(self, other):
+ return self.score >= other.score
+
+ def __eq__(self, other):
+ return self.score == other.score
+
+ def __ne__(self, other):
+ return self.score != other.score
+
+ def __int__(self):
+ return self.score
Може да минеш и само със стринг -
if figure in '12345678'
.Забележи, че не смъ сложил 9 - това е невалиден инпут. Но ако имаше и девет, дори можеше просто
figure.isdigit()