Решение на Шахматни фенове от Лиляна Белчева

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

Към профила на Лиляна Белчева

Резултати

  • 9 точки от тестове
  • 0 бонус точки
  • 9 точки общо
  • 15 успешни тест(а)
  • 2 неуспешни тест(а)

Код

Scores = {'r': 5,
'n': 3,
'b': 3,
'k': 4,
'q': 9,
'p': 1}
class ChessException(Exception):
def __init__(self, message='Chess Exception'):
self.message = message
super().__init__(self.message)
class ChessScore:
def __init__(self, figures):
for element in figures:
if element not in ('r', 'n', 'b', 'k', 'q', 'p'):
raise ChessException('Invalid chess figure')
self.figures = figures
def __int__(self):
sum_scores = 0
if len(self.figures) == 0:
return sum_scores
for key in self.figures:
if key in Scores:
sum_scores += Scores.get(key)
return sum_scores
def __eq__(self, other):
return int(self) == int(other)
def __le__(self, other):
return int(self) <= int(other)
def __lt__(self, other):
return int(self) < int(other)
def __gt__(self, other):
return int(self) > int(other)
def __ne__(self, other):
return not self == 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):
FEN = definition.split("/")
self.matrix = [{'A': ' ', 'B': ' ', 'C': ' ', 'D': ' ',
'E': ' ', 'F': ' ', 'G': ' ', 'H': ' '} for k in range(8)]
white = []
black = []
positions = [*definition]
for i, s in enumerate(FEN):
row = [*s]
k = 0
for figure in row:
low = figure.lower()
if low in ('r', 'n', 'b', 'k', 'q', 'p'):
self.matrix[7-i][chr(ord('A') + k)] = figure
k = k+1
elif low in ('1', '2', '3', '4', '5', '6', '7', '8'):
k = k + int(low)
if definition.count('K') != 1 or definition.count('k') != 1:
raise ChessException('kings')
for i, s in enumerate(FEN):
row = [*s]
k = 0
if i == 0 or i == 7:
if 'p' in row or 'P' in row:
raise ChessException('pawns')
for pos in positions:
if pos in ('r', 'n', 'b', 'k', 'q', 'p'):
black.append(pos)
elif pos in ('R', 'N', 'B', 'K', 'Q', 'P'):
white.append(pos)
lower_white = [b.lower() for b in white]
self.black_figures = ChessScore(black)
self.white_figures = ChessScore(lower_white)
self.fen = definition
def get_white_score(self):
return self.white_figures
def get_black_score(self):
return self.black_figures
def white_is_winning(self):
return int(self.white_figures) > int(self.black_figures)
def black_is_winning(self):
return int(self.white_figures) < int(self.black_figures)
def is_equal(self):
return int(self.white_figures) == int(self.black_figures)
def __str__(self):
return self.fen
def __len__(self):
size1 = 0
size2 = 0
for _ in self.white_figures.figures:
size1 = size1+1
for _ in self.black_figures.figures:
size2 = size2+1
return size1+size2
def __getitem__(self, key):
if len(key) != 2:
return None
sp = [*key]
word = sp[0]
num = int(sp[1])-1
result = self.matrix[num][word]
if result == ' ':
return None
return result

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

.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

======================================================================
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.167s

FAILED (failures=1, errors=1)

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

Лиляна обнови решението на 29.11.2022 10:07 (преди почти 2 години)

