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

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

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

Резултати

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

Код

FIGURE_TO_POINTS = {'r': 5, 'n': 3, 'b': 3, 'q': 9, 'k': 4, 'p': 1}
class ChessException(Exception):
pass
class ChessBoard:
def __init__(self, fen):
for number in range(1, 10):
fen = fen.replace(str(number), ' '.ljust(number))
fen = fen.split('/')
for index, string in enumerate(fen):
fen[index] = [*string]
self.board = fen
def get_board(self):
return self.board
def kings_next_to_each_other(self):
k_x, k_y, K_x, K_y = self.get_kings_positions()
return not (abs(k_x - K_x) > 1 or abs(k_y - K_y) > 1)
def get_kings_positions(self):
k_x, k_y, K_x, K_y = -1, -1, -1, -1
for line_index, line in enumerate(self.board):
for element_index, element in enumerate(line):
if element == 'k':
k_x, k_y = line_index, element_index
if element == 'K':
K_x, K_y = line_index, element_index
if k_x != -1 and k_y != -1 and K_x != -1 and K_y != -1:
return k_x, k_y, K_x, K_y
def has_illegal_pawns(self):
return 'p' in map(str.lower, self.board[0] + self.board[7])
def count_figures(self):
counter = 0
for line in self.board:
for figure in line:
if figure != ' ':
counter += 1
return counter
def get_figure_at(self, position):
line = 8 - (ord(position[1]) - ord('0'))
figure = ord(position[0]) - ord('A')
if self.board[line][figure] == ' ':
return None
else:
return self.board[line][figure]
class ChessPosition:
def __init__(self, fen):
self.fen = fen
self.board = ChessBoard(fen)
if self.board.kings_next_to_each_other():
raise ChessException("kings")
if fen.count('K') != 1 or fen.count('k') != 1:
raise ChessException("kings")
if self.board.has_illegal_pawns():
raise ChessException("pawns")
def __str__(self):
return self.fen
def __len__(self):
return self.board.count_figures()
def __getitem__(self, position):
return self.board.get_figure_at(position)
def _get_score_for(self, wanted_figures):
all_figures, figures = [], []
for line in self.board.get_board():
all_figures += line
for figure in all_figures:
if figure in wanted_figures:
figures += figure
for figure in all_figures:
figure = figure.lower()
return ChessScore(all_figures)
def get_white_score(self):
return self._get_score_for(['R', 'N', 'B', 'Q', 'K', 'P'])
def get_black_score(self):
return self._get_score_for(['r', 'n', 'b', 'q', 'k', 'p'])
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()
class ChessScore:
def __init__(self, figures):
self.score = 0
for figure in figures:
self.score += FIGURE_TO_POINTS[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 __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

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

..EEE.EE....E....
======================================================================
ERROR: test_black_is_winning (test.TestChessPosition)
Test black_is_winning.
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/storage/deedee/data/rails/pyfmi-2022/releases/20221115154139/lib/language/python/runner.py", line 67, in thread
    raise result
KeyError: ' '

======================================================================
ERROR: test_get_black_score (test.TestChessPosition)
Test get_black_score.
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/storage/deedee/data/rails/pyfmi-2022/releases/20221115154139/lib/language/python/runner.py", line 67, in thread
    raise result
KeyError: ' '

======================================================================
ERROR: test_get_white_score (test.TestChessPosition)
Test get_white_score.
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/storage/deedee/data/rails/pyfmi-2022/releases/20221115154139/lib/language/python/runner.py", line 67, in thread
    raise result
KeyError: ' '

======================================================================
ERROR: test_is_equal (test.TestChessPosition)
Test is_equal.
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/storage/deedee/data/rails/pyfmi-2022/releases/20221115154139/lib/language/python/runner.py", line 67, in thread
    raise result
KeyError: ' '

======================================================================
ERROR: test_king_count (test.TestChessPosition)
Test for missing or multiple kings.
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/storage/deedee/data/rails/pyfmi-2022/releases/20221115154139/lib/language/python/runner.py", line 67, in thread
    raise result
TypeError: cannot unpack non-iterable NoneType object

======================================================================
ERROR: test_white_is_winning (test.TestChessPosition)
Test white_is_winning.
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/storage/deedee/data/rails/pyfmi-2022/releases/20221115154139/lib/language/python/runner.py", line 67, in thread
    raise result
KeyError: ' '

----------------------------------------------------------------------
Ran 17 tests in 0.163s

FAILED (errors=6)

История (7 версии и 6 коментара)

Виктор обнови решението на 27.11.2022 20:28 (преди почти 2 години)

+
+
+class ChessException(Exception):
+ pass
+
+
+class ChessBoard:
+ def __init__(self, fen):
+ for number in range(1, 10):
+ fen = fen.replace(chr(number + ord('0')), ' '.ljust(number))
+
+ fen = fen.split('/')
+
+ for index, string in enumerate(fen):
+ fen[index] = [*string]
+
+ self.board = fen
+
+ def kings_next_to_each_other(self):
+ k_x, k_y, K_x, K_y = get_kings_positions
+
+ if k_x == -1 or K_x == -1:
+ return True
+
+ return abs(k_x - K_x) > 1 or abs(k_y - K_y) > 1
+
+ def get_kings_positions(self):
+ position_k, position_K = (-1, -1), (-1, -1)
+
+ for line_index, line in enumerate(self.board):
+ for element_index, element in enumerate(line):
+ if element == 'k':
+ position_k = (line_index, element_index)
+
+ if element == 'K':
+ position_K = (line_index, element_index)
+
+ if position_k != (-1, -1) and position_K != (-1, -1):
+ return position_k, position_K
+
+ return position_k, position_K
+
+ def has_illegal_pawns(self):
+ return 'p' in self.board[0] or 'p' in self.board[7] or 'P' in self.board[0] or 'P' in self.board[7]
+
+ def count_figures(self):
+ counter = 0
+
+ for line in self.board:
+ for figure in line:
+ if figure != ' ':
+ counter += 1
+
+ return counter
+
+ def get_figure_at(self, position):
+ line = 8 - (ord(position[1]) - ord('0'))
+ figure = ord(position[0]) - ord('A')
+
+ if self.board[line][figure] == ' ':
+ return None
+ else:
+ return self.board[line][figure]
+
+
+class ChessPosition:
+ def __init__(self, fen):
+ self.fen = fen
+ self.board = ChessBoard(fen)
+
+ if self.board.kings_next_to_each_other():
+ raise ChessException(kings)
+
+ if fen.count('K') != 1 or fen.count('k') != 1:
+ raise ChessException("kings")
+
+ if self.board.has_illegal_pawns():
+ raise ChessException("pawns")
+
+ def __str__(self):
+ return self.fen
+
+ def __len__(self):
+ return self.board.count_figures()
+
+ def __getitem__(self, position):
+ return self.board.get_figure_at(position)
+
+ def _get_score_without(self, ignored_figures):
+ all_figures = []
+
+ for line in self.board:
+ all_figures += line
+
+ all_figures = list(filter((' ').__ne__, all_figures))
+
+ for figure in ignored_figures:
+ all_figures = list(filter((figure).__ne__, all_figures))
+
+ return ChessScore(all_figures)
+
+ def get_white_score(self):
+ return self._get_score_without(['r', 'n', 'b', 'q', 'k', 'p'])
+
+ def get_black_score(self):
+ return self._get_score_without(['R', 'N', 'B', 'Q', 'K', 'P'])
+
+ 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()
+
+
+class ChessScore:
+ def __init__(self, figures):
+ self.score = 0
+
+ for figure in figures:
+ figure = figure.lowercase()
+
+ match figure:
+ case 'r':
+ self.score += 5
+ case 'n':
+ self.score += 3
+ case 'b':
+ self.score += 3
+ case 'q':
+ self.score += 9
+ case 'k':
+ self.score += 4
+ case 'p':
+ self.score += 1
+
+ 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 __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

Виктор обнови решението на 27.11.2022 20:36 (преди почти 2 години)

class ChessException(Exception):
pass
class ChessBoard:
def __init__(self, fen):
for number in range(1, 10):
fen = fen.replace(chr(number + ord('0')), ' '.ljust(number))
fen = fen.split('/')
for index, string in enumerate(fen):
fen[index] = [*string]
self.board = fen
def kings_next_to_each_other(self):
- k_x, k_y, K_x, K_y = get_kings_positions
+ k_x, k_y, K_x, K_y = self.get_kings_positions()
if k_x == -1 or K_x == -1:
return True
- return abs(k_x - K_x) > 1 or abs(k_y - K_y) > 1
+ return not (abs(k_x - K_x) > 1 or abs(k_y - K_y) > 1)
def get_kings_positions(self):
- position_k, position_K = (-1, -1), (-1, -1)
+ k_x, k_y, K_x, K_y = -1, -1, -1, -1
for line_index, line in enumerate(self.board):
for element_index, element in enumerate(line):
if element == 'k':
- position_k = (line_index, element_index)
+ k_x, k_y = line_index, element_index
if element == 'K':
- position_K = (line_index, element_index)
+ K_x, K_y = line_index, element_index
- if position_k != (-1, -1) and position_K != (-1, -1):
- return position_k, position_K
+ if k_x != -1 and k_y != -1 and K_x != -1 and K_y != -1:
+ return k_x, k_y, K_x, K_y
- return position_k, position_K
+ return k_x, k_y, K_x, K_y
def has_illegal_pawns(self):
return 'p' in self.board[0] or 'p' in self.board[7] or 'P' in self.board[0] or 'P' in self.board[7]
def count_figures(self):
counter = 0
for line in self.board:
for figure in line:
if figure != ' ':
counter += 1
return counter
def get_figure_at(self, position):
line = 8 - (ord(position[1]) - ord('0'))
figure = ord(position[0]) - ord('A')
if self.board[line][figure] == ' ':
return None
else:
return self.board[line][figure]
class ChessPosition:
def __init__(self, fen):
self.fen = fen
self.board = ChessBoard(fen)
if self.board.kings_next_to_each_other():
- raise ChessException(kings)
+ raise ChessException("kings")
if fen.count('K') != 1 or fen.count('k') != 1:
raise ChessException("kings")
if self.board.has_illegal_pawns():
raise ChessException("pawns")
def __str__(self):
return self.fen
def __len__(self):
return self.board.count_figures()
def __getitem__(self, position):
return self.board.get_figure_at(position)
def _get_score_without(self, ignored_figures):
all_figures = []
for line in self.board:
all_figures += line
all_figures = list(filter((' ').__ne__, all_figures))
for figure in ignored_figures:
all_figures = list(filter((figure).__ne__, all_figures))
return ChessScore(all_figures)
def get_white_score(self):
return self._get_score_without(['r', 'n', 'b', 'q', 'k', 'p'])
def get_black_score(self):
return self._get_score_without(['R', 'N', 'B', 'Q', 'K', 'P'])
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()
class ChessScore:
def __init__(self, figures):
self.score = 0
for figure in figures:
figure = figure.lowercase()
match figure:
case 'r':
self.score += 5
case 'n':
self.score += 3
case 'b':
self.score += 3
case 'q':
self.score += 9
case 'k':
self.score += 4
case 'p':
self.score += 1
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 __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

Виктор обнови решението на 27.11.2022 20:39 (преди почти 2 години)

class ChessException(Exception):
pass
class ChessBoard:
def __init__(self, fen):
for number in range(1, 10):
fen = fen.replace(chr(number + ord('0')), ' '.ljust(number))
fen = fen.split('/')
for index, string in enumerate(fen):
fen[index] = [*string]
self.board = fen
def kings_next_to_each_other(self):
k_x, k_y, K_x, K_y = self.get_kings_positions()
if k_x == -1 or K_x == -1:
return True
return not (abs(k_x - K_x) > 1 or abs(k_y - K_y) > 1)
def get_kings_positions(self):
k_x, k_y, K_x, K_y = -1, -1, -1, -1
for line_index, line in enumerate(self.board):
for element_index, element in enumerate(line):
if element == 'k':
k_x, k_y = line_index, element_index
if element == 'K':
K_x, K_y = line_index, element_index
if k_x != -1 and k_y != -1 and K_x != -1 and K_y != -1:
return k_x, k_y, K_x, K_y
return k_x, k_y, K_x, K_y
def has_illegal_pawns(self):
return 'p' in self.board[0] or 'p' in self.board[7] or 'P' in self.board[0] or 'P' in self.board[7]
def count_figures(self):
counter = 0
for line in self.board:
for figure in line:
if figure != ' ':
counter += 1
return counter
def get_figure_at(self, position):
line = 8 - (ord(position[1]) - ord('0'))
figure = ord(position[0]) - ord('A')
if self.board[line][figure] == ' ':
return None
else:
return self.board[line][figure]
class ChessPosition:
def __init__(self, fen):
self.fen = fen
self.board = ChessBoard(fen)
if self.board.kings_next_to_each_other():
raise ChessException("kings")
if fen.count('K') != 1 or fen.count('k') != 1:
raise ChessException("kings")
if self.board.has_illegal_pawns():
raise ChessException("pawns")
def __str__(self):
return self.fen
def __len__(self):
return self.board.count_figures()
def __getitem__(self, position):
return self.board.get_figure_at(position)
def _get_score_without(self, ignored_figures):
all_figures = []
for line in self.board:
all_figures += line
all_figures = list(filter((' ').__ne__, all_figures))
for figure in ignored_figures:
all_figures = list(filter((figure).__ne__, all_figures))
return ChessScore(all_figures)
def get_white_score(self):
return self._get_score_without(['r', 'n', 'b', 'q', 'k', 'p'])
def get_black_score(self):
return self._get_score_without(['R', 'N', 'B', 'Q', 'K', 'P'])
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()
class ChessScore:
def __init__(self, figures):
self.score = 0
for figure in figures:
figure = figure.lowercase()
match figure:
case 'r':
self.score += 5
case 'n':
self.score += 3
case 'b':
self.score += 3
case 'q':
self.score += 9
case 'k':
self.score += 4
case 'p':
self.score += 1
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 __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
+ return self.score != other.score

Виктор обнови решението на 27.11.2022 20:47 (преди почти 2 години)

class ChessException(Exception):
pass
class ChessBoard:
def __init__(self, fen):
for number in range(1, 10):
fen = fen.replace(chr(number + ord('0')), ' '.ljust(number))
fen = fen.split('/')
for index, string in enumerate(fen):
fen[index] = [*string]
self.board = fen
def kings_next_to_each_other(self):
k_x, k_y, K_x, K_y = self.get_kings_positions()
if k_x == -1 or K_x == -1:
return True
return not (abs(k_x - K_x) > 1 or abs(k_y - K_y) > 1)
def get_kings_positions(self):
k_x, k_y, K_x, K_y = -1, -1, -1, -1
for line_index, line in enumerate(self.board):
for element_index, element in enumerate(line):
if element == 'k':
k_x, k_y = line_index, element_index
if element == 'K':
K_x, K_y = line_index, element_index
if k_x != -1 and k_y != -1 and K_x != -1 and K_y != -1:
return k_x, k_y, K_x, K_y
return k_x, k_y, K_x, K_y
def has_illegal_pawns(self):
return 'p' in self.board[0] or 'p' in self.board[7] or 'P' in self.board[0] or 'P' in self.board[7]
def count_figures(self):
counter = 0
for line in self.board:
for figure in line:
if figure != ' ':
counter += 1
return counter
def get_figure_at(self, position):
line = 8 - (ord(position[1]) - ord('0'))
figure = ord(position[0]) - ord('A')
if self.board[line][figure] == ' ':
return None
else:
return self.board[line][figure]
class ChessPosition:
def __init__(self, fen):
self.fen = fen
self.board = ChessBoard(fen)
if self.board.kings_next_to_each_other():
raise ChessException("kings")
if fen.count('K') != 1 or fen.count('k') != 1:
raise ChessException("kings")
if self.board.has_illegal_pawns():
raise ChessException("pawns")
def __str__(self):
return self.fen
def __len__(self):
return self.board.count_figures()
def __getitem__(self, position):
return self.board.get_figure_at(position)
def _get_score_without(self, ignored_figures):
all_figures = []
for line in self.board:
all_figures += line
all_figures = list(filter((' ').__ne__, all_figures))
for figure in ignored_figures:
all_figures = list(filter((figure).__ne__, all_figures))
return ChessScore(all_figures)
def get_white_score(self):
return self._get_score_without(['r', 'n', 'b', 'q', 'k', 'p'])
def get_black_score(self):
return self._get_score_without(['R', 'N', 'B', 'Q', 'K', 'P'])
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()
class ChessScore:
def __init__(self, figures):
self.score = 0
for figure in figures:
- figure = figure.lowercase()
+ figure = figure.lower()
match figure:
case 'r':
self.score += 5
case 'n':
self.score += 3
case 'b':
self.score += 3
case 'q':
self.score += 9
case 'k':
self.score += 4
case 'p':
self.score += 1
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 __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
+ return self.score != other.score

Виктор обнови решението на 27.11.2022 20:55 (преди почти 2 години)

+FIGURE_TO_POINTS = {'r': 5, 'n': 3, 'b': 3, 'q': 9, 'k': 4, 'p': 1}
class ChessException(Exception):
pass
class ChessBoard:
def __init__(self, fen):
for number in range(1, 10):
fen = fen.replace(chr(number + ord('0')), ' '.ljust(number))
fen = fen.split('/')
for index, string in enumerate(fen):
fen[index] = [*string]
self.board = fen
def kings_next_to_each_other(self):
k_x, k_y, K_x, K_y = self.get_kings_positions()
if k_x == -1 or K_x == -1:
return True
return not (abs(k_x - K_x) > 1 or abs(k_y - K_y) > 1)
def get_kings_positions(self):
k_x, k_y, K_x, K_y = -1, -1, -1, -1
for line_index, line in enumerate(self.board):
for element_index, element in enumerate(line):
if element == 'k':
k_x, k_y = line_index, element_index
if element == 'K':
K_x, K_y = line_index, element_index
if k_x != -1 and k_y != -1 and K_x != -1 and K_y != -1:
return k_x, k_y, K_x, K_y
return k_x, k_y, K_x, K_y
def has_illegal_pawns(self):
return 'p' in self.board[0] or 'p' in self.board[7] or 'P' in self.board[0] or 'P' in self.board[7]
def count_figures(self):
counter = 0
for line in self.board:
for figure in line:
if figure != ' ':
counter += 1
return counter
def get_figure_at(self, position):
line = 8 - (ord(position[1]) - ord('0'))
figure = ord(position[0]) - ord('A')
if self.board[line][figure] == ' ':
return None
else:
return self.board[line][figure]
class ChessPosition:
def __init__(self, fen):
self.fen = fen
self.board = ChessBoard(fen)
if self.board.kings_next_to_each_other():
raise ChessException("kings")
if fen.count('K') != 1 or fen.count('k') != 1:
raise ChessException("kings")
if self.board.has_illegal_pawns():
raise ChessException("pawns")
def __str__(self):
return self.fen
def __len__(self):
return self.board.count_figures()
def __getitem__(self, position):
return self.board.get_figure_at(position)
def _get_score_without(self, ignored_figures):
all_figures = []
for line in self.board:
all_figures += line
all_figures = list(filter((' ').__ne__, all_figures))
for figure in ignored_figures:
all_figures = list(filter((figure).__ne__, all_figures))
return ChessScore(all_figures)
def get_white_score(self):
return self._get_score_without(['r', 'n', 'b', 'q', 'k', 'p'])
def get_black_score(self):
return self._get_score_without(['R', 'N', 'B', 'Q', 'K', 'P'])
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()
class ChessScore:
def __init__(self, figures):
self.score = 0
for figure in figures:
- figure = figure.lower()
-
- match figure:
- case 'r':
- self.score += 5
- case 'n':
- self.score += 3
- case 'b':
- self.score += 3
- case 'q':
- self.score += 9
- case 'k':
- self.score += 4
- case 'p':
- self.score += 1
+ self.score += FIGURE_TO_POINTS[figure.lower()]
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 __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

Виктор обнови решението на 27.11.2022 20:57 (преди почти 2 години)

FIGURE_TO_POINTS = {'r': 5, 'n': 3, 'b': 3, 'q': 9, 'k': 4, 'p': 1}
class ChessException(Exception):
pass
class ChessBoard:
def __init__(self, fen):
for number in range(1, 10):
fen = fen.replace(chr(number + ord('0')), ' '.ljust(number))
fen = fen.split('/')
for index, string in enumerate(fen):
fen[index] = [*string]
self.board = fen
def kings_next_to_each_other(self):
k_x, k_y, K_x, K_y = self.get_kings_positions()
if k_x == -1 or K_x == -1:

Това ми се струва, че е случаят с липсващи царе. Подвеждащо е да е тук, защото методът ти има друго име. Ако преди да извикаш това, си се подсигурил, че имаш точно по един цар, тези редове става излишни.

return True
return not (abs(k_x - K_x) > 1 or abs(k_y - K_y) > 1)
def get_kings_positions(self):
k_x, k_y, K_x, K_y = -1, -1, -1, -1
for line_index, line in enumerate(self.board):
for element_index, element in enumerate(line):
if element == 'k':
k_x, k_y = line_index, element_index
if element == 'K':
K_x, K_y = line_index, element_index
if k_x != -1 and k_y != -1 and K_x != -1 and K_y != -1:
return k_x, k_y, K_x, K_y
return k_x, k_y, K_x, K_y
def has_illegal_pawns(self):
return 'p' in self.board[0] or 'p' in self.board[7] or 'P' in self.board[0] or 'P' in self.board[7]
def count_figures(self):
counter = 0
for line in self.board:
for figure in line:
if figure != ' ':
counter += 1
return counter
def get_figure_at(self, position):
line = 8 - (ord(position[1]) - ord('0'))
figure = ord(position[0]) - ord('A')
if self.board[line][figure] == ' ':
return None
else:
return self.board[line][figure]
class ChessPosition:
def __init__(self, fen):
self.fen = fen
self.board = ChessBoard(fen)
if self.board.kings_next_to_each_other():
raise ChessException("kings")
if fen.count('K') != 1 or fen.count('k') != 1:
raise ChessException("kings")
if self.board.has_illegal_pawns():
raise ChessException("pawns")
def __str__(self):
return self.fen
def __len__(self):
return self.board.count_figures()
def __getitem__(self, position):
return self.board.get_figure_at(position)
def _get_score_without(self, ignored_figures):
all_figures = []
for line in self.board:
all_figures += line
all_figures = list(filter((' ').__ne__, all_figures))

В нормални обстоятелства не би трябвало да ти се налага да използваш дъндъри по този начин. Освен това не спестяваш много. Бих го написал:
all_figures = list(filter(lambda x: x not in figures + [' '], all_figures))

Това трябва да се погрижи и за двата ти филтъра.

for figure in ignored_figures:
all_figures = list(filter((figure).__ne__, all_figures))
+ for figure in all_figures:
+ figure = figure.lower()
+
return ChessScore(all_figures)
def get_white_score(self):
return self._get_score_without(['r', 'n', 'b', 'q', 'k', 'p'])

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

def get_black_score(self):
return self._get_score_without(['R', 'N', 'B', 'Q', 'K', 'P'])
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()
class ChessScore:
def __init__(self, figures):
self.score = 0
for figure in figures:
- self.score += FIGURE_TO_POINTS[figure.lower()]
+ self.score += FIGURE_TO_POINTS[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 __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

Виктор обнови решението на 29.11.2022 14:21 (преди почти 2 години)

FIGURE_TO_POINTS = {'r': 5, 'n': 3, 'b': 3, 'q': 9, 'k': 4, 'p': 1}
class ChessException(Exception):
pass
class ChessBoard:
def __init__(self, fen):
for number in range(1, 10):
- fen = fen.replace(chr(number + ord('0')), ' '.ljust(number))
+ fen = fen.replace(str(number), ' '.ljust(number))
fen = fen.split('/')
for index, string in enumerate(fen):
fen[index] = [*string]
self.board = fen
+ def get_board(self):
+ return self.board
+
def kings_next_to_each_other(self):
k_x, k_y, K_x, K_y = self.get_kings_positions()
- if k_x == -1 or K_x == -1:
- return True
-
return not (abs(k_x - K_x) > 1 or abs(k_y - K_y) > 1)
def get_kings_positions(self):
k_x, k_y, K_x, K_y = -1, -1, -1, -1
for line_index, line in enumerate(self.board):
for element_index, element in enumerate(line):
if element == 'k':
k_x, k_y = line_index, element_index
if element == 'K':
K_x, K_y = line_index, element_index
if k_x != -1 and k_y != -1 and K_x != -1 and K_y != -1:
return k_x, k_y, K_x, K_y
- return k_x, k_y, K_x, K_y
-
def has_illegal_pawns(self):
- return 'p' in self.board[0] or 'p' in self.board[7] or 'P' in self.board[0] or 'P' in self.board[7]
+ return 'p' in map(str.lower, self.board[0] + self.board[7])
def count_figures(self):
counter = 0
for line in self.board:
for figure in line:
if figure != ' ':
counter += 1
return counter
def get_figure_at(self, position):
line = 8 - (ord(position[1]) - ord('0'))
figure = ord(position[0]) - ord('A')
if self.board[line][figure] == ' ':
return None
else:
return self.board[line][figure]
class ChessPosition:
def __init__(self, fen):
self.fen = fen
self.board = ChessBoard(fen)
if self.board.kings_next_to_each_other():
raise ChessException("kings")
if fen.count('K') != 1 or fen.count('k') != 1:
raise ChessException("kings")
if self.board.has_illegal_pawns():
raise ChessException("pawns")
def __str__(self):
return self.fen
def __len__(self):
return self.board.count_figures()
def __getitem__(self, position):
return self.board.get_figure_at(position)
- def _get_score_without(self, ignored_figures):
- all_figures = []
+ def _get_score_for(self, wanted_figures):
+ all_figures, figures = [], []
- for line in self.board:
+ for line in self.board.get_board():
all_figures += line
- all_figures = list(filter((' ').__ne__, all_figures))
+ for figure in all_figures:
+ if figure in wanted_figures:
+ figures += figure
- for figure in ignored_figures:
- all_figures = list(filter((figure).__ne__, all_figures))
-
for figure in all_figures:
figure = figure.lower()
return ChessScore(all_figures)
def get_white_score(self):
- return self._get_score_without(['r', 'n', 'b', 'q', 'k', 'p'])
+ return self._get_score_for(['R', 'N', 'B', 'Q', 'K', 'P'])
def get_black_score(self):
- return self._get_score_without(['R', 'N', 'B', 'Q', 'K', 'P'])
+ return self._get_score_for(['r', 'n', 'b', 'q', 'k', 'p'])
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()
class ChessScore:
def __init__(self, figures):
self.score = 0
for figure in figures:
self.score += FIGURE_TO_POINTS[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 __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