Решение на Шахматни фенове от Даниела Дишлянова

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

Към профила на Даниела Дишлянова

Резултати

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

Код

from itertools import repeat
class ChessException(Exception):
pass
class ChessPosition:
_all_white_pieces = 'RNBQKP'
_all_black_pieces = 'rnbqkp'
def __init__(self, FEN_position):
self.FEN = FEN_position
self._white_pieces = []
self._black_pieces = []
self._generate_board()
self._check_FEN()
def __str__(self):
return self.FEN
def __len__(self):
all_pieces = 0
for row_pieces in self.board:
all_pieces += len([piece for piece in row_pieces if piece != '-'])
return all_pieces
def __getitem__(self, position):
column = ord(position[0].upper()) - ord('A') if position != '' else 0
row = ord(position[1]) - ord('1') if position != '' else 0
return self.board[row][column] if self.board[row][column] != '-' else None
def _check_FEN(self):
kings = 'Kk'
pawns = 'Pp'
found_pieces = {'K': 0, 'k': 0, pawns: 0}
for row, row_pieces in enumerate(self.board):
for column, piece in enumerate(row_pieces):
if piece in kings:

Би могла да спестиш малко код, ако просто вземеш позицията на двата царя и изчислиш дължината на вектора между тях, но тъй като сме в края на срока за домашното, не го прави, за да не рискуваш грешки в последния момент. Просто препоръка за следващи подобни задачи.

