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

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

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

Резултати

  • 7 точки от тестове
  • 0 бонус точки
  • 7 точки общо
  • 12 успешни тест(а)
  • 5 неуспешни тест(а)

Код

empty_space = "_"
fen_delimiter = "/"
king_excpetion_msg = "kings"
pawns_exception_msg = "pawns"
white_king = "K"
black_king = "k"
pawn = "p"
num_of_pos_per_row = 8
pieces_values = {
"R": 5,
"N": 3,
"B": 3,
"Q": 9,
"K": 4,
"P": 1
}
class ChessException(Exception):
pass
class ChessPosition:
def __init__(self, FEN):
self._original_fen = FEN
self._fen = self._split_fen(FEN)
self._validate_fen()
def __str__(self):
return self._original_fen
def __len__(self):
all_pawns = 0
for row in self._fen:
all_pawns += num_of_pos_per_row - row.count(empty_space)
return all_pawns
def __getitem__(self, index):
col = ord(index[0].upper()) - ord("A") + 1
row = int(index[1]) - 1
char = self._fen[row][-col]
if char == empty_space:
return None
return char
def _validate_fen(self):
if not self._validate_num_of_kings():
raise ChessException(king_excpetion_msg)
if not self._validate_positions_of_kings():
raise ChessException(king_excpetion_msg)
if not self._validate_pawns():
raise ChessException(pawns_exception_msg)
def _validate_pawns(self):
if pawn in self._fen[0].lower() or pawn in self._fen[-1].lower():
return False
return True
def _validate_positions_of_kings(self):
for index, row in enumerate(self._fen):
if black_king in row and white_king in row:
white_king_pos = row.rfind(white_king)
black_king_pos = row.rfind(black_king)
distance = abs(white_king_pos - black_king_pos)
if distance == 1:
return False
if black_king in row.lower() and index + 1 < len(self._fen):
first_king_pos = row.lower().rfind(black_king)
second_king_pos = self._fen[index +
1].lower().rfind(black_king)
if second_king_pos in range(first_king_pos - 1, first_king_pos + 2) and second_king_pos != -1:
return False
return True
def _validate_num_of_kings(self):
black_kings = self._original_fen.count(black_king)
white_kings = self._original_fen.count(white_king)
return white_kings == 1 and black_kings == 1
def _split_fen(self, FEN):
split_fen = FEN.split(fen_delimiter)
for index, element in enumerate(split_fen):
for char in element:
if char.isnumeric():
element = element.replace(char, int(char) * empty_space)
split_fen[index] = element
return split_fen
def get_white_score(self):
score = 0
for key, value in pieces_values.items():
score += self._original_fen.count(key) * value
return score
def get_black_score(self):
score = 0
for key, value in pieces_values.items():
score += self._original_fen.count(key.lower()) * value
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_white_score() == self.get_black_score()
class ChessScore:
def __init__(self, list_of_pawns):
self._score = self._calculate_score(list_of_pawns)
def _calculate_score(self, list_of_pawns):
score = 0
for char in list_of_pawns:
score += pieces_values.get(char.upper(), 0)
return score
def __int__(self):
return self._score
def __lt__(self, other):
return self._score < other.__score
def __le__(self, other):
return self._score <= other.__score
def __eq__(self, other):
return self._score == other.__score
def __ne__(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

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

....FF......FEE..
======================================================================
ERROR: test_basic_arithmetic (test.TestChessScore)
Test additiona and subtraction of ChessScores.
----------------------------------------------------------------------
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
AttributeError: 'ChessScore' object has no attribute '_ChessScore__score'

======================================================================
ERROR: test_comparison (test.TestChessScore)
Test correct comparison on a pair of scores.
----------------------------------------------------------------------
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
AttributeError: 'ChessScore' object has no attribute '_ChessScore__score'

======================================================================
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_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
AssertionError: None != 'K'

======================================================================
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.175s

FAILED (failures=3, errors=2)

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

Кристиан обнови решението на 23.11.2022 15:21 (преди почти 2 години)

+empty_space = "_"
+fen_delimiter = "/"
+king_excpetion_msg = "king"
+pawns_exception_msg = "pawns"
+white_king = "K"
+black_king = "k"
+pawn = "p"
+num_of_pos_per_row = 8
+pawns_values = {
+ "R": 5,
+ "N": 3,
+ "B": 3,
+ "Q": 9,
+ "K": 4,
+ "P": 1
+}
+
+
+class ChessException (Exception):
+ def __init__(self, message):
+ self._message = message
+ super().__init__(self._message)
+
+
+class ChessPosition:
+ def __init__(self, FEN):
+ self.__original_fen = FEN
+ self.__fen = self.__split_fen(FEN)
+ self.__validate_fen()
+
+ def __str__(self):
+ return self.__original_fen
+
+ def __len__(self):
+ all_pawns = 0
+ for row in self.__fen:
+ all_pawns += num_of_pos_per_row - row.count(empty_space)
+ return all_pawns
+
+ def __getitem__(self, index):
+ col = ord(index[0].upper()) - ord("A") + 1
+ row = int(index[1]) - 1
+ char = self.__fen[row][-col]
+ if char == empty_space:
+ return None
+ return char
+
+ def __validate_fen(self):
+ if self.__validate_num_of_kings() == False:
+ raise ChessException(king_excpetion_msg)
+ if self.__validate_positions_of_kings() == False:
+ raise ChessException(king_excpetion_msg)
+ if self.__validate_pawns() == False:
+ raise ChessException(pawns_exception_msg)
+
+ def __validate_pawns(self):
+ if pawn in self.__fen[0].lower() or pawn in self.__fen[-1].lower():
+ return False
+ return True
+
+ def __validate_positions_of_kings(self):
+ for index, row in enumerate(self.__fen):
+ if black_king in row and white_king in row:
+ white_king_pos = row.rfind(white_king)
+ black_king_pos = row.rfind(black_king)
+ distance = abs(white_king_pos - black_king_pos)
+ if distance == 1:
+ return False
+ if black_king in row.lower() and index + 1 < len(self.__fen):
+ first_king_pos = row.lower().rfind(black_king)
+ second_king_pos = self.__fen[index +
+ 1].lower().rfind(black_king)
+ if second_king_pos in range(first_king_pos - 1, first_king_pos + 2) and second_king_pos != -1:
+ return False
+ return True
+
+ def __validate_num_of_kings(self):
+ black_kings = 0
+ white_kings = 0
+ for row in self.__fen:
+ black_kings += row.count(black_king)
+ white_kings += row.count(white_king)
+ return white_kings == 1 and black_kings == 1
+
+ def __split_fen(self, FEN):
+ split_fen = FEN.split(fen_delimiter)
+ for index, element in enumerate(split_fen):
+ for char in element:
+ if char.isnumeric():
+ element = element.replace(char, int(char) * empty_space)
+ split_fen[index] = element
+ return split_fen
+
+ def get_white_score(self):
+ score = 0
+ for row in self.__fen:
+ for char in row:
+ value = pawns_values.get(char)
+ if value:
+ score += value
+ return score
+
+ def get_black_score(self):
+ score = 0
+ for row in self.__fen:
+ for char in row:
+ value = pawns_values.get(char.upper())
+ if value and char.islower():
+ score += value
+ 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_white_score() == self.get_black_score()
+
+class ChessScore:
+ def __init__(self, list_of_pawns):
+ self.__score = self.__calculate_score(list_of_pawns)
+
+ def __calculate_score(self, list_of_pawns):
+ score = 0
+ for char in list_of_pawns:
+ value = pawns_values.get(char.upper())
+ if value:
+ score += value
+ return score
+
+ def __int__(self):
+ return self.__score
+
+ def __lt__(self, other):
+ return self.__score < other.__score
+
+ def __le__(self, other):
+ return self.__score <= other.__score
+
+ def __eq__(self, other):
+ return self.__score == other.__score
+
+ def __ne__(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

Кристиан обнови решението на 23.11.2022 15:23 (преди почти 2 години)

empty_space = "_"
fen_delimiter = "/"
king_excpetion_msg = "king"
pawns_exception_msg = "pawns"
white_king = "K"
black_king = "k"
pawn = "p"
num_of_pos_per_row = 8
pawns_values = {
"R": 5,
"N": 3,
"B": 3,
"Q": 9,
"K": 4,
"P": 1
}
class ChessException (Exception):
def __init__(self, message):
self._message = message
super().__init__(self._message)
class ChessPosition:
def __init__(self, FEN):
self.__original_fen = FEN
self.__fen = self.__split_fen(FEN)
self.__validate_fen()
def __str__(self):
return self.__original_fen
def __len__(self):
all_pawns = 0
for row in self.__fen:
all_pawns += num_of_pos_per_row - row.count(empty_space)
return all_pawns
def __getitem__(self, index):
col = ord(index[0].upper()) - ord("A") + 1
row = int(index[1]) - 1
char = self.__fen[row][-col]
if char == empty_space:
return None
return char
def __validate_fen(self):
if self.__validate_num_of_kings() == False:
raise ChessException(king_excpetion_msg)
if self.__validate_positions_of_kings() == False:
raise ChessException(king_excpetion_msg)
if self.__validate_pawns() == False:
raise ChessException(pawns_exception_msg)
def __validate_pawns(self):
if pawn in self.__fen[0].lower() or pawn in self.__fen[-1].lower():
return False
return True
def __validate_positions_of_kings(self):
for index, row in enumerate(self.__fen):
if black_king in row and white_king in row:
white_king_pos = row.rfind(white_king)
black_king_pos = row.rfind(black_king)
distance = abs(white_king_pos - black_king_pos)
if distance == 1:
return False
if black_king in row.lower() and index + 1 < len(self.__fen):
first_king_pos = row.lower().rfind(black_king)
second_king_pos = self.__fen[index +
1].lower().rfind(black_king)
if second_king_pos in range(first_king_pos - 1, first_king_pos + 2) and second_king_pos != -1:
return False
return True
def __validate_num_of_kings(self):
black_kings = 0
white_kings = 0
for row in self.__fen:
black_kings += row.count(black_king)
white_kings += row.count(white_king)
return white_kings == 1 and black_kings == 1
def __split_fen(self, FEN):
split_fen = FEN.split(fen_delimiter)
for index, element in enumerate(split_fen):
for char in element:
if char.isnumeric():
element = element.replace(char, int(char) * empty_space)
split_fen[index] = element
return split_fen
def get_white_score(self):
score = 0
for row in self.__fen:
for char in row:
value = pawns_values.get(char)
if value:
score += value
return score
def get_black_score(self):
score = 0
for row in self.__fen:
for char in row:
value = pawns_values.get(char.upper())
if value and char.islower():
score += value
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_white_score() == self.get_black_score()
+
class ChessScore:
def __init__(self, list_of_pawns):
self.__score = self.__calculate_score(list_of_pawns)
def __calculate_score(self, list_of_pawns):
score = 0
for char in list_of_pawns:
value = pawns_values.get(char.upper())
if value:
score += value
return score
-
+
def __int__(self):
return self.__score
def __lt__(self, other):
return self.__score < other.__score
def __le__(self, other):
return self.__score <= other.__score
def __eq__(self, other):
return self.__score == other.__score
-
+
def __ne__(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):
+
+ def __sub__(self, other):
return self.__score - other.__score

Кристиан обнови решението на 24.11.2022 09:56 (преди почти 2 години)

empty_space = "_"
fen_delimiter = "/"
-king_excpetion_msg = "king"
+king_excpetion_msg = "kings"
pawns_exception_msg = "pawns"
white_king = "K"
black_king = "k"
pawn = "p"
num_of_pos_per_row = 8
-pawns_values = {
+pieces_values = {
"R": 5,
"N": 3,
"B": 3,
"Q": 9,
"K": 4,
"P": 1
}
-class ChessException (Exception):
- def __init__(self, message):
- self._message = message
- super().__init__(self._message)
+class ChessException(Exception):
+ pass
class ChessPosition:
def __init__(self, FEN):
- self.__original_fen = FEN
- self.__fen = self.__split_fen(FEN)
- self.__validate_fen()
+ self._original_fen = FEN
+ self._fen = self._split_fen(FEN)
+ self._validate_fen()
def __str__(self):
- return self.__original_fen
+ return self._original_fen
def __len__(self):
all_pawns = 0
- for row in self.__fen:
+ for row in self._fen:
all_pawns += num_of_pos_per_row - row.count(empty_space)
return all_pawns
def __getitem__(self, index):
col = ord(index[0].upper()) - ord("A") + 1
row = int(index[1]) - 1
- char = self.__fen[row][-col]
+ char = self._fen[row][-col]
if char == empty_space:
return None
return char
- def __validate_fen(self):
- if self.__validate_num_of_kings() == False:
+ def _validate_fen(self):
+ if not self._validate_num_of_kings():
raise ChessException(king_excpetion_msg)
- if self.__validate_positions_of_kings() == False:
+ if not self._validate_positions_of_kings():
raise ChessException(king_excpetion_msg)
- if self.__validate_pawns() == False:
+ if not self._validate_pawns():
raise ChessException(pawns_exception_msg)
- def __validate_pawns(self):
- if pawn in self.__fen[0].lower() or pawn in self.__fen[-1].lower():
+ def _validate_pawns(self):
+ if pawn in self._fen[0].lower() or pawn in self._fen[-1].lower():
return False
return True
- def __validate_positions_of_kings(self):
- for index, row in enumerate(self.__fen):
+ def _validate_positions_of_kings(self):
+ for index, row in enumerate(self._fen):
if black_king in row and white_king in row:
white_king_pos = row.rfind(white_king)
black_king_pos = row.rfind(black_king)
distance = abs(white_king_pos - black_king_pos)
if distance == 1:
return False
- if black_king in row.lower() and index + 1 < len(self.__fen):
+ if black_king in row.lower() and index + 1 < len(self._fen):
first_king_pos = row.lower().rfind(black_king)
- second_king_pos = self.__fen[index +
- 1].lower().rfind(black_king)
+ second_king_pos = self._fen[index +
+ 1].lower().rfind(black_king)
if second_king_pos in range(first_king_pos - 1, first_king_pos + 2) and second_king_pos != -1:
return False
return True
- def __validate_num_of_kings(self):
- black_kings = 0
- white_kings = 0
- for row in self.__fen:
- black_kings += row.count(black_king)
- white_kings += row.count(white_king)
+ def _validate_num_of_kings(self):
+ black_kings = self._original_fen.count(black_king)
+ white_kings = self._original_fen.count(white_king)
return white_kings == 1 and black_kings == 1
- def __split_fen(self, FEN):
+ def _split_fen(self, FEN):
split_fen = FEN.split(fen_delimiter)
for index, element in enumerate(split_fen):
for char in element:
if char.isnumeric():
element = element.replace(char, int(char) * empty_space)
split_fen[index] = element
return split_fen
def get_white_score(self):
score = 0
- for row in self.__fen:
- for char in row:
- value = pawns_values.get(char)
- if value:
- score += value
+ for key, value in pieces_values.items():
+ score += self._original_fen.count(key) * value
return score
def get_black_score(self):
score = 0
- for row in self.__fen:
- for char in row:
- value = pawns_values.get(char.upper())
- if value and char.islower():
- score += value
+ for key, value in pieces_values.items():
+ score += self._original_fen.count(key.lower()) * value
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_white_score() == self.get_black_score()
class ChessScore:
def __init__(self, list_of_pawns):
- self.__score = self.__calculate_score(list_of_pawns)
+ self._score = self._calculate_score(list_of_pawns)
- def __calculate_score(self, list_of_pawns):
+ def _calculate_score(self, list_of_pawns):
score = 0
for char in list_of_pawns:
- value = pawns_values.get(char.upper())
- if value:
- score += value
+ score += pieces_values.get(char.upper(), 0)
return score
def __int__(self):
- return self.__score
+ return self._score
def __lt__(self, other):
- return self.__score < other.__score
+ return self._score < other.__score
def __le__(self, other):
- return self.__score <= other.__score
+ return self._score <= other.__score
def __eq__(self, other):
- return self.__score == other.__score
+ return self._score == other.__score
def __ne__(self, other):
- return self.__score != other.__score
+ return self._score != other.__score
def __gt__(self, other):
- return self.__score > other.__score
+ return self._score > other.__score
def __ge__(self, other):
- return self.__score >= other.__score
+ return self._score >= other.__score
def __add__(self, other):
- return self.__score + other.__score
+ return self._score + other.__score
def __sub__(self, other):
- return self.__score - other.__score
+ return self._score - other.__score