Решение на Шахматни фенове от Веселина Велкова

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

Към профила на Веселина Велкова

Резултати

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

Код

class ChessException(Exception):
"""Exception raised by invalid input for the chess game."""
def __init__(self, message):
self._message = message
super().__init__(self._message)
class ChessPosition:
def __init__(self, fen):
chess_table = [[], [], [], [], [], [], [], []]
count_lines = 0
for character in fen:
if character.isdigit():
for _ in range(int(character)):
chess_table[count_lines].append(None)
elif character == '/':
count_lines += 1
continue
else:
chess_table[count_lines].append(character)
for rows in chess_table:
if 'k' in rows:
row_black_king = chess_table.index(rows)
position_black_king = rows.index('k')
if 'K' in rows:
row_white_king = chess_table.index(rows)
position_white_king = rows.index('K')
white_king_count = 0
black_king_count = 0
for char in fen:
if char == 'k':
black_king_count += 1
elif char == 'K':
white_king_count += 1
if white_king_count != 1 or black_king_count != 1:
raise ChessException('kings')
elif (row_white_king == row_black_king and (position_white_king == (position_black_king + 1) or
position_white_king == (position_black_king - 1))) or (
row_white_king == (row_black_king - 1) or
row_white_king == (row_black_king + 1)) and (position_black_king - 1) <= position_white_king <= (
position_black_king + 1):
raise ChessException('kings')
elif 'p' in chess_table[0] or 'P' in chess_table[0] or 'p' in chess_table[7] or 'P' in chess_table[7]:
raise ChessException('pawns')
else:
self.table = chess_table
self.fen = fen
def __repr__(self):
return self.fen
def __len__(self):
count = 0
for character in self.fen:
if character.isalpha():
count += 1
return count
def get_white_score(self):
white_list = []
for character in self.fen:
if character in {'R', 'N', 'B', 'K', 'Q', 'P'}:
white_list.append(character)
white_score = ChessScore(white_list)
return white_score
def get_black_score(self):
black_list = []
for character in self.fen:
if character in {'r', 'n', 'b', 'k', 'q', 'p'}:
black_list.append(character)
black_score = ChessScore(black_list)
return black_score
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, index):
columns = {'A': 0, 'B': 1, 'C': 2, 'D': 3, 'E': 4, 'F': 5, 'G': 6, 'H': 7}
row = int(index[1])
column = index[0]
return self.table[8 - row][columns.get(column)]
class ChessScore:
def __init__(self, list_of_figures):
self.score = 0
for character in list_of_figures:
character = character.lower()
match character:
case 'r':
self.score += 5
case 'n':
self.score += 3
case 'b':
self.score += 3
case 'k':
self.score += 4
case 'q':
self.score += 9
case 'p':
self.score += 1
def __int__(self):
return self.score
def __lt__(self, other):
return self.score < other.score
def __le__(self, other):
return self.score <= other.score
def __eq__(self, other):
return self.score == other.score
def __gt__(self, other):
return self.score > other.score
def __ne__(self, other):
return self.score != other.score
def __add__(self, other):
return self.score + other.score
def __sub__(self, other):
return self.score - other.score

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

.................
----------------------------------------------------------------------
Ran 17 tests in 0.159s

OK

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

Веселина обнови решението на 29.11.2022 02:44 (преди почти 2 години)

+class ChessException(Exception):
+ """Exception raised by invalid input for the chess game."""
+
+ def __init__(self, message):
+ self._message = message
+ super().__init__(self._message)
+
+
+class ChessPosition:
+ def __init__(self, fen):
+ chess_table = [[], [], [], [], [], [], [], []]
+ count_lines = 0
+ for character in fen:
+ if character.isdigit():
+ for _ in range(int(character)):
+ chess_table[count_lines].append(None)
+ elif character == '/':
+ count_lines += 1
+ continue
+ else:
+ chess_table[count_lines].append(character)
+ for rows in chess_table:
+ if 'k' in rows:
+ row_black_king = chess_table.index(rows)
+ position_black_king = rows.index('k')
+ if 'K' in rows:
+ row_white_king = chess_table.index(rows)
+ position_white_king = rows.index('K')
+ white_king_count = 0
+ black_king_count = 0
+ for char in fen:
+ if char == 'k':
+ black_king_count += 1
+ elif char == 'K':
+ white_king_count += 1
+ if white_king_count != 1 or black_king_count != 1:
+ raise ChessException('kings')
+ elif (row_white_king == row_black_king and (position_white_king == (position_black_king + 1) or
+ position_white_king == (position_black_king - 1))) or (
+ row_white_king == (row_black_king - 1) or
+ row_white_king == (row_black_king + 1)) and (position_black_king - 1) <= position_white_king <= (
+ position_black_king + 1):
+ raise ChessException('kings')
+ elif 'p' in chess_table[0] or 'P' in chess_table[0] or 'p' in chess_table[7] or 'P' in chess_table[7]:
+ raise ChessException('pawns')
+ else:
+ self.table = chess_table
+ self.fen = fen
+
+ def __repr__(self):
+ return self.fen
+
+ def __len__(self):
+ count = 0
+ for character in self.fen:
+ if character.isalpha():
+ count += 1
+ return count
+
+ def get_white_score(self):
+ white_list = []
+ for character in self.fen:
+ if character in {'R', 'N', 'B', 'K', 'Q', 'P'}:
+ white_list.append(character)
+ white_score = ChessScore(white_list)
+ return white_score
+
+ def get_black_score(self):
+ black_list = []
+ for character in self.fen:
+ if character in {'r', 'n', 'b', 'k', 'q', 'p'}:
+ black_list.append(character)
+ black_score = ChessScore(black_list)
+ return black_score
+
+ 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, index):
+ columns = {'A': 0, 'B': 1, 'C': 2, 'D': 3, 'E': 4, 'F': 5, 'G': 6, 'H': 7}
+ row = int(index[1])
+ column = index[0]
+ return self.table[8 - row][columns.get(column)]
+
+
+class ChessScore:
+ def __init__(self, list_of_figures):
+ self.score = 0
+ for character in list_of_figures:
+ character = character.lower()
+ match character:
+ case 'r':
+ self.score += 5
+ case 'n':
+ self.score += 3
+ case 'b':
+ self.score += 3
+ case 'k':
+ self.score += 4
+ case 'q':
+ self.score += 9
+ case 'p':
+ self.score += 1
+
+ def __int__(self):
+ return self.score
+
+ def __lt__(self, other):
+ return self.score < other.score
+
+ def __le__(self, other):
+ return self.score <= other.score
+
+ def __eq__(self, other):
+ return self.score == other.score
+
+ def __gt__(self, other):
+ return self.score > other.score
+
+ def __ne__(self, other):
+ return self.score != other.score
+
+ def __add__(self, other):
+ return self.score + other.score
+
+ def __sub__(self, other):
+ return self.score - other.score
+