Решение на Шахматни фенове от Никол Казанджиева

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

Към профила на Никол Казанджиева

Резултати

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

Код

import collections
class ChessException(Exception):
def __init__(self, message):
self.message = message
super().__init__(self.message)
def __str__(self):
return f'{self.message}'
class ChessPosition:
def __init__(self, FEN_string):
self.FEN_string = FEN_string
eight_row = self.FEN_string.split('/')[0]
first_row = self.FEN_string[::-1]
first_row = first_row.split('/')[0]
rows = first_row + eight_row
pawns = ['p', 'P']
for pawn in pawns:
if pawn in rows:
raise ChessException("pawns")
d = collections.defaultdict(int)
for c in self.FEN_string:
d[c] += 1
if d["k"] != 1 or d["K"] != 1:
raise ChessException("kings")
def get_white_score(self):
white_pieces = []
for _ in self.FEN_string:
if _ in ['R', 'N', 'B', 'K', 'Q', 'P']:
white_pieces += _
return ChessScore(white_pieces)
def get_black_score(self):
black_pieces = []
for _ in self.FEN_string:
if _ in ['r', 'n', 'b', 'k', 'q', 'p']:
black_pieces += _
return ChessScore(black_pieces)
def white_is_winning(self):
return self.get_white_score() > self.get_black_score()
def black_is_winning(self):
return self.get_white_score() < self.get_black_score()
def is_equal(self):
return self.get_white_score() == self.get_black_score()
def __len__(self):
lenn = 0
for _ in self.FEN_string:
if _ in ['r', 'n', 'b', 'k', 'q', 'p', 'R', 'N', 'B', 'K', 'Q', 'P']:
lenn += 1
return lenn
def __str__(self):
return str(self.FEN_string)
class ChessScore:
scores = dict(r=5, R=5, n=3, N=3, b=3, B=3, q=9, Q=9, k=4, K=4, p=1, P=1)
def __init__(self, pieces):
self.__chess_score = 0
for _ in pieces:
self.__chess_score += self.scores[_]
def __int__(self):
return self.__chess_score
def __add__(self, other):
return self.__chess_score + other.__chess_score
def __sub__(self, other):
return self.__chess_score - other.__chess_score
def __gt__(self, other):
if self.__chess_score > other.__chess_score:
return True
else:
return False
def __lt__(self, other):
if self.__chess_score < other.__chess_score:
return True
else:
return False
def __ge__(self, other):
if self.__chess_score >= other.__chess_score:
return True
else:
return False
def __le__(self, other):
if self.__chess_score <= other.__chess_score:
return True
else:
return False
def __eq__(self, other):
if self.__chess_score == other.__chess_score:
return True
else:
return False
def __ne__(self, other):
if self.__chess_score != other.__chess_score:
return True
else:
return False

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

.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
TypeError: 'ChessPosition' object is not subscriptable

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

FAILED (failures=1, errors=2)

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

Никол обнови решението на 29.11.2022 16:42 (преди почти 2 години)

+import collections
+
+
+class ChessException(Exception):
+ def __init__(self, message):
+ self.message = message
+ super().__init__(self.message)
+
+ def __str__(self):
+ return f'{self.message}'
+
+
+class ChessPosition:
+ def __init__(self, FEN_string):
+ self.FEN_string = FEN_string
+ eight_row = self.FEN_string.split('/')[0]
+ first_row = self.FEN_string[::-1]
+ first_row = first_row.split('/')[0]
+ rows = first_row + eight_row
+ pawns = ['p', 'P']
+ for pawn in pawns:
+ if pawn in rows:
+ raise ChessException("pawns")
+
+ d = collections.defaultdict(int)
+ for c in self.FEN_string:
+ d[c] += 1
+ if d["k"] != 1 or d["K"] != 1:
+ raise ChessException("kings")
+
+ def get_white_score(self):
+ white_pieces = []
+ for _ in self.FEN_string:
+ if _ in ['R', 'N', 'B', 'K', 'Q', 'P']:
+ white_pieces += _
+ return ChessScore(white_pieces)
+
+ def get_black_score(self):
+ black_pieces = []
+ for _ in self.FEN_string:
+ if _ in ['r', 'n', 'b', 'k', 'q', 'p']:
+ black_pieces += _
+ return ChessScore(black_pieces)
+
+ def white_is_winning(self):
+ return self.get_white_score() > self.get_black_score()
+
+ def black_is_winning(self):
+ return self.get_white_score() < self.get_black_score()
+
+ def is_equal(self):
+ return self.get_white_score() == self.get_black_score()
+
+ def __len__(self):
+ lenn = 0
+ for _ in self.FEN_string:
+ if _ in ['r', 'n', 'b', 'k', 'q', 'p', 'R', 'N', 'B', 'K', 'Q', 'P']:
+ lenn += 1
+ return lenn
+
+ def __str__(self):
+ return str(self.FEN_string)
+
+
+class ChessScore:
+ scores = dict(r=5, R=5, n=3, N=3, b=3, B=3, q=9, Q=9, k=4, K=4, p=1, P=1)
+
+ def __init__(self, pieces):
+ self.__chess_score = 0
+ for _ in pieces:
+ self.__chess_score += self.scores[_]
+
+ def __int__(self):
+ return self.__chess_score
+
+ def __add__(self, other):
+ return self.__chess_score + other.__chess_score
+
+ def __sub__(self, other):
+ return self.__chess_score - other.__chess_score
+
+ def __gt__(self, other):
+ if self.__chess_score > other.__chess_score:
+ return True
+ else:
+ return False
+
+ def __lt__(self, other):
+ if self.__chess_score < other.__chess_score:
+ return True
+ else:
+ return False
+
+ def __ge__(self, other):
+ if self.__chess_score >= other.__chess_score:
+ return True
+ else:
+ return False
+
+ def __le__(self, other):
+ if self.__chess_score <= other.__chess_score:
+ return True
+ else:
+ return False
+
+ def __eq__(self, other):
+ if self.__chess_score == other.__chess_score:
+ return True
+ else:
+ return False
+
+ def __ne__(self, other):
+ if self.__chess_score != other.__chess_score:
+ return True
+ else:
+ return False