Решение на Шахматни фенове от Николай Натов

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

Към профила на Николай Натов

Резултати

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

Код

class ChessException(Exception):
pass
class ChessScore:
piece_scores = {
'p': 1,
'r': 5,
'n': 3,
'b': 3,
'k': 4,
'q': 9
}
def __init__(self, chess_pieces):
self.total_score = 0
for piece in chess_pieces:
self.total_score += ChessScore.piece_scores[piece.lower()]
def __int__(self):
return self.total_score
def __lt__(self, other):
return self.total_score < other.total_score
def __le__(self, other):
return self.total_score <= other.total_score
def __gt__(self, other):
return self.total_score > other.total_score
def __ge__(self, other):
return self.total_score >= other.total_score
def __eq__(self, other):
return self.total_score == other.total_score
def __ne__(self, other):
return self.total_score != other.total_score
def __add__(self, other):
return self.total_score + other.total_score
def __sub__(self, other):
return self.total_score - other.total_score
class ChessPosition:
def __init__(self, chess_position_string):
self.chess_position_string = chess_position_string
self.white_pieces = []
self.black_pieces = []
self.board = []
for row in chess_position_string.split("/"):
board_row = []
for piece in row:
if piece.isdigit():
board_row.extend([''] * int(piece))
else:
board_row.append(piece)
self.board.append(board_row)
self.validate_position()
for row in self.board:
for piece in row:
if piece.isalpha() and piece.isupper():
self.white_pieces.append(piece)
elif piece.isalpha() and piece.islower():
self.black_pieces.append(piece)
def validate_position(self):
white_king_count = 0
black_king_count = 0
pawn_positions_valid = True
for i in range(len(self.board)):
for j in range(len(self.board[i])):
if self.board[i][j] == 'K':
white_king_position = (i, j)
white_king_count += 1
elif self.board[i][j] == 'k':
black_king_position = (i, j)
black_king_count += 1
elif self.board[i][j] == 'P' or self.board[i][j] == 'p':
if i == 0 or i == 7:
pawn_positions_valid = False
if white_king_count != 1 or black_king_count != 1:
raise ChessException("kings")
if abs(white_king_position[0] - black_king_position[0]) <= 1 and abs(white_king_position[1] - black_king_position[1]) <= 1:
raise ChessException("kings")
if not pawn_positions_valid:
raise ChessException("pawns")
def get_white_score(self):
return ChessScore(self.white_pieces)
def get_black_score(self):
return ChessScore(self.black_pieces)
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_white_score() == self.get_black_score()
def __str__(self):
return self.chess_position_string
def __len__(self):
return len(self.white_pieces) + len(self.black_pieces)
def __getitem__(self, key):
column_idx = ord(key[0]) - ord('A')
row_idx = len(self.board) - int(key[1])
if self.board[row_idx][column_idx] == '':
return None
return self.board[row_idx][column_idx]

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

.................
----------------------------------------------------------------------
Ran 17 tests in 0.170s

OK

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

Николай обнови решението на 28.11.2022 14:59 (преди почти 2 години)