'''next to in row'''
if 1 <= column and self.board[row][column - 1] in kings:
raise ChessException('kings')
if column <= 6 and self.board[row][column + 1] in kings:
raise ChessException('kings')
'''diagonal below'''
if 1 <= row and 1 <= column and self.board[row - 1][column - 1] in kings:
raise ChessException('kings')
if 1 <= row and column <= 6 and self.board[row - 1][column + 1] in kings:
raise ChessException('kings')
'''diagonal above'''
if row <= 6 and 1 <= column and self.board[row + 1][column - 1] in kings:
raise ChessException('kings')
if row <= 6 and column <= 6 and self.board[row + 1][column + 1] in kings:
raise ChessException('kings')
'''bolow'''
if 1 <= row and self.board[row - 1][column] in kings:
raise ChessException('kings')
'''above'''
if row <= 6 and self.board[row + 1][column] in kings:
raise ChessException('kings')
found_pieces[piece] += 1
if piece in pawns and row in (0, 7):
found_pieces[pawns] += 1
if found_pieces['K'] != 1 or found_pieces['k'] != 1:
raise ChessException('kings')
if found_pieces[pawns] != 0:
raise ChessException('pawns')
def _generate_board(self):
self.board = []
for row in reversed(self.FEN.split('/')):
column = []
for piece in row:
if piece in self._all_white_pieces + self._all_black_pieces:
column.append(piece)
self._white_pieces.append(piece) if piece in self._all_white_pieces else self._black_pieces.append(piece)
elif piece in '12345678':
column.extend(list(repeat('-', int(piece))))
self.board.append(column)
def get_white_score(self):
return ChessScore(list(map(str.lower, 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_white_score() < self.get_black_score()
def is_equal(self):
return self.get_white_score() == self.get_black_score()
class ChessScore:
def __init__(self, pieces):
self._pieces = pieces
def __int__(self):
pieces_to_points = {
'r': 5,
'n': 3,
'b': 3,
'q': 9,
'k': 4,
'p': 1
}
score = 0
for piece in self._pieces:
score += pieces_to_points[piece]
return score
def __lt__(self, other):
return int(self) < int(other)
def __le__(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)
def __eq__(self, other):
return int(self) == int(other)
def __ne__(self, other):
return int(self) != int(other)
def __add__(self, other):
return int(self) + int(other)
def __sub__(self, other):
return int(self) - int(other)

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

.................
----------------------------------------------------------------------
Ran 17 tests in 0.165s

OK

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

Даниела обнови решението на 28.11.2022 17:05 (преди над 1 година)

+from itertools import repeat
+
+class ChessException(Exception):
+ def __init__(self, message):
+ self._message = message
+ super().__init__(self._message)
+
+
+class ChessPosition:
+ _all_white_pieces = 'RNBQKP'
+ _all_black_pieces = 'rnbqkp'
+
+ def __init__(self, FEN_position):
+ self.FEN = FEN_position
+ self._white_pieces = []
+ self._black_pieces = []
+ self._generate_board()
+ self._check_FEN()
+
+ def __str__(self):
+ return self.FEN
+
+ def __len__(self):
+ all_pieces = 0
+ for row_pieces in self.board:
+ all_pieces += len([piece for piece in row_pieces if piece != '-'])
+ return all_pieces
+
+ def __getitem__(self, position):
+ column = ord(position[0].upper()) - ord('A') if position != '' else 0
+ row = ord(position[1]) - ord('1') if position != '' else 0
+ return self.board[row][column] if self.board[row][column] != '-' else None
+
+ def _check_FEN(self):
+ kings = 'Kk'
+ pawns = 'Pp'
+ found_pieces = {'K' : 0, 'k' : 0, pawns : 0}
+ for row, row_pieces in enumerate(self.board):
+ for column, piece in enumerate(row_pieces):
+ if piece in kings:

Би могла да спестиш малко код, ако просто вземеш позицията на двата царя и изчислиш дължината на вектора между тях, но тъй като сме в края на срока за домашното, не го прави, за да не рискуваш грешки в последния момент. Просто препоръка за следващи подобни задачи.

+ '''next to in row'''
+ if 1 <= column and self.board[row][column - 1] in kings:
+ raise ChessException('kings')
+ if column <= 6 and self.board[row][column + 1] in kings:
+ raise ChessException('kings')
+ '''diagonal below'''
+ if 1 <= row and 1 <= column and self.board[row - 1][column - 1] in kings:
+ raise ChessException('kings')
+ if 1 <= row and column <= 6 and self.board[row - 1][column + 1] in kings:
+ raise ChessException('kings')
+ '''diagonal above'''
+ if row <= 6 and 1 <= column and self.board[row + 1][column - 1] in kings:
+ raise ChessException('kings')
+ if row <= 6 and column <= 6 and self.board[row + 1][column + 1] in kings:
+ raise ChessException('kings')
+ '''bolow'''
+ if 1 <= row and self.board[row - 1][column] in kings:
+ raise ChessException('kings')
+ '''above'''
+ if row <= 6 and self.board[row + 1][column] in kings:
+ raise ChessException('kings')
+ found_pieces[piece] += 1
+ if piece in pawns and row in (0, 7):
+ found_pieces[pawns] += 1
+ if found_pieces['K'] != 1 or found_pieces['k'] != 1:
+ raise ChessException('kings')
+ if found_pieces[pawns] != 0:
+ raise ChessException('pawns')
+
+ def _generate_board(self):
+ self.board = []
+ for row in reversed(self.FEN.split('/')):
+ column = []
+ for piece in row:
+ if piece in self._all_white_pieces + self._all_black_pieces:
+ column.append(piece)
+ self._white_pieces.append(piece) if piece in self._all_white_pieces else self._black_pieces.append(piece)
+ elif piece in '12345678':
+ column.extend(list(repeat('-', int(piece))))
+ self.board.append(column)
+
+ def get_white_score(self):
+ return ChessScore(list(map(lambda x : x.lower(), 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_white_score() < self.get_black_score()
+
+ def is_equal(self):
+ return self.get_white_score() == self.get_black_score()
+
+
+class ChessScore:
+ def __init__(self, pieces):
+ self._pieces = pieces
+
+ def __int__(self):
+ pieces_to_points = {
+ 'r' : 5,
+ 'n' : 3,
+ 'b' : 3,
+ 'q' : 9,
+ 'k' : 4,
+ 'p' : 1
+ }
+ score = 0
+ for piece in self._pieces:
+ score += pieces_to_points[piece]
+ return score
+
+ def __lt__(self, other):
+ return int(self) < int(other)
+
+ def __le__(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)
+
+ def __eq__(self, other):
+ return int(self) == int(other)
+
+ def __ne__(self, other):
+ return int(self) != int(other)
+
+ def __add__(self, other):
+ return int(self) + int(other)
+
+ def __sub__(self, other):
+ return int(self) - int(other)

Даниела обнови решението на 29.11.2022 12:49 (преди над 1 година)

from itertools import repeat
class ChessException(Exception):
- def __init__(self, message):
- self._message = message
- super().__init__(self._message)
+ pass
class ChessPosition:
_all_white_pieces = 'RNBQKP'
_all_black_pieces = 'rnbqkp'
def __init__(self, FEN_position):
self.FEN = FEN_position
self._white_pieces = []
self._black_pieces = []
self._generate_board()
self._check_FEN()
def __str__(self):
return self.FEN
def __len__(self):
all_pieces = 0
for row_pieces in self.board:
all_pieces += len([piece for piece in row_pieces if piece != '-'])
return all_pieces
def __getitem__(self, position):
column = ord(position[0].upper()) - ord('A') if position != '' else 0
row = ord(position[1]) - ord('1') if position != '' else 0
return self.board[row][column] if self.board[row][column] != '-' else None
def _check_FEN(self):
kings = 'Kk'
pawns = 'Pp'
- found_pieces = {'K' : 0, 'k' : 0, pawns : 0}
+ found_pieces = {'K': 0, 'k': 0, pawns: 0}
for row, row_pieces in enumerate(self.board):
for column, piece in enumerate(row_pieces):
if piece in kings:
'''next to in row'''
if 1 <= column and self.board[row][column - 1] in kings:
raise ChessException('kings')
if column <= 6 and self.board[row][column + 1] in kings:
raise ChessException('kings')
'''diagonal below'''
if 1 <= row and 1 <= column and self.board[row - 1][column - 1] in kings:
raise ChessException('kings')
if 1 <= row and column <= 6 and self.board[row - 1][column + 1] in kings:
raise ChessException('kings')
'''diagonal above'''
if row <= 6 and 1 <= column and self.board[row + 1][column - 1] in kings:
raise ChessException('kings')
if row <= 6 and column <= 6 and self.board[row + 1][column + 1] in kings:
raise ChessException('kings')
'''bolow'''
if 1 <= row and self.board[row - 1][column] in kings:
raise ChessException('kings')
'''above'''
if row <= 6 and self.board[row + 1][column] in kings:
raise ChessException('kings')
found_pieces[piece] += 1
if piece in pawns and row in (0, 7):
found_pieces[pawns] += 1
if found_pieces['K'] != 1 or found_pieces['k'] != 1:
raise ChessException('kings')
if found_pieces[pawns] != 0:
raise ChessException('pawns')
def _generate_board(self):
self.board = []
for row in reversed(self.FEN.split('/')):
column = []
for piece in row:
if piece in self._all_white_pieces + self._all_black_pieces:
column.append(piece)
self._white_pieces.append(piece) if piece in self._all_white_pieces else self._black_pieces.append(piece)
elif piece in '12345678':
column.extend(list(repeat('-', int(piece))))
- self.board.append(column)
+ self.board.append(column)
def get_white_score(self):
- return ChessScore(list(map(lambda x : x.lower(), self._white_pieces)))
+ return ChessScore(list(map(str.lower, 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_white_score() < self.get_black_score()
def is_equal(self):
return self.get_white_score() == self.get_black_score()
class ChessScore:
def __init__(self, pieces):
self._pieces = pieces
def __int__(self):
pieces_to_points = {
- 'r' : 5,
- 'n' : 3,
- 'b' : 3,
- 'q' : 9,
- 'k' : 4,
- 'p' : 1
+ 'r': 5,
+ 'n': 3,
+ 'b': 3,
+ 'q': 9,
+ 'k': 4,
+ 'p': 1
}
score = 0
for piece in self._pieces:
score += pieces_to_points[piece]
return score
def __lt__(self, other):
return int(self) < int(other)
def __le__(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)
def __eq__(self, other):
return int(self) == int(other)
def __ne__(self, other):
return int(self) != int(other)
def __add__(self, other):
return int(self) + int(other)
def __sub__(self, other):
return int(self) - int(other)