Решение на Шахматни фенове от Цветелина Чакърова

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

Към профила на Цветелина Чакърова

Резултати

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

Код

class ChessException(Exception):
pass
class ChessPosition:
_chess_board = {}
_figures = 'rnbqkp'
def fen_to_chessboard(self):
current_row = 8
current_column = 'A'
for position in self._fen:
if position in self._figures + self._figures.upper():
self._chess_board[(current_row, current_column)] = position
current_column = chr(ord(current_column) + 1)
elif position == '/':
current_row -= 1
current_column = 'A'
else:
numer_of_empty_positions = int(position)
for number in range(numer_of_empty_positions):
self._chess_board[(current_row, current_column)] = None
current_column = chr(ord(current_column) + 1)
def validate_right_kings(self):
white_kings_count = 0
black_kings_count = 0
for key, value in self._chess_board.items():
if value == 'K':
white_kings_count += 1
elif value == 'k':
black_kings_count += 1
if white_kings_count != 1 or black_kings_count != 1:
raise ChessException('kings')
def validate_kings_positions(self):
is_white_king_found = False
is_black_king_found = False
for key, value in self._chess_board.items():
if value == 'K':
is_white_king_found = True
white_king_position = key
elif value == 'k':
is_black_king_found = True
black_king_position = key
if is_white_king_found and is_black_king_found:
break
if abs(white_king_position[0] - black_king_position[0]) == 1 or \
abs(ord(white_king_position[1]) - ord(black_king_position[1])) == 1:
raise ChessException('kings')
def validate_pawns(self):
for key, value in self._chess_board.items():
if key[0] in (1, 8) and value in ('P', 'p'):
raise ChessException('pawns')
def __init__(self, fen):
self._fen = fen
self.fen_to_chessboard()
self.validate_right_kings()
self.validate_kings_positions()
self.validate_pawns()
self._white_score = []
self._black_score = []
self._number_of_figures = 0
for value in self._chess_board.values():
if value is not None:
self._number_of_figures += 1
if value in self._figures.upper():
self._white_score.append(value.lower())
if value in self._figures:
self._black_score.append(value.lower())
def get_white_score(self):
return ChessScore(self._white_score)
def get_black_score(self):
return ChessScore(self._black_score)
def white_is_winning(self):
return int(self.get_white_score()) > int(self.get_black_score())
def black_is_winning(self):
return int(self.get_white_score()) < int(self.get_black_score())
def is_equal(self):
return int(self.get_white_score()) == int(self.get_black_score())
def __str__(self):
return self._fen
def __len__(self):
return self._number_of_figures
def __getitem__(self, item):
return self._chess_board[int(item[1]), item[0]]
class ChessScore:
_scores = {'r': 5, 'n': 3, 'b': 3, 'q': 9, 'k': 4, 'p': 1}
def __init__(self, pieces):
self._pieces = pieces
self._score = 0
for piece in self._pieces:
self._score += self._scores[piece]
def __int__(self):
return self._score
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 __add__(self, other):
return int(self) + int(other)
def __sub__(self, other):
return int(self) - int(other)

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

..EEE.E.E...E....
======================================================================
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
solution.ChessException: kings

======================================================================
ERROR: test_get_black_score (test.TestChessPosition)
Test get_black_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
solution.ChessException: kings

======================================================================
ERROR: 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
solution.ChessException: kings

======================================================================
ERROR: test_is_equal (test.TestChessPosition)
Test is_equal.
----------------------------------------------------------------------
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
solution.ChessException: kings

======================================================================
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
solution.ChessException: kings

======================================================================
ERROR: 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
solution.ChessException: kings

----------------------------------------------------------------------
Ran 17 tests in 0.167s

FAILED (errors=6)

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

Цветелина обнови решението на 24.11.2022 14:11 (преди над 1 година)

+class ChessException(Exception):
+ pass
+
+
+def fen_to_chessboard(fen):
+ chess_board = {}
+ current_row = 8
+ current_column = 'A'
+ position_at_current_row = 1
+ for position in fen:
+ if position in ('R', 'r', 'N', 'n', 'B', 'b', 'Q', 'q', 'K', 'k', 'P', 'p'):
+ chess_board[(current_row, current_column)] = position
+ current_column = chr(ord(current_column) + 1)
+ elif position == '/':
+ current_row -= 1
+ current_column = 'A'
+ else:
+ numer_of_empty_positions = ord(position) - ord('0')
+ for number in range(numer_of_empty_positions):
+ chess_board[(current_row, current_column)] = 'None'

Не е добра идея да пазиш този None като стринг. В крайна сметка по-логично е да е просто None, а и това е стойността, която се очаква по условие.

+ current_column = chr(ord(current_column) + 1)
+ return chess_board
+
+
+def validate_kings(chess_board):
+ white_kings_count = 0
+ black_kings_count = 0
+ is_king_found = False
+ for key, value in chess_board.items():
+ if value in ('K', 'k'):
+ if not is_king_found:

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

