Решение на Шахматни фенове от Ивана Дончевска

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

Към профила на Ивана Дончевска

Резултати

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

Код

scores = {'p': 1, 'n': 3, 'b': 3, 'k': 4, 'r': 5, 'q': 9,
'P': 1, 'N': 3, 'B': 3, 'K': 4, 'R': 5, 'Q': 9}
positions = {'A': 1, 'B': 2, 'C': 3, 'D': 4, 'E': 5, 'F': 6, 'G': 7, 'H': 8}
class ChessException(Exception):
pass
class ChessScore:
def __init__(self, list_of_figures):
self.list_of_figures = list_of_figures
def __int__(self):
sum_points = 0
for figure in self.list_of_figures:
sum_points += scores[figure]
return sum_points
def __str__(self):
return str(int(self))
def __lt__(self, other):
return int(self) < int(other)
def __gt__(self, other):
return int(self) > int(other)
def __le__(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)
class ChessPosition:
def __init__(self, fen):
self.fen = fen
self.spaces_hard_coded = []
for character in self.fen:
if character.isdigit():
for _ in range(int(character)):
# D stands for digit
self.spaces_hard_coded.append('D')
elif character == '/':
continue
else:
self.spaces_hard_coded.append(character)
self.reversed_fen = list(reversed(self.spaces_hard_coded))
# Check for more or less than one king of each color
if self.fen.count('k') != 1 or self.fen.count('K') != 1:
raise ChessException('kings')
# Check for two kings one to one
if self.spaces_hard_coded.index('k') in [self.spaces_hard_coded.index('K') + 1,
self.spaces_hard_coded.index('K') - 1,
self.spaces_hard_coded.index('K') + 7,
self.spaces_hard_coded.index('K') + 8,
self.spaces_hard_coded.index('K') + 9,
self.spaces_hard_coded.index('K') - 7,
self.spaces_hard_coded.index('K') - 8,
self.spaces_hard_coded.index('K') - 9]:
raise ChessException('kings')
# Check for pawn on line 1 and 8
if 'p' in self.spaces_hard_coded:
if self.spaces_hard_coded.index('p') in range(8) or self.reversed_fen.index('p') in range(8):
raise ChessException('pawns')
if 'P' in self.spaces_hard_coded:
if self.spaces_hard_coded.index('P') in range(8) or self.reversed_fen.index('P') in range(8):
raise ChessException('pawns')
self.white_figures = []
self.black_figures = []
for figure in self.fen:
if ord(figure) in range(65, 91):
self.white_figures.append(figure)
elif ord(figure) in range(97, 123):
self.black_figures.append(figure)
else:
continue
def __str__(self):
return self.fen
def __len__(self):
counter = 0
for character in self.fen:
if character.isalpha():
counter += 1
return counter
def __getitem__(self, index):
col = int(positions[index[0]])
row = int(index[1])
return self.reversed_fen[(8 * row) - col] if self.reversed_fen[(8 * row) - col] != 'D' else None
def get_white_score(self):
return ChessScore(self.white_figures)
def get_black_score(self):
return ChessScore(self.black_figures)
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()

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

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

OK

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

Ивана обнови решението на 28.11.2022 21:01 (преди над 1 година)

