Решение на Шахматни фенове от Евтим Иванов

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

Към профила на Евтим Иванов

Резултати

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

Код

VALID_FIGURES = ['r', 'n', 'b', 'q', 'p', 'k', 'R', 'N', 'B', 'Q', 'K', 'P']
WHITE_FIGURES = ['R', 'N', 'B', 'Q', 'K', 'P']
BLACK_FIGURES = ['r', 'n', 'b', 'q', 'k', 'p']
WHITE_KING = 'K'
BLACK_KING = 'k'
INVALID_KINGS_POSITION = (-1, -1)
PAWNS = ['P', 'p']
FIGURE_SCORE = {'r':5, 'n':3, 'b':3, 'q':9, 'k':4, 'p':1}
TABLE_COLUMNS = {'A': 0, 'B': 1, 'C':2, 'D':3, 'E':4, 'F':5, 'G':6, 'H':7}
class ChessException(Exception):
def __init__(self):
pass
class ChessPosition:
def __init__(self, fen):
self.fen = fen
self.build_table()
self.check_kings()
self.check_pawns()
self.white_figures = []
self.black_figures = []
def build_table(self):
self.table = []
figures_in_rows = self.fen.split('/')
for row in reversed(figures_in_rows):
current_row = []
for info_for_figure in row:
if info_for_figure in VALID_FIGURES:
current_row.append(info_for_figure)
elif info_for_figure.isnumeric():
empty_rows = [None] * int(info_for_figure)
current_row.extend(empty_rows)
self.table.append(current_row)
def check_kings(self):
position_of_white_king = INVALID_KINGS_POSITION
position_of_black_king = INVALID_KINGS_POSITION
for row_index, row in enumerate(self.table):
for column_index, column in enumerate(row):
figure = self.table[row_index][column_index]
if figure == WHITE_KING:
current_position_of_white_king = (row_index, column_index)
if self.king_already_exists(position_of_white_king):
raise ChessException('kings')
else:
position_of_white_king = current_position_of_white_king
elif figure == BLACK_KING:
current_position_of_black_king = (row_index, column_index)
if self.king_already_exists(position_of_black_king):
raise ChessException('kings')
else:
position_of_black_king = current_position_of_black_king
if self.check_kings_for_invalid_positions(position_of_white_king, position_of_black_king) or\
self.check_if_both_kings_exists(position_of_white_king, position_of_black_king):
raise ChessException('kings')
def check_pawns(self):
rows_to_check = [0, 7]
for row_to_check in rows_to_check:
for column_index, column in enumerate(self.table[row_to_check]):
figure = self.table[row_to_check][column_index]
if figure in PAWNS:
raise ChessException('pawns')
def get_white_score(self):
self.white_figures = self.get_color_score(WHITE_FIGURES)
return ChessScore(self.white_figures)

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