+ found_king_position = key
+ found_king = value
+ is_king_found = True
+ if value == 'K':
+ white_kings_count += 1
+ else:
+ black_kings_count += 1
+
+ if white_kings_count != 1 or black_kings_count != 1:
+ raise ChessException('kings')
+
+ king_to_search = 'K' if found_king == 'k' else 'k'
+ x = found_king_position[0]
+ y = found_king_position[1]
+ if ((x + 1, chr(ord(y) - 1)) in chess_board and chess_board[(x + 1, chr(ord(y) - 1))] == king_to_search) or\

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

+ ((x + 1, chr(ord(y))) in chess_board and chess_board[(x + 1, chr(ord(y)))] == king_to_search) or\
+ ((x + 1, chr(ord(y) + 1)) in chess_board and chess_board[(x + 1, chr(ord(y) - 1))] == king_to_search) or\
+ ((x, chr(ord(y) - 1)) in chess_board and chess_board[(x, chr(ord(y) - 1))] == king_to_search) or\
+ ((x, chr(ord(y) + 1)) in chess_board and chess_board[(x, chr(ord(y) + 1))] == king_to_search) or\
+ ((x - 1, chr(ord(y) - 1)) in chess_board and chess_board[(x - 1, chr(ord(y) - 1))] == king_to_search) or\
+ ((x - 1, chr(ord(y))) in chess_board and chess_board[(x - 1, chr(ord(y)))] == king_to_search) or \
+ ((x - 1, chr(ord(y) + 1)) in chess_board and chess_board[(x - 1, chr(ord(y) + 1))] == king_to_search):
+ raise ChessException('kings')
+
+
+def validate_pawns(chess_board):
+ for row in (1, 8):
+ for column in ('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'):

Ако обхождаш оригиналния fen, няма да е нужно да дефинираш колоните експлицитно. Просто обхождаш ред 1 и ред 8.
То и тук можеш да си го спестиш, ако обходиш всички values() за дадения речник.

+ if chess_board[(row, column)] in ('P', 'p'):
+ raise ChessException('pawns')
+
+
+class ChessPosition:
+ _chess_board = {}
+
+ def __init__(self, fen):
+ self._fen = fen
+ self._chess_board = fen_to_chessboard(self._fen)
+ validate_kings(self._chess_board)
+ validate_pawns(self._chess_board)
+
+ def get_white_score(self):
+ white_pieces = []
+ for value in self._chess_board.values():
+ if value in ('R', 'N', 'B', 'Q', 'K', 'P'):
+ white_pieces.append(value.lower())
+ return ChessScore(white_pieces)
+
+ def get_black_score(self):
+ black_pieces = []
+ for value in self._chess_board.values():
+ if value in ('r', 'n', 'b', 'q', 'k', 'p'):
+ black_pieces.append(value)
+ return ChessScore(black_pieces)
+
+ def white_is_winning(self):
+ return int(self.get_white_score() > self.get_black_score())
+
+ def black_is_winning(self):
+ return int(self.get_white_score() < self.get_black_score())
+
+ def is_equal(self):
+ return int(self.get_white_score() == self.get_black_score())
+
+ def __str__(self):
+ return self._fen
+
+ def __len__(self):
+ number_of_figures = 0
+ for value in self._chess_board.values():
+ if value != 'None':
+ number_of_figures += 1
+ return number_of_figures
+
+ def __getitem__(self, item):
+ return self._chess_board[ord(item[1]) - ord('0'), item[0]]
+
+
+class ChessScore:
+
+ _scores = {'r': 5, 'n': 3, 'b': 3, 'q': 9, 'k': 4, 'p': 1}
+
+ def __init__(self, pieces):
+ self._pieces = pieces
+
+ def __int__(self):
+ score = 0
+ for piece in self._pieces:
+ score += self._scores[piece]
+ return score
+
+ 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 __add__(self, other):
+ return int(self) + int(other)
+
+ def __sub__(self, other):
+ return int(self) - int(other)

Цветелина обнови решението на 26.11.2022 18:39 (преди над 1 година)