+scores = {'p': 1, 'n': 3, 'b': 3, 'k': 4, 'r': 5, 'q': 9}
+positions = {'A': 1, 'B': 2, 'C': 3, 'D': 4, 'E': 5, 'F': 6, 'G': 7, 'H': 8}
+
+
+class ChessException(Exception):
+ pass
+
+
+# list_of_figures is list of strings
+class ChessScore:
+ def __init__(self, list_of_figures):
+ self.list_of_figures = list_of_figures
+
+ def __int__(self):
+ sum_points = 0
+ for figure in self.list_of_figures:
+ sum_points += scores[figure]
+ return sum_points
+
+ def __lt__(self, other):
+ return int(self.list_of_figures) < int(other.list_of_figures)
+
+ def __gt__(self, other):
+ return int(self.list_of_figures) > int(other.list_of_figures)
+
+ def __le__(self, other):
+ return int(self.list_of_figures) <= int(other.list_of_figures)
+
+ # is it needed??
+ def __ge__(self, other):
+ return int(self.list_of_figures) >= int(other.list_of_figures)
+
+ def __eq__(self, other):
+ return int(self.list_of_figures) == int(other.list_of_figures)
+
+ def __ne__(self, other):
+ return int(self.list_of_figures) != int(other.list_of_figures)
+
+ def __add__(self, other):
+ return int(self.list_of_figures) + int(other.list_of_figures)
+
+ def __sub__(self, other):
+ return int(self.list_of_figures) - int(other.list_of_figures)
+
+
+def reverse(string):
+ string = string[::-1]
+ return string
+
+
+class ChessPosition:
+ def __init__(self, fen):
+ self.fen = fen
+ self.spaces_hard_coded = []
+ for character in self.fen:
+ if character.isdigit():
+ for _ in range(int(character)):
+ # D stands for digit
+ self.spaces_hard_coded.append('D')
+ elif character == '/':
+ continue
+ else:
+ self.spaces_hard_coded.append(character)
+ self.reversed_fen = reverse(self.spaces_hard_coded)
+ # Check for more or less than one king of each color
+ if self.fen.count('k') != 1 or self.fen.count('K') != 1:
+ raise ChessException(f'kings')
+ # Check for two kings one to one
+ if self.spaces_hard_coded.index('k') in [self.spaces_hard_coded.index('K') + 1,
+ self.spaces_hard_coded.index('K') - 1,
+ self.spaces_hard_coded.index('K') + 7,
+ self.spaces_hard_coded.index('K') + 8,
+ self.spaces_hard_coded.index('K') + 9,
+ self.spaces_hard_coded.index('K') - 7,
+ self.spaces_hard_coded.index('K') - 8,
+ self.spaces_hard_coded.index('K') - 9]:
+ raise ChessException(f'kings')
+ # Check for pawn on line 1 and 8
+ if self.spaces_hard_coded.index('p') in range(8) or self.spaces_hard_coded.index('P') in range(8):
+ raise ChessException(f'pawns')
+ elif self.reversed_fen.index('p') in range(8) or self.reversed_fen.index('P') in range(8):
+ raise ChessException(f'pawns')
+
+ self.white_figures = []
+ self.black_figures = []
+ for figure in self.fen:
+ if figure.upper():
+ self.white_figures.append(figure)
+ elif figure.lower():
+ self.black_figures.append(figure)
+ else:
+ continue
+
+ def __str__(self):
+ return self.fen
+
+ def __len__(self):
+ counter = 0
+ for character in self.fen:
+ if character.isalpha():
+ counter += 1
+ return counter
+
+ def __getitem__(self, index):
+ col = int(positions[index[0]])
+ row = int(index[1])
+ return self.reversed_fen[(8 * row) - col] if self.reversed_fen[(8 * row) - col] != 'D' else None
+
+ def get_white_score(self):
+ return ChessScore(self.white_figures)
+
+ def get_black_score(self):
+ return ChessScore(self.black_figures)
+
+ 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()
+

Ивана обнови решението на 28.11.2022 21:03 (преди над 1 година)

scores = {'p': 1, 'n': 3, 'b': 3, 'k': 4, 'r': 5, 'q': 9}
positions = {'A': 1, 'B': 2, 'C': 3, 'D': 4, 'E': 5, 'F': 6, 'G': 7, 'H': 8}
class ChessException(Exception):
pass
# list_of_figures is list of strings

Мястото на този коментар е неуместно. По-удачно е да е в тялото на инициализатора. Като цяло не мисля, че коментарът е нужен, но ако настояваш, по-добре го сложи там.

