Решение на Шахматни фенове от Пламена Кръстева

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

Към профила на Пламена Кръстева

Резултати

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

Код

class ChessException(Exception):
def __init__(self, message):
self.message = message
super().__init__(self.message)
class ChessPosition:
def __init__(self, fen):
self.fen = fen
self.board = []
def fen_to_board():
for row in self.fen.split('/'):
brow = []
for c in row:
if c == ' ':
break
elif c in '12345678':
brow.extend(['--'] * int(c))
else:
brow.append(c)
self.board.append(brow)
return self.board
self.board = fen_to_board()
for i, x in enumerate(self.board):
if 'k' in x:
index = x.index('k')
if 'K' in [self.board[i-1][index-1:index+1], self.board[i][index-1], self.board[i][index+1], self.board[i+1][index-1:index+1]]:
raise ChessException('kings')
countw, countb = 0, 0
for row in self.board:
for c in row:
if c == 'K':
countw += 1
if c == 'k':
countb += 1
if countw != 1 or countb != 1:
raise ChessException('kings')
if 'P' in self.board[7] or 'p' in self.board[0]:
raise ChessException('pawns')
def get_white_score(self):
list_of_whites = []
for row in self.board:
for c in row:
if c.isupper():
list_of_whites.append(c.lower())
return ChessScore(list_of_whites)
def get_black_score(self):
list_of_blacks = []
for row in self.board:
for c in row:
if c.islower():
list_of_blacks.append(c)
return ChessScore(list_of_blacks)
def white_is_winning(self):
return bool(self.get_white_score() > self.get_black_score())
def black_is_winning(self):
return not self.white_is_winning()
def is_equal(self):
return bool(self.get_white_score() == self.get_black_score())
def __repr__(self):
return f"ChessPosition {self.fen}"
def __str__(self):
return f"{self.fen}"
def __len__(self):
count_pieces = 0
for r in self.board:
for c in r:
if c.lower() in {'r', 'n', 'b', 'q', 'k', 'p'}:
count_pieces += 1
return count_pieces
def __position__(self, string_position):
index1 = ord(string_position[0]) - 64
index2 = string_position[1]
return self.board[index2-1][index1-1]
class ChessScore:
def __init__(self, pieces):
self.pieces = pieces
def __int__(self):
count = 0
for piece in self.pieces:
if piece == 'r':
count += 5
elif piece == 'n':
count += 3
elif piece == 'b':
count += 3
elif piece == 'q':
count += 9
elif piece == 'k':
count += 4
elif piece == 'p':
count += 1
return count
def __add__(self, other):
return int(self)+int(other)
def __sub__(self, other):
return int(self)-int(other)
def __lt__(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 __gt__(self, other):
return int(self) > int(other)
def __ge__(self, other):
return int(self) >= int(other)

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

.EE..E..EE.E.....
======================================================================
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_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
Exception: Incorrect result for 8/8/8/k7/7K/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

======================================================================
ERROR: test_len (test.TestChessPosition)
Test number of pieces for a position.
----------------------------------------------------------------------
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: list index out of range

======================================================================
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
Exception: No exception raised on: 8/8/k7/8/7K/8/8/p7

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

----------------------------------------------------------------------
Ran 17 tests in 0.162s

FAILED (errors=6)

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

Пламена обнови решението на 29.11.2022 17:36 (преди почти 2 години)

+class ChessException(Exception):
+ def __init__(self, message):
+ self.message = message
+ super().__init__(self.message)
+
+
+class ChessPosition:
+ def __init__(self, fen):
+ self.fen = fen
+ self.board = []
+
+ def fen_to_board():
+ for row in self.fen.split('/'):
+ brow = []
+ for c in row:
+ if c == ' ':
+ break
+ elif c in '12345678':
+ brow.extend(['--'] * int(c))
+ else:
+ brow.append(c)
+ self.board.append(brow)
+ return self.board
+ self.board = fen_to_board()
+ for i, x in enumerate(self.board):
+ if 'k' in x:
+ index = x.index('k')
+ if 'K' in [self.board[i-1][index-1:index+1], self.board[i][index-1], self.board[i][index+1], self.board[i+1][index-1:index+1]]:
+ raise ChessException('kings')
+ countw, countb = 0, 0
+ for row in self.board:
+ for c in row:
+ if c == 'K':
+ countw += 1
+ if c == 'k':
+ countb += 1
+ if countw != 1 or countb != 1:
+ raise ChessException('kings')
+ if 'P' in self.board[7] or 'p' in self.board[0]:
+ raise ChessException('pawns')
+
+ def get_white_score(self):
+ list_of_whites = []
+ for row in self.board:
+ for c in row:
+ if c.isupper():
+ list_of_whites.append(c.lower())
+ return ChessScore(list_of_whites)
+
+ def get_black_score(self):
+ list_of_blacks = []
+ for row in self.board:
+ for c in row:
+ if c.islower():
+ list_of_blacks.append(c)
+ return ChessScore(list_of_blacks)
+
+ def white_is_winning(self):
+ return bool(self.get_white_score() > self.get_black_score())
+
+ def black_is_winning(self):
+ return not self.white_is_winning()
+
+ def is_equal(self):
+ return bool(self.get_white_score() == self.get_black_score())
+
+ def __repr__(self):
+ return f"ChessPosition {self.fen}"
+
+ def __str__(self):
+ return f"{self.fen}"
+
+ def __len__(self):
+ count_pieces = 0
+ for r in self.board:
+ for c in r:
+ if c.lower() in {'r', 'n', 'b', 'q', 'k', 'p'}:
+ count_pieces += 1
+ return count_pieces
+
+ def __position__(self, string_position):
+ index1 = ord(string_position[0]) - 64
+ index2 = string_position[1]
+ return self.board[index2-1][index1-1]
+
+
+class ChessScore:
+ def __init__(self, pieces):
+ self.pieces = pieces
+
+ def __int__(self):
+ count = 0
+ for piece in self.pieces:
+ if piece == 'r':
+ count += 5
+ elif piece == 'n':
+ count += 3
+ elif piece == 'b':
+ count += 3
+ elif piece == 'q':
+ count += 9
+ elif piece == 'k':
+ count += 4
+ elif piece == 'p':
+ count += 1
+ return count
+
+ def __add__(self, other):
+ return int(self)+int(other)
+
+ def __sub__(self, other):
+ return int(self)-int(other)
+
+ def __lt__(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 __gt__(self, other):
+ return int(self) > int(other)
+
+ def __ge__(self, other):
+ return int(self) >= int(other)