class ChessException(Exception):
pass
-def fen_to_chessboard(fen):
- chess_board = {}
- current_row = 8
- current_column = 'A'
- position_at_current_row = 1
- for position in fen:
- if position in ('R', 'r', 'N', 'n', 'B', 'b', 'Q', 'q', 'K', 'k', 'P', 'p'):
- chess_board[(current_row, current_column)] = position
- current_column = chr(ord(current_column) + 1)
- elif position == '/':
- current_row -= 1
- current_column = 'A'
- else:
- numer_of_empty_positions = ord(position) - ord('0')
- for number in range(numer_of_empty_positions):
- chess_board[(current_row, current_column)] = 'None'
- current_column = chr(ord(current_column) + 1)
- return chess_board
+class ChessPosition:
+ _chess_board = {}
+ _figures = 'rnbqkp'
-
-def validate_kings(chess_board):
- white_kings_count = 0
- black_kings_count = 0
- is_king_found = False
- for key, value in chess_board.items():
- if value in ('K', 'k'):
- if not is_king_found:
- found_king_position = key
- found_king = value
- is_king_found = True
- if value == 'K':
- white_kings_count += 1
+ def fen_to_chessboard(self):
+ current_row = 8
+ current_column = 'A'
+ position_at_current_row = 1
+ for position in self._fen:
+ if position in self._figures + self._figures.upper():
+ self._chess_board[(current_row, current_column)] = position
+ current_column = chr(ord(current_column) + 1)
+ elif position == '/':
+ current_row -= 1
+ current_column = 'A'
else:
- black_kings_count += 1
+ numer_of_empty_positions = int(position)
+ for number in range(numer_of_empty_positions):
+ self._chess_board[(current_row, current_column)] = None
+ current_column = chr(ord(current_column) + 1)
- if white_kings_count != 1 or black_kings_count != 1:
- raise ChessException('kings')
+ def validate_kings(self):
+ white_kings_count = 0
+ black_kings_count = 0
+ is_king_found = False
+ for key, value in self._chess_board.items():
+ if value in ('K', 'k'):
+ if not is_king_found:
+ found_king_position = key
+ found_king = value
+ is_king_found = True
+ if value == 'K':
+ white_kings_count += 1
+ else:
+ black_kings_count += 1
- king_to_search = 'K' if found_king == 'k' else 'k'
- x = found_king_position[0]
- y = found_king_position[1]
- if ((x + 1, chr(ord(y) - 1)) in chess_board and chess_board[(x + 1, chr(ord(y) - 1))] == king_to_search) or\
- ((x + 1, chr(ord(y))) in chess_board and chess_board[(x + 1, chr(ord(y)))] == king_to_search) or\
- ((x + 1, chr(ord(y) + 1)) in chess_board and chess_board[(x + 1, chr(ord(y) - 1))] == king_to_search) or\
- ((x, chr(ord(y) - 1)) in chess_board and chess_board[(x, chr(ord(y) - 1))] == king_to_search) or\
- ((x, chr(ord(y) + 1)) in chess_board and chess_board[(x, chr(ord(y) + 1))] == king_to_search) or\
- ((x - 1, chr(ord(y) - 1)) in chess_board and chess_board[(x - 1, chr(ord(y) - 1))] == king_to_search) or\
- ((x - 1, chr(ord(y))) in chess_board and chess_board[(x - 1, chr(ord(y)))] == king_to_search) or \
- ((x - 1, chr(ord(y) + 1)) in chess_board and chess_board[(x - 1, chr(ord(y) + 1))] == king_to_search):
- raise ChessException('kings')
+ if white_kings_count != 1 or black_kings_count != 1:
+ raise ChessException('kings')
+ king_to_search = 'K' if found_king == 'k' else 'k'
+ x = found_king_position[0]
+ y = found_king_position[1]
+ if ((x + 1, chr(ord(y) - 1)) in self._chess_board and self._chess_board[(x + 1, chr(ord(y) - 1))] == king_to_search) or \
+ ((x + 1, chr(ord(y))) in self._chess_board and self._chess_board[(x + 1, chr(ord(y)))] == king_to_search) or \
+ ((x + 1, chr(ord(y) + 1)) in self._chess_board and self._chess_board[(x + 1, chr(ord(y) - 1))] == king_to_search) or \
+ ((x, chr(ord(y) - 1)) in self._chess_board and self._chess_board[(x, chr(ord(y) - 1))] == king_to_search) or \
+ ((x, chr(ord(y) + 1)) in self._chess_board and self._chess_board[(x, chr(ord(y) + 1))] == king_to_search) or \
+ ((x - 1, chr(ord(y) - 1)) in self._chess_board and self._chess_board[(x - 1, chr(ord(y) - 1))] == king_to_search) or \
+ ((x - 1, chr(ord(y))) in self._chess_board and self._chess_board[(x - 1, chr(ord(y)))] == king_to_search) or \
+ ((x - 1, chr(ord(y) + 1)) in self._chess_board and self._chess_board[(x - 1, chr(ord(y) + 1))] == king_to_search):
+ raise ChessException('kings')
-def validate_pawns(chess_board):
- for row in (1, 8):
- for column in ('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'):
- if chess_board[(row, column)] in ('P', 'p'):
- raise ChessException('pawns')
+ def validate_pawns(self):
+ for row in (1, 8):
+ for column in ('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'):
+ if self._chess_board[(row, column)] in ('P', 'p'):
+ raise ChessException('pawns')
-
-class ChessPosition:
- _chess_board = {}
-
def __init__(self, fen):
self._fen = fen
- self._chess_board = fen_to_chessboard(self._fen)
- validate_kings(self._chess_board)
- validate_pawns(self._chess_board)
+ self.fen_to_chessboard()
+ self.validate_kings()
+ self.validate_pawns()
def get_white_score(self):
white_pieces = []
for value in self._chess_board.values():
- if value in ('R', 'N', 'B', 'Q', 'K', 'P'):
+ if value is not None and value in self._figures.upper():
white_pieces.append(value.lower())
return ChessScore(white_pieces)
def get_black_score(self):
black_pieces = []
for value in self._chess_board.values():
- if value in ('r', 'n', 'b', 'q', 'k', 'p'):
+ if value is not None and value in self._figures:
black_pieces.append(value)
return ChessScore(black_pieces)
def white_is_winning(self):
- return int(self.get_white_score() > self.get_black_score())
+ return int(self.get_white_score()) > int(self.get_black_score())
def black_is_winning(self):
- return int(self.get_white_score() < self.get_black_score())
+ return int(self.get_white_score()) < int(self.get_black_score())
def is_equal(self):
- return int(self.get_white_score() == self.get_black_score())
+ return int(self.get_white_score()) == int(self.get_black_score())
def __str__(self):
return self._fen
def __len__(self):
number_of_figures = 0
for value in self._chess_board.values():
- if value != 'None':
+ if value is not None:
number_of_figures += 1
return number_of_figures
def __getitem__(self, item):
- return self._chess_board[ord(item[1]) - ord('0'), item[0]]
+ return self._chess_board[int(item[1]), item[0]]
class ChessScore:
_scores = {'r': 5, 'n': 3, 'b': 3, 'q': 9, 'k': 4, 'p': 1}
def __init__(self, pieces):
self._pieces = pieces
def __int__(self):
score = 0
for piece in self._pieces:
score += self._scores[piece]
return score
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 __add__(self, other):
return int(self) + int(other)
def __sub__(self, other):
return int(self) - int(other)
+