class ChessScore:
def __init__(self, list_of_figures):
self.list_of_figures = list_of_figures
def __int__(self):
sum_points = 0
for figure in self.list_of_figures:
sum_points += scores[figure]
return sum_points
def __lt__(self, other):
return int(self.list_of_figures) < int(other.list_of_figures)
def __gt__(self, other):
return int(self.list_of_figures) > int(other.list_of_figures)
def __le__(self, other):
return int(self.list_of_figures) <= int(other.list_of_figures)
# is it needed??
def __ge__(self, other):
return int(self.list_of_figures) >= int(other.list_of_figures)
def __eq__(self, other):
return int(self.list_of_figures) == int(other.list_of_figures)
def __ne__(self, other):
return int(self.list_of_figures) != int(other.list_of_figures)
def __add__(self, other):
return int(self.list_of_figures) + int(other.list_of_figures)
def __sub__(self, other):
return int(self.list_of_figures) - int(other.list_of_figures)
def reverse(string):
string = string[::-1]
return string
class ChessPosition:
def __init__(self, fen):
self.fen = fen
self.spaces_hard_coded = []
for character in self.fen:
if character.isdigit():
for _ in range(int(character)):
# D stands for digit
self.spaces_hard_coded.append('D')
elif character == '/':
continue
else:
self.spaces_hard_coded.append(character)
self.reversed_fen = reverse(self.spaces_hard_coded)
# Check for more or less than one king of each color
if self.fen.count('k') != 1 or self.fen.count('K') != 1:
- raise ChessException(f'kings')
+ raise ChessException('kings')
# Check for two kings one to one
if self.spaces_hard_coded.index('k') in [self.spaces_hard_coded.index('K') + 1,
self.spaces_hard_coded.index('K') - 1,
self.spaces_hard_coded.index('K') + 7,
self.spaces_hard_coded.index('K') + 8,
self.spaces_hard_coded.index('K') + 9,
self.spaces_hard_coded.index('K') - 7,
self.spaces_hard_coded.index('K') - 8,
self.spaces_hard_coded.index('K') - 9]:
- raise ChessException(f'kings')
+ raise ChessException('kings')
# Check for pawn on line 1 and 8
if self.spaces_hard_coded.index('p') in range(8) or self.spaces_hard_coded.index('P') in range(8):
- raise ChessException(f'pawns')
+ raise ChessException('pawns')
elif self.reversed_fen.index('p') in range(8) or self.reversed_fen.index('P') in range(8):
- raise ChessException(f'pawns')
+ raise ChessException('pawns')
self.white_figures = []
self.black_figures = []
for figure in self.fen:
if figure.upper():
self.white_figures.append(figure)
elif figure.lower():
self.black_figures.append(figure)
else:
continue
def __str__(self):
return self.fen
def __len__(self):
counter = 0
for character in self.fen:
if character.isalpha():
counter += 1
return counter
def __getitem__(self, index):
col = int(positions[index[0]])
row = int(index[1])
return self.reversed_fen[(8 * row) - col] if self.reversed_fen[(8 * row) - col] != 'D' else None
def get_white_score(self):
return ChessScore(self.white_figures)
def get_black_score(self):
return ChessScore(self.black_figures)
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()

Ивана обнови решението на 29.11.2022 12:19 (преди над 1 година)

scores = {'p': 1, 'n': 3, 'b': 3, 'k': 4, 'r': 5, 'q': 9}
positions = {'A': 1, 'B': 2, 'C': 3, 'D': 4, 'E': 5, 'F': 6, 'G': 7, 'H': 8}
class ChessException(Exception):
pass
-# list_of_figures is list of strings
class ChessScore:
def __init__(self, list_of_figures):
self.list_of_figures = list_of_figures
def __int__(self):
sum_points = 0
for figure in self.list_of_figures:
sum_points += scores[figure]
return sum_points
def __lt__(self, other):
return int(self.list_of_figures) < int(other.list_of_figures)
def __gt__(self, other):
return int(self.list_of_figures) > int(other.list_of_figures)
def __le__(self, other):
return int(self.list_of_figures) <= int(other.list_of_figures)
- # is it needed??
def __ge__(self, other):
return int(self.list_of_figures) >= int(other.list_of_figures)
def __eq__(self, other):
return int(self.list_of_figures) == int(other.list_of_figures)
def __ne__(self, other):
return int(self.list_of_figures) != int(other.list_of_figures)
def __add__(self, other):
return int(self.list_of_figures) + int(other.list_of_figures)
def __sub__(self, other):
return int(self.list_of_figures) - int(other.list_of_figures)
-def reverse(string):
- string = string[::-1]
- return string
-
-
class ChessPosition:
def __init__(self, fen):
self.fen = fen
self.spaces_hard_coded = []
for character in self.fen:
if character.isdigit():
for _ in range(int(character)):
# D stands for digit
self.spaces_hard_coded.append('D')
elif character == '/':
continue
else:
self.spaces_hard_coded.append(character)
- self.reversed_fen = reverse(self.spaces_hard_coded)
+ self.reversed_fen = list(reversed(self.spaces_hard_coded))
+ print(self.reversed_fen)
# Check for more or less than one king of each color
if self.fen.count('k') != 1 or self.fen.count('K') != 1:
raise ChessException('kings')
# Check for two kings one to one
if self.spaces_hard_coded.index('k') in [self.spaces_hard_coded.index('K') + 1,
self.spaces_hard_coded.index('K') - 1,
self.spaces_hard_coded.index('K') + 7,
self.spaces_hard_coded.index('K') + 8,
self.spaces_hard_coded.index('K') + 9,
self.spaces_hard_coded.index('K') - 7,
self.spaces_hard_coded.index('K') - 8,
self.spaces_hard_coded.index('K') - 9]:
raise ChessException('kings')
# Check for pawn on line 1 and 8
- if self.spaces_hard_coded.index('p') in range(8) or self.spaces_hard_coded.index('P') in range(8):
- raise ChessException('pawns')
- elif self.reversed_fen.index('p') in range(8) or self.reversed_fen.index('P') in range(8):
- raise ChessException('pawns')
+ if 'p' in self.spaces_hard_coded:
+ if self.spaces_hard_coded.index('p') in range(8) or self.reversed_fen.index('p') in range(8):
+ raise ChessException('pawns')
+ if 'P' in self.spaces_hard_coded:
+ if self.spaces_hard_coded.index('P') in range(8) or self.reversed_fen.index('P') in range(8):
+ raise ChessException('pawns')
self.white_figures = []
self.black_figures = []
for figure in self.fen:
if figure.upper():
self.white_figures.append(figure)
elif figure.lower():
self.black_figures.append(figure)
else:
continue
def __str__(self):
return self.fen
def __len__(self):
counter = 0
for character in self.fen:
if character.isalpha():
counter += 1
return counter
def __getitem__(self, index):
col = int(positions[index[0]])
row = int(index[1])
return self.reversed_fen[(8 * row) - col] if self.reversed_fen[(8 * row) - col] != 'D' else None
def get_white_score(self):
return ChessScore(self.white_figures)
def get_black_score(self):
return ChessScore(self.black_figures)
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()

