Решение на Шахматни фенове от Георги Чобанов

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

Към профила на Георги Чобанов

Резултати

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

Код

white_piece_points = {
'R': 5,
'N': 3,
'B': 3,
'Q': 9,
'K': 4,
'P': 1
}
black_piece_points = {
'r': 5,
'n': 3,
'b': 3,
'q': 9,
'k': 4,
'p': 1
}
class ChessException(Exception):
pass
class ChessPosition:
def __init__(self, fen):
board = self._init_board(fen)
if not self._is_king_init_valid(fen, board):
raise ChessException('kings')
if not self._is_pawn_init_valid(board):
raise ChessException('pawns')
self._board = board
self._fen = fen
def _init_board(self, fen):
board = []
rows = fen.split('/')
for row in reversed(rows):
row_to_append = []
for symbol in row:
if symbol >= '1' and symbol <= '8':
row_to_append += ['0'] * int(symbol)
else:
row_to_append.append(symbol)
board.append(row_to_append)
return board
def _is_king_init_valid(self, fen, board):
if fen.count('k') != 1 or fen.count('K') != 1:
return False
black_king_row, black_king_col = (0, 0)
for row_number, row in enumerate(board):
if 'K' in row:
column_number = row.index('K')
black_king_row, black_king_col = row_number, column_number
break
for i in [-1, 0, 1]:
for j in [-1, 0, 1]:
if black_king_row + i > 7 or \
black_king_row + i < 0 or \
black_king_col + j > 7 or \
black_king_col + j < 0 or \
i == 0 and j == 0:
continue
if board[black_king_row + i][black_king_col + j] == 'k':
return False
return True
def _is_pawn_init_valid(self, board):
for pawn in ['p', 'P']:
if pawn in board[0] or pawn in board[7]:
return False
return True
def get_white_score(self):
score = 0
for row in range(8):
for col in range(8):
piece = self._board[row][col]
if piece in white_piece_points:
score += white_piece_points[piece]
return score
def get_black_score(self):
score = 0
for row in range(8):
for col in range(8):
piece = self._board[row][col]
if piece in black_piece_points:
score += black_piece_points[piece]
return score
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_equal(self):
return self.get_black_score() == self.get_white_score()
def __repr__(self):
return self._fen
def __len__(self):
count_pieces = 0
for row in range(8):
for col in range(8):
piece = self._board[row][col]
if piece != '0':
count_pieces += 1
return count_pieces
def __getitem__(self, position):
col = ord(position[0]) - ord('A')
row = int(position[1]) - 1
piece = self._board[row][col]
return None if piece == '0' else piece
class ChessScore:
def __init__(self, pieces):
self._score = self._caluclate_score(pieces)
def _caluclate_score(self, pieces):
pieces_scores = [black_piece_points[piece] for piece in pieces]
return sum(pieces_scores)
def __eq__(self, other):
return self._score == other._score
def __lt__(self, other):
return self._score < other._score
def __le__(self, other):
return self._score <= other._score
def __gt__(self, other):
return self._score > other._score
def __ge__(self, other):
return self._score >= other._score
def __add__(self, other):
return self._score + other._score
def __sub__(self, other):
return self._score - other._score

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

....F.......F..EE
======================================================================
ERROR: test_correct_mapping_of_pieces (test.TestChessScore)
Test correct mapping for each piece.
----------------------------------------------------------------------
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: int() argument must be a string, a bytes-like object or a real number, not 'ChessScore'

======================================================================
ERROR: test_correct_sum_of_pieces (test.TestChessScore)
Test correct sum for random pieces.
----------------------------------------------------------------------
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: int() argument must be a string, a bytes-like object or a real number, not 'ChessScore'

======================================================================
FAIL: test_get_white_score (test.TestChessPosition)
Test get_white_score.
----------------------------------------------------------------------
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: 4 is not an instance of <class 'solution.ChessScore'>

======================================================================
FAIL: test_white_is_winning (test.TestChessPosition)
Test white_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
AssertionError: 4 is not an instance of <class 'solution.ChessScore'>

----------------------------------------------------------------------
Ran 17 tests in 0.163s

FAILED (failures=2, errors=2)

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

Георги обнови решението на 29.11.2022 01:17 (преди почти 2 години)