Цветелина обнови решението на 26.11.2022 18:49 (преди над 1 година)

class ChessException(Exception):
pass
class ChessPosition:
_chess_board = {}
_figures = 'rnbqkp'
def fen_to_chessboard(self):
current_row = 8
current_column = 'A'
position_at_current_row = 1
for position in self._fen:
if position in self._figures + self._figures.upper():
self._chess_board[(current_row, current_column)] = position
current_column = chr(ord(current_column) + 1)
elif position == '/':
current_row -= 1
current_column = 'A'
else:
numer_of_empty_positions = int(position)
for number in range(numer_of_empty_positions):
self._chess_board[(current_row, current_column)] = None
current_column = chr(ord(current_column) + 1)
def validate_kings(self):
white_kings_count = 0
black_kings_count = 0
is_king_found = False
for key, value in self._chess_board.items():
if value in ('K', 'k'):
if not is_king_found:
found_king_position = key
found_king = value
is_king_found = True
if value == 'K':
white_kings_count += 1
else:
black_kings_count += 1
if white_kings_count != 1 or black_kings_count != 1:
raise ChessException('kings')
king_to_search = 'K' if found_king == 'k' else 'k'
x = found_king_position[0]
y = found_king_position[1]
if ((x + 1, chr(ord(y) - 1)) in self._chess_board and self._chess_board[(x + 1, chr(ord(y) - 1))] == king_to_search) or \
((x + 1, chr(ord(y))) in self._chess_board and self._chess_board[(x + 1, chr(ord(y)))] == king_to_search) or \
((x + 1, chr(ord(y) + 1)) in self._chess_board and self._chess_board[(x + 1, chr(ord(y) - 1))] == king_to_search) or \
((x, chr(ord(y) - 1)) in self._chess_board and self._chess_board[(x, chr(ord(y) - 1))] == king_to_search) or \
((x, chr(ord(y) + 1)) in self._chess_board and self._chess_board[(x, chr(ord(y) + 1))] == king_to_search) or \
((x - 1, chr(ord(y) - 1)) in self._chess_board and self._chess_board[(x - 1, chr(ord(y) - 1))] == king_to_search) or \
((x - 1, chr(ord(y))) in self._chess_board and self._chess_board[(x - 1, chr(ord(y)))] == king_to_search) or \
((x - 1, chr(ord(y) + 1)) in self._chess_board and self._chess_board[(x - 1, chr(ord(y) + 1))] == king_to_search):
raise ChessException('kings')
def validate_pawns(self):
for row in (1, 8):
for column in ('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'):
if self._chess_board[(row, column)] in ('P', 'p'):
raise ChessException('pawns')
def __init__(self, fen):
self._fen = fen
self.fen_to_chessboard()
self.validate_kings()
self.validate_pawns()
- def get_white_score(self):
- white_pieces = []
+ self._white_score = []
+ self._black_score = []
+ self._number_of_figures = 0
for value in self._chess_board.values():
- if value is not None and value in self._figures.upper():
- white_pieces.append(value.lower())
- return ChessScore(white_pieces)
+ if value is not None:
+ self._number_of_figures += 1
+ if value in self._figures.upper():
+ self._white_score.append(value.lower())
+ if value in self._figures:
+ self._black_score.append(value.lower())
+ def get_white_score(self):
+ return ChessScore(self._white_score)
+
def get_black_score(self):
- black_pieces = []
- for value in self._chess_board.values():
- if value is not None and value in self._figures:
- black_pieces.append(value)
- return ChessScore(black_pieces)
+ return ChessScore(self._black_score)
def white_is_winning(self):
return int(self.get_white_score()) > int(self.get_black_score())
def black_is_winning(self):
return int(self.get_white_score()) < int(self.get_black_score())
def is_equal(self):
return int(self.get_white_score()) == int(self.get_black_score())
def __str__(self):
return self._fen
def __len__(self):
- number_of_figures = 0
- for value in self._chess_board.values():
- if value is not None:
- number_of_figures += 1
- return number_of_figures
+ return self._number_of_figures
def __getitem__(self, item):
return self._chess_board[int(item[1]), item[0]]
class ChessScore:
_scores = {'r': 5, 'n': 3, 'b': 3, 'q': 9, 'k': 4, 'p': 1}
def __init__(self, pieces):
self._pieces = pieces
def __int__(self):
score = 0
for piece in self._pieces:
score += self._scores[piece]
return score
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 __add__(self, other):
return int(self) + int(other)
def __sub__(self, other):
return int(self) - int(other)
-