def get_black_score(self):
self.black_figures = self.get_color_score(BLACK_FIGURES)
return ChessScore(self.black_figures)
def get_color_score(self, figures_of_that_color):
current_color_figures = []
for row_index, row in enumerate(self.table):
for column_index, column in enumerate(row):
figure = self.table[row_index][column_index]
if figure in figures_of_that_color:
current_color_figures.append(figure)
return current_color_figures
def __str__(self):
return self.fen
def __len__(self):
number_of_figures = 0
for row_index, row in enumerate(self.table):
for column_index, column in enumerate(row):
figure = self.table[row_index][column_index]
if figure in BLACK_FIGURES or figure in WHITE_FIGURES:
number_of_figures += 1
return number_of_figures
def __getitem__(self, table_position):
row = int(table_position[1]) - 1
column = table_position[0]
return self.table[row][TABLE_COLUMNS[column]]
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_eqaul(self):
return self.get_black_score() == self.get_white_score()
def king_already_exists(self, position_of_king):
return position_of_king != INVALID_KINGS_POSITION
def check_kings_for_invalid_positions(self, position_of_white_king, position_of_black_king):
difference_in_x = abs(position_of_white_king[0] - position_of_black_king[0])
differnece_in_y = abs(position_of_white_king[1] - position_of_black_king[1])
return difference_in_x <= 1 and differnece_in_y <= 1
def check_if_both_kings_exists(self, position_of_white_king, position_of_black_king):
return position_of_black_king == INVALID_KINGS_POSITION or position_of_white_king == INVALID_KINGS_POSITION
class ChessScore:
def __init__(self, figures):
self.figures = list(map(str.lower, figures))
self.get_score()
def get_score(self):
self.score = 0
for figure in self.figures:
if figure in FIGURE_SCORE:
self.score += FIGURE_SCORE[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

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

EE....EE.E.E.....
======================================================================
ERROR: test_correct_parent (test.TestChessException)
Ensure ChessException is a child of Exception.
----------------------------------------------------------------------
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: ChessException.__init__() takes 1 positional argument but 2 were given

======================================================================
ERROR: test_against_touching_kings (test.TestChessPosition)
Test for kings next to each other.
----------------------------------------------------------------------
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: ChessException.__init__() takes 1 positional argument but 2 were given

======================================================================
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
AttributeError: 'ChessPosition' object has no attribute 'is_equal'

======================================================================
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: ChessException.__init__() takes 1 positional argument but 2 were given

======================================================================
ERROR: test_pawns_position (test.TestChessPosition)
Test for incorrect pawns.
----------------------------------------------------------------------
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: ChessException.__init__() takes 1 positional argument but 2 were given

======================================================================
ERROR: test_validation_conflict (test.TestChessPosition)
Test for correct Exception on multiple validation fails.
----------------------------------------------------------------------
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: ChessException.__init__() takes 1 positional argument but 2 were given

----------------------------------------------------------------------
Ran 17 tests in 0.165s

FAILED (errors=6)

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

Евтим обнови решението на 26.11.2022 15:19 (преди почти 2 години)

+VALID_FIGURES = ['r', 'n', 'b', 'q', 'p', 'k', 'R', 'N', 'B', 'Q', 'K', 'P']
+WHITE_FIGURES = ['R', 'N', 'B', 'Q', 'K', 'P']
+BLACK_FIGURES = ['r', 'n', 'b', 'q', 'k', 'p']
+WHITE_KING = 'K'
+BLACK_KING = 'k'
+INVALID_KINGS_POSITION = (-1, -1)
+PAWNS = ['P', 'p']
+FIGURE_SCORE = {'r':5, 'n':3, 'b':3, 'q':9, 'k':4, 'p':1}
+TABLE_COLUMNS = {'A': 0, 'B': 1, 'C':2, 'D':3, 'E':4, 'F':5, 'G':6, 'H':7}
+
+class ChessException(Exception):
+ def __init__(self, message = 'CHESS PROBLEM OCCURED'):
+ self.message = message
+ super().__init__(self.message)
+
+
+class ChessPosition:
+ def __init__(self, fen):
+ self.fen = fen
+ self.build_table()
+ self.check_kings()
+ self.check_pawns()
+
+ def build_table(self):
+ self.table = []
+ figures_in_rows = self.fen.split('/')
+
+ for row in reversed(figures_in_rows):
+ current_row = []
+ for info_for_figure in row:
+ if info_for_figure in VALID_FIGURES:
+ current_row.append(info_for_figure)
+ elif info_for_figure.isnumeric():
+ empty_rows = [None] * int(info_for_figure)
+ current_row.extend(empty_rows)
+ self.table.append(current_row)
+
+ def check_kings(self):
+ position_of_white_king = INVALID_KINGS_POSITION
+ position_of_black_king = INVALID_KINGS_POSITION
+ for row_index, row in enumerate(self.table):
+ for column_index, column in enumerate(row):
+ figure = self.table[row_index][column_index]
+ if figure == WHITE_KING:
+ current_position_of_white_king = (row_index, column_index)
+
+ if self.king_already_exists(position_of_white_king):
+ raise ChessException('kings')
+ else:
+ position_of_white_king = current_position_of_white_king
+ elif figure == BLACK_KING:
+ current_position_of_black_king = (row_index, column_index)
+
+ if self.king_already_exists(position_of_black_king):
+ raise ChessException('kings')
+ else:
+ position_of_black_king = current_position_of_black_king
+
+ if self.check_kings_for_invalid_positions(position_of_white_king, position_of_black_king) or\
+ self.check_if_both_kings_exists(position_of_white_king, position_of_black_king):
+ raise ChessException('kings')
+
+ def check_pawns(self):
+ rows_to_check = [0, 7]
+
+ for row_to_check in rows_to_check:
+ for column_index, column in enumerate(self.table[row_to_check]):
+ figure = self.table[row_to_check][column_index]
+ if figure in PAWNS:
+ raise ChessException('pawns')
+
+ def get_white_score(self):
+ self.white_figures = []
+ self.white_figures = self.get_color_score(WHITE_FIGURES)
+ return ChessScore(self.white_figures)

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

+
+ def get_black_score(self):
+ self.black_figures = []
+ self.black_figures = self.get_color_score(BLACK_FIGURES)
+ return ChessScore(self.black_figures)
+
+ def get_color_score(self, FIGURES_OF_THAT_COLOR):
+ current_color_figures = []
+ for row_index, row in enumerate(self.table):
+ for column_index, column in enumerate(row):
+ figure = self.table[row_index][column_index]
+ if figure in FIGURES_OF_THAT_COLOR:
+ current_color_figures.append(figure)
+ return current_color_figures
+
+ def __str__(self):
+ return self.fen
+
+ def __len__(self):
+ number_of_figures = 0
+ for row_index, row in enumerate(self.table):
+ for column_index, column in enumerate(row):
+ figure = self.table[row_index][column_index]
+ if figure in BLACK_FIGURES or figure in WHITE_FIGURES:
+ number_of_figures += 1
+ return number_of_figures
+
+ def __getitem__(self, table_position):
+ row = int(table_position[1]) - 1
+ column = table_position[0]
+ return self.table[row][TABLE_COLUMNS[column]]
+
+ 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_eqaul(self):
+ return self.get_black_score() == self.get_white_score()
+
+ def king_already_exists(self, position_of_king):
+ return position_of_king != INVALID_KINGS_POSITION
+
+ def check_kings_for_invalid_positions(self, position_of_white_king, position_of_black_king):
+ return abs(position_of_white_king[0] - position_of_black_king[0]) <= 1 and\
+ abs(position_of_white_king[1] - position_of_black_king[1]) <= 1
+
+ def check_if_both_kings_exists(self, position_of_white_king, position_of_black_king):
+ return position_of_black_king == INVALID_KINGS_POSITION or position_of_white_king == INVALID_KINGS_POSITION
+
+
+class ChessScore:
+ def __init__(self, figures):
+ self.figures = list(map(str.lower,figures))
+
+ def get_score(self):
+ score = 0
+ for figure in self.figures:
+ if figure in FIGURE_SCORE:
+ score += FIGURE_SCORE[figure]
+ return score
+
+ def __add__(self, other):
+ return self.get_score() + other.get_score()
+
+ def __sub__(self, other):
+ return self.get_score() - other.get_score()
+
+ def __lt__(self, other):
+ return self.get_score() < other.get_score()
+
+ def __gt__(self, other):
+ return self.get_score() > other.get_score()
+
+ def __le__(self, other):
+ return self.get_score() <= other.get_score()
+
+ def __ge__(self, other):
+ return self.get_score() >= other.get_score()
+
+ def __eq__(self, other):
+ return self.get_score() == other.get_score()
+
+ def __ne__(self, other):
+ return self.get_score() != other.get_score()
+

Евтим обнови решението на 27.11.2022 14:29 (преди почти 2 години)

VALID_FIGURES = ['r', 'n', 'b', 'q', 'p', 'k', 'R', 'N', 'B', 'Q', 'K', 'P']
WHITE_FIGURES = ['R', 'N', 'B', 'Q', 'K', 'P']
BLACK_FIGURES = ['r', 'n', 'b', 'q', 'k', 'p']
WHITE_KING = 'K'
BLACK_KING = 'k'
INVALID_KINGS_POSITION = (-1, -1)
PAWNS = ['P', 'p']
FIGURE_SCORE = {'r':5, 'n':3, 'b':3, 'q':9, 'k':4, 'p':1}
TABLE_COLUMNS = {'A': 0, 'B': 1, 'C':2, 'D':3, 'E':4, 'F':5, 'G':6, 'H':7}
class ChessException(Exception):
- def __init__(self, message = 'CHESS PROBLEM OCCURED'):
- self.message = message
- super().__init__(self.message)
+ def __init__(self):
+ pass
class ChessPosition:
def __init__(self, fen):
self.fen = fen
self.build_table()
self.check_kings()
self.check_pawns()
+ self.white_figures = []
+ self.black_figures = []
def build_table(self):
self.table = []
figures_in_rows = self.fen.split('/')
for row in reversed(figures_in_rows):
current_row = []
for info_for_figure in row:
if info_for_figure in VALID_FIGURES:
current_row.append(info_for_figure)
elif info_for_figure.isnumeric():
empty_rows = [None] * int(info_for_figure)
current_row.extend(empty_rows)
self.table.append(current_row)
def check_kings(self):
position_of_white_king = INVALID_KINGS_POSITION
position_of_black_king = INVALID_KINGS_POSITION
for row_index, row in enumerate(self.table):
for column_index, column in enumerate(row):
figure = self.table[row_index][column_index]
if figure == WHITE_KING:
current_position_of_white_king = (row_index, column_index)
if self.king_already_exists(position_of_white_king):
raise ChessException('kings')
else:
position_of_white_king = current_position_of_white_king
elif figure == BLACK_KING:
current_position_of_black_king = (row_index, column_index)
if self.king_already_exists(position_of_black_king):
raise ChessException('kings')
else:
position_of_black_king = current_position_of_black_king
if self.check_kings_for_invalid_positions(position_of_white_king, position_of_black_king) or\
self.check_if_both_kings_exists(position_of_white_king, position_of_black_king):
raise ChessException('kings')
def check_pawns(self):
rows_to_check = [0, 7]
for row_to_check in rows_to_check:
for column_index, column in enumerate(self.table[row_to_check]):
figure = self.table[row_to_check][column_index]
if figure in PAWNS:
raise ChessException('pawns')
def get_white_score(self):
- self.white_figures = []
self.white_figures = self.get_color_score(WHITE_FIGURES)
return ChessScore(self.white_figures)
def get_black_score(self):
- self.black_figures = []
self.black_figures = self.get_color_score(BLACK_FIGURES)
return ChessScore(self.black_figures)
- def get_color_score(self, FIGURES_OF_THAT_COLOR):
+ def get_color_score(self, figures_of_that_color):
current_color_figures = []
for row_index, row in enumerate(self.table):
for column_index, column in enumerate(row):
figure = self.table[row_index][column_index]
- if figure in FIGURES_OF_THAT_COLOR:
+ if figure in figures_of_that_color:
current_color_figures.append(figure)
return current_color_figures
def __str__(self):
return self.fen
def __len__(self):
number_of_figures = 0
for row_index, row in enumerate(self.table):
for column_index, column in enumerate(row):
figure = self.table[row_index][column_index]
if figure in BLACK_FIGURES or figure in WHITE_FIGURES:
number_of_figures += 1
return number_of_figures
def __getitem__(self, table_position):
row = int(table_position[1]) - 1
column = table_position[0]
return self.table[row][TABLE_COLUMNS[column]]
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_eqaul(self):
return self.get_black_score() == self.get_white_score()
def king_already_exists(self, position_of_king):
return position_of_king != INVALID_KINGS_POSITION
def check_kings_for_invalid_positions(self, position_of_white_king, position_of_black_king):
- return abs(position_of_white_king[0] - position_of_black_king[0]) <= 1 and\
- abs(position_of_white_king[1] - position_of_black_king[1]) <= 1
+ difference_in_x = abs(position_of_white_king[0] - position_of_black_king[0])
+ differnece_in_y = abs(position_of_white_king[1] - position_of_black_king[1])
+ return difference_in_x <= 1 and differnece_in_y <= 1
def check_if_both_kings_exists(self, position_of_white_king, position_of_black_king):
return position_of_black_king == INVALID_KINGS_POSITION or position_of_white_king == INVALID_KINGS_POSITION
class ChessScore:
def __init__(self, figures):
- self.figures = list(map(str.lower,figures))
+ self.figures = list(map(str.lower, figures))
+ self.get_score()
def get_score(self):
- score = 0
+ self.score = 0
for figure in self.figures:
if figure in FIGURE_SCORE:
- score += FIGURE_SCORE[figure]
- return score
+ self.score += FIGURE_SCORE[figure]
+ def __int__(self):
+ return self.score
+
def __add__(self, other):
- return self.get_score() + other.get_score()
+ return self.score + other.score
def __sub__(self, other):
- return self.get_score() - other.get_score()
+ return self.score - other.score
def __lt__(self, other):
- return self.get_score() < other.get_score()
+ return self.score < other.score
def __gt__(self, other):
- return self.get_score() > other.get_score()
+ return self.score > other.score
def __le__(self, other):
- return self.get_score() <= other.get_score()
+ return self.score <= other.score
def __ge__(self, other):
- return self.get_score() >= other.get_score()
+ return self.score >= other.score
def __eq__(self, other):
- return self.get_score() == other.get_score()
+ return self.score == other.score
def __ne__(self, other):
- return self.get_score() != other.get_score()
+ return self.score != other.score