+white_piece_points = {
+ 'R': 5,
+ 'N': 3,
+ 'B': 3,
+ 'Q': 9,
+ 'K': 4,
+ 'P': 1
+}
+
+black_piece_points = {
+ 'r': 5,
+ 'n': 3,
+ 'b': 3,
+ 'q': 9,
+ 'k': 4,
+ 'p': 1
+}
+
+
+class ChessException(Exception):
+
+ def __init__(self, message: str = "Raised a chess excpetion"):
+ super().__init__(message)
+
+
+class ChessPosition():
+
+ def __init__(self, FEN):
+ board = self._init_board(FEN)
+
+ if not self._is_king_init_valid(FEN, board):
+ raise ChessException('kings')
+
+ if not self._is_pawn_init_valid(board):
+ raise ChessException('pawns')
+
+ self._board = board
+ self._FEN = FEN
+
+ def _init_board(self, FEN):
+
+ board = []
+ rows = FEN.split('/')
+
+ for row in reversed(rows):
+ row_to_append = []
+ for symbol in row:
+ if symbol >= '1' and symbol <= '8':
+ row_to_append += ['0'] * int(symbol)
+ else:
+ row_to_append.append(symbol)
+ board.append(row_to_append)
+
+ return board
+
+ def _is_king_init_valid(self, FEN, board):
+
+ if FEN.count('k') != 1 or FEN.count('K') != 1:
+ return False
+
+ black_king_row, black_king_col = (0, 0)
+ for row_number, row in enumerate(board):
+ if 'K' in row:
+ column_number = row.index('K')
+ black_king_row, black_king_col = row_number, column_number
+ break
+
+ for i in [-1, 0, 1]:
+ for j in [-1, 0, 1]:
+ if (black_king_row + i > 7) or (
+ black_king_row + i < 0) or (
+ black_king_col + j > 7) or (
+ black_king_col + j < 0) or (
+ i == 0 and j == 0):
+ continue
+ if board[black_king_row + i][black_king_col + j] == 'k':
+ return False
+
+ return True
+
+ def _is_pawn_init_valid(self, board):
+
+ for pawn in ['p', 'P']:
+ if pawn in board[0] or pawn in board[7]:
+ return False
+
+ return True
+
+ def get_white_score(self):
+
+ score = 0
+
+ for row in range(8):
+ for col in range(8):
+ piece = self._board[row][col]
+ if piece in white_piece_points:
+ score += white_piece_points[piece]
+
+ return score
+
+ def get_black_score(self):
+
+ score = 0
+
+ for row in range(8):
+ for col in range(8):
+ piece = self._board[row][col]
+ if piece in black_piece_points:
+ score += black_piece_points[piece]
+
+ return score
+
+ 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_equal(self):
+
+ return self.get_black_score() == self.get_white_score()
+
+ def __repr__(self):
+ return self._FEN
+
+ def __len__(self):
+ count_pieces = 0
+ for row in range(8):
+ for col in range(8):
+ piece = self._board[row][col]
+ if piece != '0':
+ count_pieces += 1
+ return count_pieces
+
+ def __getitem__(self, position):
+ col = ord(position[0]) - ord('A')
+ row = int(position[1]) - 1
+ piece = self._board[row][col]
+ return None if piece == '0' else piece
+
+
+class ChessScore():
+
+ def __init__(self, pieces):
+ self._pieces = pieces
+
+ def _is_white_piece(self, piece):
+ return piece in white_piece_points
+
+ def _is_black_piece(self, piece):
+ return piece in black_piece_points
+
+ def _calculate_score(self):
+ white_pieces_scores = [white_piece_points[piece] for piece in filter(self._is_white_piece, self._pieces)]
+ black_pieces_scores = [black_piece_points[piece] for piece in filter(self._is_black_piece, self._pieces)]
+ return sum(white_pieces_scores) + sum(black_pieces_scores)
+
+ def __eq__(self, other):
+ return self._calculate_score() == other._calculate_score()
+
+ def __lt__(self, other):
+ return self._calculate_score() < other._calculate_score()
+
+ def __le__(self, other):
+ return self._calculate_score() <= other._calculate_score()
+
+ def __gt__(self, other):
+ return self._calculate_score() > other._calculate_score()
+
+ def __ge__(self, other):
+ return self._calculate_score() >= other._calculate_score()
+
+ def __add__(self, other):
+ return self._calculate_score() + other._calculate_score()
+
+ def __sub__(self, other):
+ return self._calculate_score() - other._calculate_score()