Ивана обнови решението на 29.11.2022 12:21 (преди над 1 година)

scores = {'p': 1, 'n': 3, 'b': 3, 'k': 4, 'r': 5, 'q': 9}
positions = {'A': 1, 'B': 2, 'C': 3, 'D': 4, 'E': 5, 'F': 6, 'G': 7, 'H': 8}
class ChessException(Exception):
pass
class ChessScore:
def __init__(self, list_of_figures):
self.list_of_figures = list_of_figures
def __int__(self):
sum_points = 0
for figure in self.list_of_figures:
sum_points += scores[figure]
return sum_points
def __lt__(self, other):
return int(self.list_of_figures) < int(other.list_of_figures)
def __gt__(self, other):
return int(self.list_of_figures) > int(other.list_of_figures)
def __le__(self, other):
return int(self.list_of_figures) <= int(other.list_of_figures)
def __ge__(self, other):
return int(self.list_of_figures) >= int(other.list_of_figures)
def __eq__(self, other):
return int(self.list_of_figures) == int(other.list_of_figures)
def __ne__(self, other):
return int(self.list_of_figures) != int(other.list_of_figures)
def __add__(self, other):
return int(self.list_of_figures) + int(other.list_of_figures)
def __sub__(self, other):
return int(self.list_of_figures) - int(other.list_of_figures)
class ChessPosition:
def __init__(self, fen):
self.fen = fen
self.spaces_hard_coded = []
for character in self.fen:
if character.isdigit():
for _ in range(int(character)):
# D stands for digit
self.spaces_hard_coded.append('D')
elif character == '/':
continue
else:
self.spaces_hard_coded.append(character)
self.reversed_fen = list(reversed(self.spaces_hard_coded))
- print(self.reversed_fen)
# Check for more or less than one king of each color
if self.fen.count('k') != 1 or self.fen.count('K') != 1:
raise ChessException('kings')
# Check for two kings one to one
if self.spaces_hard_coded.index('k') in [self.spaces_hard_coded.index('K') + 1,
self.spaces_hard_coded.index('K') - 1,
self.spaces_hard_coded.index('K') + 7,
self.spaces_hard_coded.index('K') + 8,
self.spaces_hard_coded.index('K') + 9,
self.spaces_hard_coded.index('K') - 7,
self.spaces_hard_coded.index('K') - 8,
self.spaces_hard_coded.index('K') - 9]:
raise ChessException('kings')
# Check for pawn on line 1 and 8
if 'p' in self.spaces_hard_coded:
if self.spaces_hard_coded.index('p') in range(8) or self.reversed_fen.index('p') in range(8):
raise ChessException('pawns')
if 'P' in self.spaces_hard_coded:
if self.spaces_hard_coded.index('P') in range(8) or self.reversed_fen.index('P') in range(8):
raise ChessException('pawns')
self.white_figures = []
self.black_figures = []
for figure in self.fen:
if figure.upper():
self.white_figures.append(figure)
elif figure.lower():
self.black_figures.append(figure)
else:
continue
def __str__(self):
return self.fen
def __len__(self):
counter = 0
for character in self.fen:
if character.isalpha():
counter += 1
return counter
def __getitem__(self, index):
col = int(positions[index[0]])
row = int(index[1])
return self.reversed_fen[(8 * row) - col] if self.reversed_fen[(8 * row) - col] != 'D' else None
def get_white_score(self):
return ChessScore(self.white_figures)
def get_black_score(self):
return ChessScore(self.black_figures)
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()