Цветелина обнови решението на 26.11.2022 19:08 (преди над 1 година)

class ChessException(Exception):
pass
class ChessPosition:
_chess_board = {}
_figures = 'rnbqkp'
def fen_to_chessboard(self):
current_row = 8
current_column = 'A'
position_at_current_row = 1
for position in self._fen:
if position in self._figures + self._figures.upper():
self._chess_board[(current_row, current_column)] = position
current_column = chr(ord(current_column) + 1)
elif position == '/':
current_row -= 1
current_column = 'A'
else:
numer_of_empty_positions = int(position)
for number in range(numer_of_empty_positions):
self._chess_board[(current_row, current_column)] = None
current_column = chr(ord(current_column) + 1)
- def validate_kings(self):
+ def validate_right_kings(self):
white_kings_count = 0
black_kings_count = 0
+ for key, value in self._chess_board.items():
+ if value == 'K':
+ white_kings_count += 1
+ elif value == 'k':
+ black_kings_count += 1
+
+ if white_kings_count != 1 or black_kings_count != 1:
+ raise ChessException('kings')
+
+ def validate_kings_positions(self):
is_king_found = False
for key, value in self._chess_board.items():
if value in ('K', 'k'):
if not is_king_found:
found_king_position = key
found_king = value
is_king_found = True
- if value == 'K':
- white_kings_count += 1
- else:
- black_kings_count += 1
- if white_kings_count != 1 or black_kings_count != 1:
- raise ChessException('kings')
-
king_to_search = 'K' if found_king == 'k' else 'k'
x = found_king_position[0]
y = found_king_position[1]
if ((x + 1, chr(ord(y) - 1)) in self._chess_board and self._chess_board[(x + 1, chr(ord(y) - 1))] == king_to_search) or \
((x + 1, chr(ord(y))) in self._chess_board and self._chess_board[(x + 1, chr(ord(y)))] == king_to_search) or \
((x + 1, chr(ord(y) + 1)) in self._chess_board and self._chess_board[(x + 1, chr(ord(y) - 1))] == king_to_search) or \
((x, chr(ord(y) - 1)) in self._chess_board and self._chess_board[(x, chr(ord(y) - 1))] == king_to_search) or \
((x, chr(ord(y) + 1)) in self._chess_board and self._chess_board[(x, chr(ord(y) + 1))] == king_to_search) or \
((x - 1, chr(ord(y) - 1)) in self._chess_board and self._chess_board[(x - 1, chr(ord(y) - 1))] == king_to_search) or \
((x - 1, chr(ord(y))) in self._chess_board and self._chess_board[(x - 1, chr(ord(y)))] == king_to_search) or \
((x - 1, chr(ord(y) + 1)) in self._chess_board and self._chess_board[(x - 1, chr(ord(y) + 1))] == king_to_search):
raise ChessException('kings')
def validate_pawns(self):
for row in (1, 8):
for column in ('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'):
if self._chess_board[(row, column)] in ('P', 'p'):
raise ChessException('pawns')
def __init__(self, fen):
self._fen = fen
self.fen_to_chessboard()
- self.validate_kings()
+ self.validate_right_kings()
+ self.validate_kings_positions()
self.validate_pawns()
-
self._white_score = []
self._black_score = []
self._number_of_figures = 0
for value in self._chess_board.values():
if value is not None:
self._number_of_figures += 1
if value in self._figures.upper():
self._white_score.append(value.lower())
if value in self._figures:
self._black_score.append(value.lower())
def get_white_score(self):
return ChessScore(self._white_score)
def get_black_score(self):
return ChessScore(self._black_score)
def white_is_winning(self):
return int(self.get_white_score()) > int(self.get_black_score())
def black_is_winning(self):
return int(self.get_white_score()) < int(self.get_black_score())
def is_equal(self):
return int(self.get_white_score()) == int(self.get_black_score())
def __str__(self):
return self._fen
def __len__(self):
return self._number_of_figures
def __getitem__(self, item):
return self._chess_board[int(item[1]), item[0]]
class ChessScore:
_scores = {'r': 5, 'n': 3, 'b': 3, 'q': 9, 'k': 4, 'p': 1}
def __init__(self, pieces):
self._pieces = pieces
def __int__(self):
score = 0
for piece in self._pieces:
score += self._scores[piece]
return score
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 __add__(self, other):
return int(self) + int(other)
def __sub__(self, other):
return int(self) - int(other)

