Решение на Шахматни фенове от Димитър Аврамов

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

Към профила на Димитър Аврамов

Резултати

  • 8 точки от тестове
  • 0 бонус точки
  • 8 точки общо
  • 14 успешни тест(а)
  • 3 неуспешни тест(а)

Код

import re
class ChessException(Exception):
def __init__(self, ex_message):
super().__init__(ex_message)
self.message = ex_message
class ChessScore:
def __init__(self, figures_definitions):
self.figures = [x.lower() for x in figures_definitions]
def __int__(self):
figures_points = {'p': 1, 'k': 4, 'q': 9, 'b': 3, 'n': 3, 'r': 5}
points = 0
for figure in self.figures:
points += figures_points[figure]
return points
def __lt__(self, other):
return int(self) < int(other)
def __gt__(self, other):
return int(self) > int(other)
def __le__(self, other):
return int(self) <= int(other)
def __eq__(self, other):
return int(self) == int(other)
def __ne__(self, other):
return int(self) != int(other)
def __add__(self, other):
return int(self) + int(other)
def __sub__(self, other):
return int(self) - int(other)
class ChessPosition:
def __init__(self, definition):
self.__set_definition(definition)
self.white_figures_points = self.__calculate_points(('P', 'R', 'N', 'B', 'Q', 'K'))
self.black_figures_points = self.__calculate_points(('p', 'r', 'n', 'b', 'q', 'k'))
def __set_definition(self, definition):
if definition.count('k') != 1 or definition.count('K') != 1:
raise ChessException('kings')
if len(re.findall(r'[kK][a-zA-Z\/]{7,9}[kK]', definition)) != 0 or len(re.findall(r'kK|Kk', definition)) != 0:
raise ChessException('kings')
if (definition.split('/')[0].lower().count('p') != 0) or (definition.split('/')[7].lower().count('p') != 0):
raise ChessException('pawns')
self.fen_definition = definition
def __calculate_points(self, figures):
figures_points = {'p': 1, 'k': 4, 'q': 9, 'b': 3, 'n': 3, 'r': 5}
result = 0
for figure in self.fen_definition:
if figure in figures:
result += figures_points[figure.lower()]
return result
def white_is_winning(self):
return self.white_figures_points > self.black_figures_points
def black_is_winning(self):
return self.black_figures_points > self.white_figures_points
def is_equal(self):
return self.white_figures_points == self.black_figures_points
def get_white_score(self):
return ChessScore(self.__get_score(('P', 'R', 'N', 'B', 'Q', 'K')))
def get_black_score(self):
return ChessScore(self.__get_score(('p', 'r', 'n', 'b', 'q', 'k')))
def __get_score(self, figures):
result = []
for figure in self.fen_definition:
if figure in figures:
result.append(figure)
return result
def __str__(self):
return self.fen_definition
def __len__(self):
return len(re.findall(r'[RNBQKPrnbqkp]', self.fen_definition))
def __getitem__(self, key):
columns_indexes = {'A': 0, 'B': 1, 'C': 2, 'D': 3, 'E': 4, 'F': 5, 'G': 6, 'H': 7}
rows = self.fen_definition.split('/')
row_idx = len(rows) - int(key[1])
col_idx = columns_indexes[key[0]]
figure = rows[row_idx][col_idx]
if figure in ('P', 'R', 'N', 'B', 'Q', 'K') or figure in ('p', 'r', 'n', 'b', 'q', 'k'):
return figure
return None

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

.E...E.....F.....
======================================================================
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
Exception: No exception raised on: k7/K7/8/8/8/8/8/8

======================================================================
ERROR: test_getitem (test.TestChessPosition)
Test getitem functionality.
----------------------------------------------------------------------
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
IndexError: string index out of range

======================================================================
FAIL: 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
AssertionError: 'pawns' != 'kings'
- pawns
+ kings


----------------------------------------------------------------------
Ran 17 tests in 0.180s