Не знам дали и друг път не съм го коментирал, но имаш доста излишни празни редове, които нарушават логическата обособеност на отделните методи и кодът се следи трудно.

Георги обнови решението на 29.11.2022 15:12 (преди почти 2 години)

white_piece_points = {
'R': 5,
'N': 3,
'B': 3,
'Q': 9,
'K': 4,
'P': 1
}
black_piece_points = {
'r': 5,
'n': 3,
'b': 3,
'q': 9,
'k': 4,
'p': 1
}
class ChessException(Exception):
+ pass
- def __init__(self, message: str = "Raised a chess excpetion"):
- super().__init__(message)
+class ChessPosition:
-class ChessPosition():
-
- def __init__(self, FEN):
- board = self._init_board(FEN)
-
- if not self._is_king_init_valid(FEN, board):
+ def __init__(self, fen):
+ board = self._init_board(fen)
+ if not self._is_king_init_valid(fen, board):
raise ChessException('kings')
-
if not self._is_pawn_init_valid(board):
raise ChessException('pawns')
-
self._board = board
- self._FEN = FEN
+ self._fen = fen
- def _init_board(self, FEN):
-
+ def _init_board(self, fen):
board = []
- rows = FEN.split('/')
-
+ rows = fen.split('/')
for row in reversed(rows):
row_to_append = []
for symbol in row:
if symbol >= '1' and symbol <= '8':
row_to_append += ['0'] * int(symbol)
else:
row_to_append.append(symbol)
- board.append(row_to_append)
-
+ board.append(row_to_append)
return board
- def _is_king_init_valid(self, FEN, board):
-
- if FEN.count('k') != 1 or FEN.count('K') != 1:
+ def _is_king_init_valid(self, fen, board):
+ if fen.count('k') != 1 or fen.count('K') != 1:
return False
-
black_king_row, black_king_col = (0, 0)
for row_number, row in enumerate(board):
if 'K' in row:
column_number = row.index('K')
black_king_row, black_king_col = row_number, column_number
break
-
for i in [-1, 0, 1]:
for j in [-1, 0, 1]:
- if (black_king_row + i > 7) or (
- black_king_row + i < 0) or (
- black_king_col + j > 7) or (
- black_king_col + j < 0) or (
- i == 0 and j == 0):
+ if black_king_row + i > 7 or \
+ black_king_row + i < 0 or \
+ black_king_col + j > 7 or \
+ black_king_col + j < 0 or \
+ i == 0 and j == 0:
continue
if board[black_king_row + i][black_king_col + j] == 'k':
return False
-
return True
def _is_pawn_init_valid(self, board):
-
for pawn in ['p', 'P']:
if pawn in board[0] or pawn in board[7]:
return False
-
return True
def get_white_score(self):
-
score = 0
-
for row in range(8):
for col in range(8):
piece = self._board[row][col]
if piece in white_piece_points:
score += white_piece_points[piece]
-
return score
def get_black_score(self):
-
score = 0
-
for row in range(8):
for col in range(8):
piece = self._board[row][col]
if piece in black_piece_points:
score += black_piece_points[piece]
-
return score
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_equal(self):
-
return self.get_black_score() == self.get_white_score()
def __repr__(self):
- return self._FEN
+ return self._fen
def __len__(self):
count_pieces = 0
for row in range(8):
for col in range(8):
piece = self._board[row][col]
if piece != '0':
count_pieces += 1
return count_pieces
def __getitem__(self, position):
col = ord(position[0]) - ord('A')
row = int(position[1]) - 1
piece = self._board[row][col]
return None if piece == '0' else piece
-class ChessScore():
+class ChessScore:
def __init__(self, pieces):
self._pieces = pieces
- def _is_white_piece(self, piece):
- return piece in white_piece_points
-
- def _is_black_piece(self, piece):
- return piece in black_piece_points
-
def _calculate_score(self):
- white_pieces_scores = [white_piece_points[piece] for piece in filter(self._is_white_piece, self._pieces)]
- black_pieces_scores = [black_piece_points[piece] for piece in filter(self._is_black_piece, self._pieces)]
- return sum(white_pieces_scores) + sum(black_pieces_scores)
+ pieces_scores = [black_piece_points[piece] for piece in self._pieces]
+ return sum(pieces_scores)
def __eq__(self, other):
return self._calculate_score() == other._calculate_score()
def __lt__(self, other):
return self._calculate_score() < other._calculate_score()
def __le__(self, other):
return self._calculate_score() <= other._calculate_score()
def __gt__(self, other):
return self._calculate_score() > other._calculate_score()
def __ge__(self, other):
return self._calculate_score() >= other._calculate_score()
def __add__(self, other):
return self._calculate_score() + other._calculate_score()
def __sub__(self, other):
return self._calculate_score() - other._calculate_score()
+