Цветелина обнови решението на 26.11.2022 19:20 (преди над 1 година)

class ChessException(Exception):
pass
class ChessPosition:
_chess_board = {}
_figures = 'rnbqkp'
def fen_to_chessboard(self):
current_row = 8
current_column = 'A'
position_at_current_row = 1
for position in self._fen:
if position in self._figures + self._figures.upper():
self._chess_board[(current_row, current_column)] = position
current_column = chr(ord(current_column) + 1)
elif position == '/':
current_row -= 1
current_column = 'A'
else:
numer_of_empty_positions = int(position)
for number in range(numer_of_empty_positions):
self._chess_board[(current_row, current_column)] = None
current_column = chr(ord(current_column) + 1)
def validate_right_kings(self):
white_kings_count = 0
black_kings_count = 0
for key, value in self._chess_board.items():
if value == 'K':
white_kings_count += 1
elif value == 'k':
black_kings_count += 1
if white_kings_count != 1 or black_kings_count != 1:
raise ChessException('kings')
def validate_kings_positions(self):
is_king_found = False
for key, value in self._chess_board.items():
if value in ('K', 'k'):
if not is_king_found:
found_king_position = key
found_king = value
is_king_found = True
king_to_search = 'K' if found_king == 'k' else 'k'
x = found_king_position[0]
y = found_king_position[1]
if ((x + 1, chr(ord(y) - 1)) in self._chess_board and self._chess_board[(x + 1, chr(ord(y) - 1))] == king_to_search) or \
((x + 1, chr(ord(y))) in self._chess_board and self._chess_board[(x + 1, chr(ord(y)))] == king_to_search) or \
((x + 1, chr(ord(y) + 1)) in self._chess_board and self._chess_board[(x + 1, chr(ord(y) - 1))] == king_to_search) or \
((x, chr(ord(y) - 1)) in self._chess_board and self._chess_board[(x, chr(ord(y) - 1))] == king_to_search) or \
((x, chr(ord(y) + 1)) in self._chess_board and self._chess_board[(x, chr(ord(y) + 1))] == king_to_search) or \
((x - 1, chr(ord(y) - 1)) in self._chess_board and self._chess_board[(x - 1, chr(ord(y) - 1))] == king_to_search) or \
((x - 1, chr(ord(y))) in self._chess_board and self._chess_board[(x - 1, chr(ord(y)))] == king_to_search) or \
((x - 1, chr(ord(y) + 1)) in self._chess_board and self._chess_board[(x - 1, chr(ord(y) + 1))] == king_to_search):
raise ChessException('kings')
def validate_pawns(self):
- for row in (1, 8):
- for column in ('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'):
- if self._chess_board[(row, column)] in ('P', 'p'):
- raise ChessException('pawns')
+ for key, value in self._chess_board.items():
+ if key[0] in (1, 8) and value in ('P', 'p'):
+ raise ChessException('pawns')
def __init__(self, fen):
self._fen = fen
self.fen_to_chessboard()
self.validate_right_kings()
self.validate_kings_positions()
self.validate_pawns()
self._white_score = []
self._black_score = []
self._number_of_figures = 0
for value in self._chess_board.values():
if value is not None:
self._number_of_figures += 1
if value in self._figures.upper():
self._white_score.append(value.lower())
if value in self._figures:
self._black_score.append(value.lower())
def get_white_score(self):
return ChessScore(self._white_score)
def get_black_score(self):
return ChessScore(self._black_score)
def white_is_winning(self):
return int(self.get_white_score()) > int(self.get_black_score())
def black_is_winning(self):
return int(self.get_white_score()) < int(self.get_black_score())
def is_equal(self):
return int(self.get_white_score()) == int(self.get_black_score())
def __str__(self):
return self._fen
def __len__(self):
return self._number_of_figures
def __getitem__(self, item):
return self._chess_board[int(item[1]), item[0]]
class ChessScore:
_scores = {'r': 5, 'n': 3, 'b': 3, 'q': 9, 'k': 4, 'p': 1}
def __init__(self, pieces):
self._pieces = pieces
def __int__(self):
score = 0
for piece in self._pieces:
score += self._scores[piece]
return score
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 __add__(self, other):
return int(self) + int(other)
def __sub__(self, other):
return int(self) - int(other)
+

