Ивана обнови решението на 28.11.2022 21:01 (преди почти 2 години)
+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()
+