Георги обнови решението на 29.11.2022 17:44 (преди почти 2 години)

white_piece_points = {
'R': 5,
'N': 3,
'B': 3,
'Q': 9,
'K': 4,
'P': 1
}
black_piece_points = {
'r': 5,
'n': 3,
'b': 3,
'q': 9,
'k': 4,
'p': 1
}
class ChessException(Exception):
pass
class ChessPosition:
def __init__(self, fen):
board = self._init_board(fen)
if not self._is_king_init_valid(fen, board):
raise ChessException('kings')
if not self._is_pawn_init_valid(board):
raise ChessException('pawns')
self._board = board
self._fen = fen
def _init_board(self, fen):
board = []
rows = fen.split('/')
for row in reversed(rows):
row_to_append = []
for symbol in row:
if symbol >= '1' and symbol <= '8':
row_to_append += ['0'] * int(symbol)
else:
row_to_append.append(symbol)
board.append(row_to_append)
return board
def _is_king_init_valid(self, fen, board):
if fen.count('k') != 1 or fen.count('K') != 1:
return False
black_king_row, black_king_col = (0, 0)
for row_number, row in enumerate(board):
if 'K' in row:
column_number = row.index('K')
black_king_row, black_king_col = row_number, column_number
break
for i in [-1, 0, 1]:
for j in [-1, 0, 1]:
if black_king_row + i > 7 or \
black_king_row + i < 0 or \
black_king_col + j > 7 or \
black_king_col + j < 0 or \
i == 0 and j == 0:
continue
if board[black_king_row + i][black_king_col + j] == 'k':
return False
return True
def _is_pawn_init_valid(self, board):
for pawn in ['p', 'P']:
if pawn in board[0] or pawn in board[7]:
return False
return True
def get_white_score(self):
score = 0
for row in range(8):
for col in range(8):
piece = self._board[row][col]
if piece in white_piece_points:
score += white_piece_points[piece]
return score
def get_black_score(self):
score = 0
for row in range(8):
for col in range(8):
piece = self._board[row][col]
if piece in black_piece_points:
score += black_piece_points[piece]
return score
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_equal(self):
return self.get_black_score() == self.get_white_score()
def __repr__(self):
return self._fen
def __len__(self):
count_pieces = 0
for row in range(8):
for col in range(8):
piece = self._board[row][col]
if piece != '0':
count_pieces += 1
return count_pieces
def __getitem__(self, position):
col = ord(position[0]) - ord('A')
row = int(position[1]) - 1
piece = self._board[row][col]
return None if piece == '0' else piece
class ChessScore:
def __init__(self, pieces):
- self._pieces = pieces
+ self._score = self._caluclate_score(pieces)
- def _calculate_score(self):
- pieces_scores = [black_piece_points[piece] for piece in self._pieces]
+ def _caluclate_score(self, pieces):
+ pieces_scores = [black_piece_points[piece] for piece in pieces]
return sum(pieces_scores)
def __eq__(self, other):
- return self._calculate_score() == other._calculate_score()
+ return self._score == other._score
def __lt__(self, other):
- return self._calculate_score() < other._calculate_score()
+ return self._score < other._score
def __le__(self, other):
- return self._calculate_score() <= other._calculate_score()
+ return self._score <= other._score
def __gt__(self, other):
- return self._calculate_score() > other._calculate_score()
+ return self._score > other._score
def __ge__(self, other):
- return self._calculate_score() >= other._calculate_score()
+ return self._score >= other._score
def __add__(self, other):
- return self._calculate_score() + other._calculate_score()
+ return self._score + other._score
def __sub__(self, other):
- return self._calculate_score() - other._calculate_score()
-
+ return self._score - other._score