Цветелина обнови решението на 26.11.2022 19:26 (преди над 1 година)

class ChessException(Exception):
pass
class ChessPosition:
_chess_board = {}
_figures = 'rnbqkp'
def fen_to_chessboard(self):
current_row = 8
current_column = 'A'
position_at_current_row = 1
for position in self._fen:
if position in self._figures + self._figures.upper():
self._chess_board[(current_row, current_column)] = position
current_column = chr(ord(current_column) + 1)
elif position == '/':
current_row -= 1
current_column = 'A'
else:
numer_of_empty_positions = int(position)
for number in range(numer_of_empty_positions):
self._chess_board[(current_row, current_column)] = None
current_column = chr(ord(current_column) + 1)
def validate_right_kings(self):
white_kings_count = 0
black_kings_count = 0
for key, value in self._chess_board.items():
if value == 'K':
white_kings_count += 1
elif value == 'k':
black_kings_count += 1
if white_kings_count != 1 or black_kings_count != 1:
raise ChessException('kings')
def validate_kings_positions(self):
is_king_found = False
for key, value in self._chess_board.items():
if value in ('K', 'k'):
if not is_king_found:
found_king_position = key
found_king = value
is_king_found = True
king_to_search = 'K' if found_king == 'k' else 'k'
x = found_king_position[0]
y = found_king_position[1]
if ((x + 1, chr(ord(y) - 1)) in self._chess_board and self._chess_board[(x + 1, chr(ord(y) - 1))] == king_to_search) or \
((x + 1, chr(ord(y))) in self._chess_board and self._chess_board[(x + 1, chr(ord(y)))] == king_to_search) or \
((x + 1, chr(ord(y) + 1)) in self._chess_board and self._chess_board[(x + 1, chr(ord(y) - 1))] == king_to_search) or \
((x, chr(ord(y) - 1)) in self._chess_board and self._chess_board[(x, chr(ord(y) - 1))] == king_to_search) or \
((x, chr(ord(y) + 1)) in self._chess_board and self._chess_board[(x, chr(ord(y) + 1))] == king_to_search) or \
((x - 1, chr(ord(y) - 1)) in self._chess_board and self._chess_board[(x - 1, chr(ord(y) - 1))] == king_to_search) or \
((x - 1, chr(ord(y))) in self._chess_board and self._chess_board[(x - 1, chr(ord(y)))] == king_to_search) or \
((x - 1, chr(ord(y) + 1)) in self._chess_board and self._chess_board[(x - 1, chr(ord(y) + 1))] == king_to_search):
raise ChessException('kings')
def validate_pawns(self):
for key, value in self._chess_board.items():
if key[0] in (1, 8) and value in ('P', 'p'):
raise ChessException('pawns')
def __init__(self, fen):
self._fen = fen
self.fen_to_chessboard()
self.validate_right_kings()
self.validate_kings_positions()
self.validate_pawns()
self._white_score = []
self._black_score = []
self._number_of_figures = 0
for value in self._chess_board.values():
if value is not None:
self._number_of_figures += 1
if value in self._figures.upper():
self._white_score.append(value.lower())
if value in self._figures:
self._black_score.append(value.lower())
def get_white_score(self):
return ChessScore(self._white_score)
def get_black_score(self):
return ChessScore(self._black_score)
def white_is_winning(self):
return int(self.get_white_score()) > int(self.get_black_score())
def black_is_winning(self):
return int(self.get_white_score()) < int(self.get_black_score())
def is_equal(self):
return int(self.get_white_score()) == int(self.get_black_score())
def __str__(self):
return self._fen
def __len__(self):
return self._number_of_figures
def __getitem__(self, item):
return self._chess_board[int(item[1]), item[0]]
class ChessScore:
-
_scores = {'r': 5, 'n': 3, 'b': 3, 'q': 9, 'k': 4, 'p': 1}
def __init__(self, pieces):
self._pieces = pieces
+ self._score = 0
+ for piece in self._pieces:
+ self._score += self._scores[piece]
def __int__(self):
- score = 0
- for piece in self._pieces:
- score += self._scores[piece]
- return score
+ return self._score
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 __add__(self, other):
return int(self) + int(other)
def __sub__(self, other):
return int(self) - int(other)
-

