Решение на Шахматни фенове от Кристияна Николова

Обратно към всички решения

Към профила на Кристияна Николова

Резултати

  • 10 точки от тестове
  • 1 бонус точка
  • 11 точки общо
  • 17 успешни тест(а)
  • 0 неуспешни тест(а)

Код

valid_figures = ['r', 'R', 'n', 'N', 'b', 'B', 'q', 'Q', 'k', 'K', 'p', 'P']
figures_scores = {'r': 5, 'n': 3, 'b': 3, 'q': 9, 'k': 4, 'p': 1}
board_columns = {'A': 0, 'B': 1, 'C': 2, 'D': 3, 'E': 4, 'F': 5, 'G': 6, 'H': 7}
class ChessException(Exception):
pass
class ChessScore:
def __init__(self, figures):
self.figures = list(map(str.lower, figures))
self.score()
def score(self):
self.score = 0
for figure in self.figures:
if figure in figures_scores:
self.score += figures_scores[figure]
def __int__(self):
return self.score
def __add__(self, other):
return self.score + other.score
def __sub__(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 __le__(self, other):
return self.score <= other.score
def __lt__(self, other):
return self.score < other.score
def __ge__(self, other):
return self.score >= other.score
def __gt__(self, other):
return self.score > other.score
class ChessPosition:
def __init__(self, fen):
self.fen = fen
self.generate_board()
self.validate_kings()
self.invalid_pawns_check()
self.white_figures = []
self.black_figures = []
def __str__(self):
return self.fen
def generate_board(self):
self.board = []
figures_on_each_row = self.fen.split('/')[::-1] #обръщаме реда, за да започваме от 1 ред
for row in figures_on_each_row:
current_row = []
for figure in row:
if figure in valid_figures:
current_row.append(figure)
elif figure.isnumeric(): #8ца за празен ред
empty_row = []
for _ in range(int(figure)):
empty_row.append(None)
current_row.extend(empty_row)
self.board.append(current_row)
def kings_invalid_positions_check(self, pos_of_white_king, pos_of_black_king):
dif_x = abs(pos_of_white_king[0] - pos_of_black_king[0])
dif_y = abs(pos_of_white_king[1] - pos_of_black_king[1])
return dif_x <= 1 and dif_y <= 1
def validate_kings(self):
pos_of_white_king = pos_of_black_king = (-1, -1)
for index_of_row, current_row in enumerate(self.board):
for index_of_column, current_column in enumerate(current_row):
current_figure = self.board[index_of_row][index_of_column]
if current_figure == 'k': #black king
cur_pos_of_black_king = (index_of_row, index_of_column)
if pos_of_black_king != (-1, -1):
raise ChessException('kings')
else:
pos_of_black_king = cur_pos_of_black_king
elif current_figure == 'K': #white king
cur_pos_of_white_king = (index_of_row, index_of_column)
if pos_of_white_king != (-1, -1):
raise ChessException('kings')
else:
pos_of_white_king = cur_pos_of_white_king
if self.kings_invalid_positions_check(pos_of_white_king, pos_of_black_king) or\
(pos_of_white_king == (-1, -1) or\
pos_of_black_king == (-1, -1)):
raise ChessException('kings')
def invalid_pawns_check(self):
invalid_rows = [0, 7]
for row in invalid_rows:
for index_of_column, column in enumerate(self.board[row]):
if self.board[row][index_of_column] in ['p', 'P']:
raise ChessException('pawns')
def get_white_score(self):

Бих те посъветвал логиката тук да е част от инициализацията, а резултатът да пазиш в атрибут на иснтанцията, за да не циклиш при всяко извикване на метода.

white_figures = []
for index_of_row, row in enumerate(self.board):
for index_of_column, column in enumerate(row):
current_figure = self.board[index_of_row][index_of_column]
if current_figure in ['R', 'N', 'B', 'Q', 'K', 'P']:
white_figures.append(current_figure)
return ChessScore(white_figures)
def get_black_score(self):
black_figures = []
for index_of_row, row in enumerate(self.board):
for index_of_column, column in enumerate(row):
current_figure = self.board[index_of_row][index_of_column]
if current_figure in ['r', 'n', 'b', 'q', 'k', 'p']:
black_figures.append(current_figure)
return ChessScore(black_figures)
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_equal(self):
return self.get_white_score() == self.get_black_score()
def __getitem__(self, board_pos):
row = int(board_pos[1]) - 1
column = board_pos[0]
board_column = board_columns[column]
return self.board[row][board_column]
def __len__(self):

За някои методи, като този например, по-удобно е да циклиш в self.fen, вместо в парснатата си версия, защото е едномерен. И така е ок, разбира се. Просто препоръка.

counter = 0
for index_of_row, row in enumerate(self.board):
for index_of_column, column in enumerate(row):
current_figure = self.board[index_of_row][index_of_column]
if current_figure in valid_figures:
counter += 1
return counter

Лог от изпълнението

.................
----------------------------------------------------------------------
Ran 17 tests in 0.174s

OK

История (2 версии и 4 коментара)

Кристияна обнови решението на 28.11.2022 00:22 (преди почти 2 години)

+valid_figures = ['r', 'R', 'n', 'N', 'b', 'B', 'q', 'Q', 'k', 'K', 'p', 'P']
+figures_scores = {'r': 5, 'n': 3, 'b': 3, 'q': 9, 'k': 4, 'p': 1}
+board_columns = {'A': 0, 'B': 1, 'C':2, 'D':3, 'E':4, 'F':5, 'G':6, 'H':7}
+
+class ChessException(Exception):
+ pass
+
+
+class ChessScore:
+ def __init__(self, figures):
+ self.figures = list(map(str.lower, figures))
+ self.score()
+
+ def score(self):
+ self.score = 0
+ for figure in self.figures:
+ if figure in figures_scores:
+ self.score += figures_scores[figure]
+
+ def __int__(self):
+ return self.score
+
+ def __add__(self, other):
+ return self.score + other.score
+
+ def __sub__(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 __le__(self, other):
+ return self.score <= other.score
+
+ def __lt__(self, other):
+ return self.score < other.score
+
+ def __ge__(self, other):
+ return self.score >= other.score
+
+ def __gt__(self, other):
+ return self.score > other.score
+
+
+class ChessPosition:
+ def __init__(self, fen):
+ self.fen = fen
+ self.generate_board()
+ self.validate_kings()
+ self.invalid_pawns_check()
+ self.white_figures = []
+ self.black_figures = []
+
+ def __str__(self):
+ return self.fen
+
+ def generate_board(self):
+ self.board = []
+ figures_on_each_row = self.fen.split('/')[::-1] #обръщаме реда, за да започваме от 1 ред
+
+ for row in figures_on_each_row:
+ current_row = []
+ for figure in row:
+ if figure in valid_figures:
+ current_row.append(figure)
+ elif figure.isnumeric(): #8ца за празен ред
+ empty_row = []
+ for _ in range(int(figure)):
+ empty_row.append(None)
+ current_row.extend(empty_row)
+ self.board.append(current_row)
+
+ def kings_invalid_positions_check(self, pos_of_white_king, pos_of_black_king):
+ dif_x = abs(pos_of_white_king[0] - pos_of_black_king[0])
+ dif_y = abs(pos_of_white_king[1] - pos_of_black_king[1])
+ return dif_x <= 1 and dif_y <= 1
+
+ def validate_kings(self):
+ pos_of_white_king = pos_of_black_king = (-1, -1)
+
+ for index_of_row, current_row in enumerate(self.board):
+ for index_of_column, current_column in enumerate(current_row):
+ current_figure = self.board[index_of_row][index_of_column]
+ if current_figure == 'k': #black king
+ cur_pos_of_black_king = (index_of_row, index_of_column)
+
+ if pos_of_black_king != (-1, -1):
+ raise ChessException('kings')
+ else:
+ pos_of_black_king = cur_pos_of_black_king
+ elif current_figure == 'K': #white king
+ cur_pos_of_white_king = (index_of_row, index_of_column)
+
+ if pos_of_white_king != (-1, -1):
+ raise ChessException('kings')
+ else:
+ pos_of_white_king = cur_pos_of_white_king
+
+ if self.kings_invalid_positions_check(pos_of_white_king, pos_of_black_king) or\
+ (pos_of_white_king == (-1, -1) or\
+ pos_of_black_king == (-1, -1)):
+ raise ChessException('kings')
+
+ def invalid_pawns_check(self):
+ invalid_rows = [0, 7]
+ for row in invalid_rows:
+ for index_of_column, column in enumerate(self.board[row]):
+ if self.board[row][index_of_column] in ['p', 'P']:
+ raise ChessException('pawns')
+
+ def get_white_score(self):
+ white_figures = []
+ for index_of_row, row in enumerate(self.board):
+ for index_of_column, column in enumerate(row):
+ current_figure = self.board[index_of_row][index_of_column]
+ if current_figure in ['R', 'N', 'B', 'Q', 'K', 'P']:
+ white_figures.append(current_figure)
+ return ChessScore(white_figures)
+
+ def get_black_score(self):
+ black_figures = []
+ for index_of_row, row in enumerate(self.board):
+ for index_of_column, column in enumerate(row):
+ current_figure = self.board[index_of_row][index_of_column]
+ if current_figure in ['r', 'n', 'b', 'q', 'k', 'p']:
+ black_figures.append(current_figure)
+ return ChessScore(black_figures)
+
+ 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_equal(self):
+ return self.get_white_score() == self.get_black_score()
+
+ def __getitem__(self, board_pos):
+ row = int(board_pos[1]) - 1
+ column = board_pos[0]
+ board_column = board_columns[column]
+ return self.board[row][board_column]
+
+ def __len__(self):
+ counter = 0
+ for index_of_row, row in enumerate(self.board):
+ for index_of_column, column in enumerate(row):
+ current_figure = self.board[index_of_row][index_of_column]
+ if current_figure in valid_figures:
+ counter += 1
+ return counter

Кристияна обнови решението на 28.11.2022 00:23 (преди почти 2 години)

valid_figures = ['r', 'R', 'n', 'N', 'b', 'B', 'q', 'Q', 'k', 'K', 'p', 'P']
figures_scores = {'r': 5, 'n': 3, 'b': 3, 'q': 9, 'k': 4, 'p': 1}
-board_columns = {'A': 0, 'B': 1, 'C':2, 'D':3, 'E':4, 'F':5, 'G':6, 'H':7}
+board_columns = {'A': 0, 'B': 1, 'C': 2, 'D': 3, 'E': 4, 'F': 5, 'G': 6, 'H': 7}
class ChessException(Exception):
pass
class ChessScore:
def __init__(self, figures):
self.figures = list(map(str.lower, figures))
self.score()
def score(self):
self.score = 0
for figure in self.figures:
if figure in figures_scores:
self.score += figures_scores[figure]
def __int__(self):
return self.score
def __add__(self, other):
return self.score + other.score
def __sub__(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 __le__(self, other):
return self.score <= other.score
def __lt__(self, other):
return self.score < other.score
def __ge__(self, other):
return self.score >= other.score
def __gt__(self, other):
return self.score > other.score
class ChessPosition:
def __init__(self, fen):
self.fen = fen
self.generate_board()
self.validate_kings()
self.invalid_pawns_check()
self.white_figures = []
self.black_figures = []
def __str__(self):
return self.fen
def generate_board(self):
self.board = []
figures_on_each_row = self.fen.split('/')[::-1] #обръщаме реда, за да започваме от 1 ред
for row in figures_on_each_row:
current_row = []
for figure in row:
if figure in valid_figures:
current_row.append(figure)
elif figure.isnumeric(): #8ца за празен ред
empty_row = []
for _ in range(int(figure)):
empty_row.append(None)
current_row.extend(empty_row)
self.board.append(current_row)
def kings_invalid_positions_check(self, pos_of_white_king, pos_of_black_king):
dif_x = abs(pos_of_white_king[0] - pos_of_black_king[0])
dif_y = abs(pos_of_white_king[1] - pos_of_black_king[1])
return dif_x <= 1 and dif_y <= 1
def validate_kings(self):
pos_of_white_king = pos_of_black_king = (-1, -1)
for index_of_row, current_row in enumerate(self.board):
for index_of_column, current_column in enumerate(current_row):
current_figure = self.board[index_of_row][index_of_column]
if current_figure == 'k': #black king
cur_pos_of_black_king = (index_of_row, index_of_column)
-
if pos_of_black_king != (-1, -1):
raise ChessException('kings')
else:
pos_of_black_king = cur_pos_of_black_king
elif current_figure == 'K': #white king
cur_pos_of_white_king = (index_of_row, index_of_column)
-
if pos_of_white_king != (-1, -1):
raise ChessException('kings')
else:
pos_of_white_king = cur_pos_of_white_king
if self.kings_invalid_positions_check(pos_of_white_king, pos_of_black_king) or\
(pos_of_white_king == (-1, -1) or\
pos_of_black_king == (-1, -1)):
raise ChessException('kings')
def invalid_pawns_check(self):
invalid_rows = [0, 7]
for row in invalid_rows:
for index_of_column, column in enumerate(self.board[row]):
if self.board[row][index_of_column] in ['p', 'P']:
raise ChessException('pawns')
def get_white_score(self):

Бих те посъветвал логиката тук да е част от инициализацията, а резултатът да пазиш в атрибут на иснтанцията, за да не циклиш при всяко извикване на метода.

white_figures = []
for index_of_row, row in enumerate(self.board):
for index_of_column, column in enumerate(row):
current_figure = self.board[index_of_row][index_of_column]
if current_figure in ['R', 'N', 'B', 'Q', 'K', 'P']:
white_figures.append(current_figure)
return ChessScore(white_figures)
def get_black_score(self):
black_figures = []
for index_of_row, row in enumerate(self.board):
for index_of_column, column in enumerate(row):
current_figure = self.board[index_of_row][index_of_column]
if current_figure in ['r', 'n', 'b', 'q', 'k', 'p']:
black_figures.append(current_figure)
return ChessScore(black_figures)
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_equal(self):
return self.get_white_score() == self.get_black_score()
def __getitem__(self, board_pos):
row = int(board_pos[1]) - 1
column = board_pos[0]
board_column = board_columns[column]
return self.board[row][board_column]
def __len__(self):

За някои методи, като този например, по-удобно е да циклиш в self.fen, вместо в парснатата си версия, защото е едномерен. И така е ок, разбира се. Просто препоръка.

counter = 0
for index_of_row, row in enumerate(self.board):
for index_of_column, column in enumerate(row):
current_figure = self.board[index_of_row][index_of_column]
if current_figure in valid_figures:
counter += 1
return counter