Ивана обнови решението на 29.11.2022 16:33 (преди над 1 година)

-scores = {'p': 1, 'n': 3, 'b': 3, 'k': 4, 'r': 5, 'q': 9}
+scores = {'p': 1, 'n': 3, 'b': 3, 'k': 4, 'r': 5, 'q': 9,
+ 'P': 1, 'N': 3, 'B': 3, 'K': 4, 'R': 5, 'Q': 9}
positions = {'A': 1, 'B': 2, 'C': 3, 'D': 4, 'E': 5, 'F': 6, 'G': 7, 'H': 8}
class ChessException(Exception):
pass
class ChessScore:
def __init__(self, list_of_figures):
self.list_of_figures = list_of_figures
def __int__(self):
sum_points = 0
for figure in self.list_of_figures:
sum_points += scores[figure]
return sum_points
+ def __str__(self):
+ return str(int(self))
+
def __lt__(self, other):
- return int(self.list_of_figures) < int(other.list_of_figures)
+ return int(self) < int(other)
def __gt__(self, other):
- return int(self.list_of_figures) > int(other.list_of_figures)
+ return int(self) > int(other)
def __le__(self, other):
- return int(self.list_of_figures) <= int(other.list_of_figures)
+ return int(self) <= int(other)
def __ge__(self, other):
- return int(self.list_of_figures) >= int(other.list_of_figures)
+ return int(self) >= int(other)
def __eq__(self, other):
- return int(self.list_of_figures) == int(other.list_of_figures)
+ return int(self) == int(other)
def __ne__(self, other):
- return int(self.list_of_figures) != int(other.list_of_figures)
+ return int(self) != int(other)
def __add__(self, other):
- return int(self.list_of_figures) + int(other.list_of_figures)
+ return int(self) + int(other)
def __sub__(self, other):
- return int(self.list_of_figures) - int(other.list_of_figures)
+ return int(self) - int(other)
class ChessPosition:
def __init__(self, fen):
self.fen = fen
self.spaces_hard_coded = []
for character in self.fen:
if character.isdigit():
for _ in range(int(character)):
# D stands for digit
self.spaces_hard_coded.append('D')
elif character == '/':
continue
else:
self.spaces_hard_coded.append(character)
self.reversed_fen = list(reversed(self.spaces_hard_coded))
# Check for more or less than one king of each color
if self.fen.count('k') != 1 or self.fen.count('K') != 1:
raise ChessException('kings')
# Check for two kings one to one
if self.spaces_hard_coded.index('k') in [self.spaces_hard_coded.index('K') + 1,
self.spaces_hard_coded.index('K') - 1,
self.spaces_hard_coded.index('K') + 7,
self.spaces_hard_coded.index('K') + 8,
self.spaces_hard_coded.index('K') + 9,
self.spaces_hard_coded.index('K') - 7,
self.spaces_hard_coded.index('K') - 8,
self.spaces_hard_coded.index('K') - 9]:
raise ChessException('kings')
# Check for pawn on line 1 and 8
if 'p' in self.spaces_hard_coded:
if self.spaces_hard_coded.index('p') in range(8) or self.reversed_fen.index('p') in range(8):
raise ChessException('pawns')
if 'P' in self.spaces_hard_coded:
if self.spaces_hard_coded.index('P') in range(8) or self.reversed_fen.index('P') in range(8):
raise ChessException('pawns')
self.white_figures = []
self.black_figures = []
for figure in self.fen:
- if figure.upper():
+ if ord(figure) in range(65, 91):
self.white_figures.append(figure)
- elif figure.lower():
+ elif ord(figure) in range(97, 123):
self.black_figures.append(figure)
else:
continue
def __str__(self):
return self.fen
def __len__(self):
counter = 0
for character in self.fen:
if character.isalpha():
counter += 1
return counter
def __getitem__(self, index):
col = int(positions[index[0]])
row = int(index[1])
return self.reversed_fen[(8 * row) - col] if self.reversed_fen[(8 * row) - col] != 'D' else None
def get_white_score(self):
return ChessScore(self.white_figures)
def get_black_score(self):
return ChessScore(self.black_figures)
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()