+Scores = {'r': 5,
+ 'n': 3,
+ 'b': 3,
+ 'k': 4,
+ 'q': 9,
+ 'p': 1}
+
+
+class ChessException(Exception):
+ def __init__(self, message='Chess Exception'):
+ self.message = message
+ super().__init__(self.message)
+
+
+class ChessScore:
+ def __init__(self, figures):
+ for element in figures:
+ if element not in ('r', 'n', 'b', 'k', 'q', 'p'):
+ raise ChessException('Invalid chess figure')
+ else:
+ self.figures = figures
+
+ def __int__(self):
+ sum_scores = 0
+ for key in self.figures:
+ if key in Scores:
+ sum_scores += Scores.get(key)
+ return sum_scores
+
+ def __eq__(self, other):
+ return int(self) == int(other)
+
+ def __le__(self, other):
+ return int(self) <= int(other)
+
+ def __lt__(self, other):
+ return int(self) < int(other)
+
+ def __gt__(self, other):
+ return int(self) > int(other)
+
+ def __ne__(self, other):
+ return not self == 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):
+ FEN = definition.split("/")
+ self.matrix = [{'A': ' ', 'B': ' ', 'C': ' ', 'D': ' ',
+ 'E': ' ', 'F': ' ', 'G': ' ', 'H': ' '} for k in range(8)]
+ white = []
+ black = []
+ positions = [*definition]
+
+ for i, s in enumerate(FEN):
+ row = [*s]
+ k = 0
+ for figure in row:
+ low = figure.lower()
+ if low in ('r', 'n', 'b', 'k', 'q', 'p'):
+ self.matrix[7-i][chr(ord('A') + k)] = figure
+ k = k+1
+ elif low in ('1', '2', '3', '4', '5', '6', '7', '8'):
+ k = k + int(low)
+
+ if definition.count('K') != 1 or definition.count('k') != 1:
+ raise ChessException('kings')
+
+ for i, s in enumerate(FEN):
+ row = [*s]
+ k = 0
+ if i == 0 or i == 7:
+ if 'p' in row or 'P' in row:
+ raise ChessException('pawns')
+
+ for pos in positions:
+ if pos in ('r', 'n', 'b', 'k', 'q', 'p'):
+ black.append(pos)
+ elif pos in ('R', 'N', 'B', 'K', 'Q', 'P'):
+ white.append(pos)
+
+ lower_white = [b.lower() for b in white]
+ self.black_figures = ChessScore(black)
+ self.white_figures = ChessScore(lower_white)
+ self.fen = definition
+
+ def get_white_score(self):
+ return self.white_figures
+
+ def get_black_score(self):
+ return self.black_figures
+
+ def white_is_winning(self):
+ return int(self.white_figures) > int(self.black_figures)
+
+ def black_is_winning(self):
+ return int(self.white_figures) < int(self.black_figures)
+
+ def is_equal(self):
+ return int(self.white_figures) == int(self.black_figures)
+
+ def __str__(self):
+ return self.fen
+
+ def __len__(self):
+ size1 = 0
+ size2 = 0
+ for _ in self.white_figures.figures:
+ size1 = size1+1
+ for _ in self.black_figures.figures:
+ size2 = size2+1
+ return size1+size2
+
+ def __getitem__(self, key):
+ if len(key) != 2:
+ return None
+ sp = [*key]
+ word = sp[0]
+ num = int(sp[1])-1
+ result = self.matrix[num][word]
+ if result == ' ':
+ return None
+ return result

Лиляна обнови решението на 29.11.2022 10:20 (преди почти 2 години)

Scores = {'r': 5,
'n': 3,
'b': 3,
'k': 4,
'q': 9,
'p': 1}
class ChessException(Exception):
def __init__(self, message='Chess Exception'):
self.message = message
super().__init__(self.message)
class ChessScore:
def __init__(self, figures):
for element in figures:
if element not in ('r', 'n', 'b', 'k', 'q', 'p'):
raise ChessException('Invalid chess figure')
- else:
- self.figures = figures
+ self.figures = figures
def __int__(self):
sum_scores = 0
+ if len(self.figures) == 0:
+ return sum_scores
+
for key in self.figures:
if key in Scores:
sum_scores += Scores.get(key)
return sum_scores
def __eq__(self, other):
return int(self) == int(other)
def __le__(self, other):
return int(self) <= int(other)
def __lt__(self, other):
return int(self) < int(other)
def __gt__(self, other):
return int(self) > int(other)
def __ne__(self, other):
return not self == 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):
FEN = definition.split("/")
self.matrix = [{'A': ' ', 'B': ' ', 'C': ' ', 'D': ' ',
'E': ' ', 'F': ' ', 'G': ' ', 'H': ' '} for k in range(8)]
white = []
black = []
positions = [*definition]
for i, s in enumerate(FEN):
row = [*s]
k = 0
for figure in row:
low = figure.lower()
if low in ('r', 'n', 'b', 'k', 'q', 'p'):
self.matrix[7-i][chr(ord('A') + k)] = figure
k = k+1
elif low in ('1', '2', '3', '4', '5', '6', '7', '8'):
k = k + int(low)
if definition.count('K') != 1 or definition.count('k') != 1:
raise ChessException('kings')
for i, s in enumerate(FEN):
row = [*s]
k = 0
if i == 0 or i == 7:
if 'p' in row or 'P' in row:
raise ChessException('pawns')
for pos in positions:
if pos in ('r', 'n', 'b', 'k', 'q', 'p'):
black.append(pos)
elif pos in ('R', 'N', 'B', 'K', 'Q', 'P'):
white.append(pos)
lower_white = [b.lower() for b in white]
self.black_figures = ChessScore(black)
self.white_figures = ChessScore(lower_white)
self.fen = definition
def get_white_score(self):
return self.white_figures
def get_black_score(self):
return self.black_figures
def white_is_winning(self):
return int(self.white_figures) > int(self.black_figures)
def black_is_winning(self):
return int(self.white_figures) < int(self.black_figures)
def is_equal(self):
return int(self.white_figures) == int(self.black_figures)
def __str__(self):
return self.fen
def __len__(self):
size1 = 0
size2 = 0
for _ in self.white_figures.figures:
size1 = size1+1
for _ in self.black_figures.figures:
size2 = size2+1
return size1+size2
def __getitem__(self, key):
if len(key) != 2:
return None
sp = [*key]
word = sp[0]
num = int(sp[1])-1
result = self.matrix[num][word]
if result == ' ':
return None
return result