Цветелина обнови решението на 27.11.2022 12:06 (преди над 1 година)

class ChessException(Exception):
pass
class ChessPosition:
_chess_board = {}
_figures = 'rnbqkp'
def fen_to_chessboard(self):
current_row = 8
current_column = 'A'
- position_at_current_row = 1
for position in self._fen:
if position in self._figures + self._figures.upper():
self._chess_board[(current_row, current_column)] = position
current_column = chr(ord(current_column) + 1)
elif position == '/':
current_row -= 1
current_column = 'A'
else:
numer_of_empty_positions = int(position)
for number in range(numer_of_empty_positions):
self._chess_board[(current_row, current_column)] = None
current_column = chr(ord(current_column) + 1)
def validate_right_kings(self):
white_kings_count = 0
black_kings_count = 0
for key, value in self._chess_board.items():
if value == 'K':
white_kings_count += 1
elif value == 'k':
black_kings_count += 1
if white_kings_count != 1 or black_kings_count != 1:
raise ChessException('kings')
def validate_kings_positions(self):
- is_king_found = False
+ is_white_king_found = False
+ is_black_king_found = False
for key, value in self._chess_board.items():
- if value in ('K', 'k'):
- if not is_king_found:
- found_king_position = key
- found_king = value
- is_king_found = True
+ if value == 'K':
+ is_white_king_found = True
+ white_king_position = key
+ elif value == 'k':
+ is_black_king_found = True
+ black_king_position = key
+ if is_white_king_found and is_black_king_found:
+ break
- king_to_search = 'K' if found_king == 'k' else 'k'
- x = found_king_position[0]
- y = found_king_position[1]
- if ((x + 1, chr(ord(y) - 1)) in self._chess_board and self._chess_board[(x + 1, chr(ord(y) - 1))] == king_to_search) or \
- ((x + 1, chr(ord(y))) in self._chess_board and self._chess_board[(x + 1, chr(ord(y)))] == king_to_search) or \
- ((x + 1, chr(ord(y) + 1)) in self._chess_board and self._chess_board[(x + 1, chr(ord(y) - 1))] == king_to_search) or \
- ((x, chr(ord(y) - 1)) in self._chess_board and self._chess_board[(x, chr(ord(y) - 1))] == king_to_search) or \
- ((x, chr(ord(y) + 1)) in self._chess_board and self._chess_board[(x, chr(ord(y) + 1))] == king_to_search) or \
- ((x - 1, chr(ord(y) - 1)) in self._chess_board and self._chess_board[(x - 1, chr(ord(y) - 1))] == king_to_search) or \
- ((x - 1, chr(ord(y))) in self._chess_board and self._chess_board[(x - 1, chr(ord(y)))] == king_to_search) or \
- ((x - 1, chr(ord(y) + 1)) in self._chess_board and self._chess_board[(x - 1, chr(ord(y) + 1))] == king_to_search):
+ if abs(white_king_position[0] - black_king_position[0]) == 1 or \
+ abs(ord(white_king_position[1]) - ord(black_king_position[1])) == 1:
raise ChessException('kings')
def validate_pawns(self):
for key, value in self._chess_board.items():
if key[0] in (1, 8) and value in ('P', 'p'):
raise ChessException('pawns')
def __init__(self, fen):
self._fen = fen
self.fen_to_chessboard()
self.validate_right_kings()
self.validate_kings_positions()
self.validate_pawns()
self._white_score = []
self._black_score = []
self._number_of_figures = 0
for value in self._chess_board.values():
if value is not None:
self._number_of_figures += 1
if value in self._figures.upper():
self._white_score.append(value.lower())
if value in self._figures:
self._black_score.append(value.lower())
def get_white_score(self):
return ChessScore(self._white_score)
def get_black_score(self):
return ChessScore(self._black_score)
def white_is_winning(self):
return int(self.get_white_score()) > int(self.get_black_score())
def black_is_winning(self):
return int(self.get_white_score()) < int(self.get_black_score())
def is_equal(self):
return int(self.get_white_score()) == int(self.get_black_score())
def __str__(self):
return self._fen
def __len__(self):
return self._number_of_figures
def __getitem__(self, item):
return self._chess_board[int(item[1]), item[0]]
class ChessScore:
_scores = {'r': 5, 'n': 3, 'b': 3, 'q': 9, 'k': 4, 'p': 1}
def __init__(self, pieces):
self._pieces = pieces
self._score = 0
for piece in self._pieces:
self._score += self._scores[piece]
def __int__(self):
return self._score
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 __add__(self, other):
return int(self) + int(other)
def __sub__(self, other):
return int(self) - int(other)