+class ChessException(Exception):
+ pass
+
+class ChessScore:
+ piece_scores = {
+ 'p': 1,
+ 'r': 5,
+ 'n': 3,
+ 'b': 3,
+ 'k': 4,
+ 'q': 9
+ }
+
+ def __init__(self, chess_pieces):
+ self.total_score = 0
+ for piece in chess_pieces:
+ self.total_score += ChessScore.piece_scores[piece.lower()]
+
+ def __int__(self):
+ return self.total_score
+
+ def __lt__(self, other):
+ return self.total_score < other.total_score
+
+ def __le__(self, other):
+ return self.total_score <= other.total_score
+
+ def __gt__(self, other):
+ return self.total_score > other.total_score
+
+ def __ge__(self, other):
+ return self.total_score >= other.total_score
+
+ def __eq__(self, other):
+ return self.total_score == other.total_score
+
+ def __ne__(self, other):
+ return self.total_score != other.total_score
+
+ def __add__(self, other):
+ return self.total_score + other.total_score
+
+ def __sub__(self, other):
+ return self.total_score - other.total_score
+
+class ChessPosition:
+ def __init__(self, chess_position_string):
+ self.chess_position_string = chess_position_string
+ self.white_pieces = []
+ self.black_pieces = []
+ self.board = [];
+ for row in chess_position_string.split("/"):
+ board_row = []
+ for piece in row:
+ if piece.isdigit():
+ board_row.extend([''] * int(piece))
+ else:
+ board_row.append(piece)
+ self.board.append(board_row)
+ self.validate_position()
+ for row in self.board:
+ for piece in row:
+ if piece.isalpha() and piece.isupper():
+ self.white_pieces.append(piece)
+ elif piece.isalpha() and piece.islower():
+ self.black_pieces.append(piece)
+
+ def validate_position(self):
+ white_king_count = 0
+ black_king_count = 0
+ pawn_positions_valid = True
+ for i in range(len(self.board)):
+ for j in range(len(self.board[i])):
+ if self.board[i][j] == 'K':
+ white_king_position = (i, j)
+ white_king_count += 1
+ elif self.board[i][j] == 'k':
+ black_king_position = (i, j)
+ black_king_count += 1
+ elif self.board[i][j] == 'P' or self.board[i][j] == 'p':
+ if i == 0 or i == 7:
+ pawn_positions_valid = False
+
+ if white_king_count != 1 or black_king_count != 1:
+ raise ChessException("kings")
+
+ if abs(white_king_position[0] - black_king_position[0]) <= 1 and abs(white_king_position[1] - black_king_position[1]) <= 1:
+ raise ChessException("kings")
+
+ if not pawn_positions_valid:
+ raise ChessException("pawns")
+
+ def get_white_score(self):
+ return ChessScore(self.white_pieces)
+
+ def get_black_score(self):
+ return ChessScore(self.black_pieces)
+
+ 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_white_score() == self.get_black_score()
+
+ def __str__(self):
+ return self.chess_position_string
+
+ def __len__(self):
+ return len(self.white_pieces) + len(self.black_pieces)
+
+ def __getitem__(self, key):
+ column_idx = ord(key[0]) - ord('A')
+ row_idx = len(self.board) - int(key[1])
+ return self.board[row_idx][column_idx]

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

class ChessException(Exception):
pass
class ChessScore:
piece_scores = {
'p': 1,
'r': 5,
'n': 3,
'b': 3,
'k': 4,
'q': 9
}
def __init__(self, chess_pieces):
self.total_score = 0
for piece in chess_pieces:
self.total_score += ChessScore.piece_scores[piece.lower()]
def __int__(self):
return self.total_score
def __lt__(self, other):
return self.total_score < other.total_score
def __le__(self, other):
return self.total_score <= other.total_score
def __gt__(self, other):
return self.total_score > other.total_score
def __ge__(self, other):
return self.total_score >= other.total_score
def __eq__(self, other):
return self.total_score == other.total_score
def __ne__(self, other):
return self.total_score != other.total_score
def __add__(self, other):
return self.total_score + other.total_score
def __sub__(self, other):
return self.total_score - other.total_score
class ChessPosition:
def __init__(self, chess_position_string):
self.chess_position_string = chess_position_string
self.white_pieces = []
self.black_pieces = []
- self.board = [];
+ self.board = []
for row in chess_position_string.split("/"):
board_row = []
for piece in row:
if piece.isdigit():
board_row.extend([''] * int(piece))
else:
board_row.append(piece)
self.board.append(board_row)
self.validate_position()
for row in self.board:
for piece in row:
if piece.isalpha() and piece.isupper():
self.white_pieces.append(piece)
elif piece.isalpha() and piece.islower():
self.black_pieces.append(piece)
def validate_position(self):
white_king_count = 0
black_king_count = 0
pawn_positions_valid = True
for i in range(len(self.board)):
for j in range(len(self.board[i])):
if self.board[i][j] == 'K':
white_king_position = (i, j)
white_king_count += 1
elif self.board[i][j] == 'k':
black_king_position = (i, j)
black_king_count += 1
elif self.board[i][j] == 'P' or self.board[i][j] == 'p':
if i == 0 or i == 7:
pawn_positions_valid = False
if white_king_count != 1 or black_king_count != 1:
raise ChessException("kings")
if abs(white_king_position[0] - black_king_position[0]) <= 1 and abs(white_king_position[1] - black_king_position[1]) <= 1:
raise ChessException("kings")
if not pawn_positions_valid:
raise ChessException("pawns")
def get_white_score(self):
return ChessScore(self.white_pieces)
def get_black_score(self):
return ChessScore(self.black_pieces)
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_white_score() == self.get_black_score()
def __str__(self):
return self.chess_position_string
def __len__(self):
return len(self.white_pieces) + len(self.black_pieces)
def __getitem__(self, key):
column_idx = ord(key[0]) - ord('A')
row_idx = len(self.board) - int(key[1])
- return self.board[row_idx][column_idx]
+ if self.board[row_idx][column_idx] == '':
+ return None
+ return self.board[row_idx][column_idx]