Иван обнови решението на 29.11.2022 00:01 (преди почти 2 години)
+# A note from the "developer": 2 parts of the code are not finished. I will finish them tomorrow
Не обещавам, че ще погледна пак, обаче, за повече фийдбак :)
+# Uploading this intermediate version just so that I can receive comments on the other parts of the code.
+# Thanks :)
+
+import re
+
+class ChessException(Exception):
+ pass
+
+class ChessPosition:
+ def __init__(self, FEN):
+ self._rows = FEN.split("/")
+ self._rows.reverse()
+
+ # Checking for kings doesn't yet work as I was too tired to implement it. Will upload a new version tomorrow.
+
+ if FEN == "" or FEN is None:
+ raise Exception("FEN string is empty")
+
+ if FEN.count('K') != 1 or FEN.count('k') != 1:
+ raise ChessException("kings")
+
+ pawn_check = re.compile(r'[Pp]')
+ if pawn_check.search(self._rows[0]) or pawn_check.search(self._rows[7]):
+ raise ChessException("pawns")
+
+ self._FEN = FEN
+
+ def __str__(self):
+ return f"{self._FEN}"
+
+ def __len__(self):
+ length_regex = re.compile(r'[RrNnBbQqKkPp]')
+ return len(re.findall(length_regex, self._FEN))
+
+ def __getitem__(self, key):
+ # Doesn't properly work yet. Will upload a new version tomorrow.
+ letter = ord(key[0]) - 65
+ number = ord(key[1]) - 49
+ result = self._rows[number][letter]
+ return result
+
+ def get_white_score(self):
+ regex = re.compile(r'[RNBQKP]')
+ whites = re.findall(regex, self._FEN)
+ cs = ChessScore(whites)
+ return int(cs)
+
+ def get_black_score(self):
+ regex = re.compile(r'[rnbqkp]')
+ blacks = re.findall(regex, self._FEN)
+ cs = ChessScore(blacks)
+ return int(cs)
Спрямо инструкциите на задачата, методи от този тип се очаква да върнат ChessScore
обект.
+
+ def white_is_winning(self):
+ white_score = self.get_white_score()
+ black_score = self.get_black_score()
+ if (white_score > black_score):
return white_score > black_score
+ return True
+ return False
+
+ def black_is_winning(self):
+ white_score = self.get_white_score()
+ black_score = self.get_black_score()
+ if (white_score < black_score):
+ return True
+ return False
+
+ def is_equal(self):
+ white_score = self.get_white_score()
+ black_score = self.get_black_score()
+ if (white_score == black_score):
+ return True
+ return False
+
+class ChessScore:
+ def __init__(self, letters):
+
+ self._list = [char.lower() for char in letters]
+
+ result = 0
+ for figure in self._list:
+ score = 0
+ match figure:
+ case 'r': score = 5
+ case 'n': score = 3
+ case 'b': score = 3
+ case 'k': score = 4
+ case 'q': score = 9
+ case 'p': score = 1
+ case '_': raise TypeError("Unexpected figure encountered.")
+ result += score
+ self._result = result
+
+ def __int__(self):
+ return self._result
+
+ def __add__(self, other):
+ return self._result + other._result
+
+ def __sub__(self, other):
+ return self._result - other._result
+
+ def __lt__(self, other):
+ return self._result < other._result
+
+ def __gt__(self, other):
+ return self._result > other._result
+
+ def __le__(self, other):
+ return self._result <= other._result
+
+ def __ge__(self, other):
+ return self._result >= other._result
+
+ def __eq__(self, other):
+ return self._result == other._result
+
+ def __ne__(self, other):
+ return self._result != other._result
+
+cp = ChessPosition("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR")
+print(cp)
+#print(len(cp))
+#print(cp["E2"])
+print(cp.get_white_score())
+print(cp.get_black_score())
+print(cp.white_is_winning())
+print(cp.black_is_winning())
+print(cp.is_equal())
+
+score1 = ChessScore(['r', 'n', 'b', 'k', 'q', 'p'])
+score2 = ChessScore(['r', 'b'])
+#print(int(score1))
+#print(int(score2))
+#print(score1 + score2)
+#print(score1 - score2)