FAILED (failures=1, errors=2)

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

Димитър обнови решението на 27.11.2022 16:12 (преди почти 2 години)

+import re
+
+
+class ChessException(Exception):
+ def __init__(self, ex_message):
+ super().__init__(ex_message)
+ self.message = ex_message
+
+
+class ChessScore:
+ def __init__(self, figures_definitions):
+ self.figures = [x.lower() for x in figures_definitions]
+
+ def __int__(self):
+ figures_points = {'p': 1, 'k': 4, 'q': 9, 'b': 3, 'n': 3, 'r': 5}
+ points = 0
+
+ for figure in self.figures:
+ points += figures_points[figure]
+
+ return points
+
+ def __lt__(self, other):
+ return int(self) < int(other)
+
+ def __gt__(self, other):
+ return int(self) > int(other)
+
+ def __le__(self, other):
+ return int(self) <= int(other)
+
+ def __eq__(self, other):
+ return int(self) == int(other)
+
+ def __ne__(self, other):
+ return int(self) != int(other)
+
+ def __add__(self, other):
+ return int(self) + int(other)
+
+ def __sub__(self, other):
+ return int(self) - int(other)
+
+
+class ChessPosition:
+ def __init__(self, definition):
+ self.__set_definition(definition)
+ self.white_figures_points = self.__calculate_points(('P', 'R', 'N', 'B', 'Q', 'K'))
+ self.black_figures_points = self.__calculate_points(('p', 'r', 'n', 'b', 'q', 'k'))
+
+ def __set_definition(self, definition):
+
+ if definition.count('k') != 1 or definition.count('K') != 1:
+ raise ChessException('kings')
+
+ if len(re.findall(r'[kK][a-zA-Z\/]{7,9}[kK]', definition)) != 0 or len(re.findall(r'kK|Kk', definition)) != 0:
+ raise ChessException('kings')
+
+ if (definition.split('/')[0].lower().count('p') != 0) or (definition.split('/')[7].lower().count('p') != 0):
+ raise ChessException('pawns')
+
+ self.fen_definition = definition
+
+ def __calculate_points(self, figures):
+ figures_points = {'p': 1, 'k': 4, 'q': 9, 'b': 3, 'n': 3, 'r': 5}
+ result = 0
+
+ for figure in self.fen_definition:
+ if figure in figures:
+ result += figures_points[figure.lower()]
+
+ return result
+
+ def white_is_winning(self):
+ return self.white_figures_points > self.black_figures_points
+
+ def black_is_winning(self):
+ return self.black_figures_points > self.white_figures_points
+
+ def is_equal(self):
+ return self.white_figures_points == self.black_figures_points
+
+ def get_white_score(self):
+ return ChessScore(self.__get_score(('P', 'R', 'N', 'B', 'Q', 'K')))
+
+ def get_black_score(self):
+ return ChessScore(self.__get_score(('p', 'r', 'n', 'b', 'q', 'k')))
+
+ def __get_score(self, figures):
+ result = []
+
+ for figure in self.fen_definition:
+ if figure in figures:
+ result.append(figure)
+
+ return result
+
+ def __str__(self):
+ return self.fen_definition
+
+ def __len__(self):
+ return len(re.findall(r'[RNBQKPrnbqkp]', self.fen_definition))
+
+ def __getitem__(self, key):
+ columns_indexes = {'A': 0, 'B': 1, 'C': 2, 'D': 3, 'E': 4, 'F': 5, 'G': 6, 'H': 7}
+
+ rows = self.fen_definition.split('/')
+ row_idx = len(rows) - int(key[1])
+ col_idx = columns_indexes[key[0]]
+ figure = rows[row_idx][col_idx]
+
+ if figure in ('P', 'R', 'N', 'B', 'Q', 'K') or figure in ('p', 'r', 'n', 'b', 'q', 'k'):
+ return figure
+
+ return None