Решение на Шахматни фенове от Ива Гочева

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

Към профила на Ива Гочева

Резултати

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

Код

import re
class ChessException(Exception):
def __init__(self, message='chess error occured'):
super().__init__(message)
class ChessPosition:
def __init__(self, fen):
self.__validateKings(fen, 'K')
self.__validateKings(fen, 'k')
self.__validateKingsPosition(fen)
self.__validatePawnsPosition(fen)
self.fen = fen
def get_white_score(self):
figures = []
for idx in range(0, len(self.fen)):
element = self.fen[idx]
if 'rnbkqp'.find(element) != -1:
figures.append(element)
return ChessScore(figures)
def get_black_score(self):
figures = []
for idx in range(0, len(self.fen)):
element = self.fen[idx]
if 'RNBKQP'.find(element) != -1:
figures.append(element)
return ChessScore(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()
def __print__(o):
print(o.fen)
def __len__(o):
return len(re.sub("[/1-8]", '', o.fen))
def __getitem__(self, position):
row = self.fen.split('/')[ord(position[0]) - ord('A')]
for idx in range(0, len(row)):
element = row[idx]
if idx == position and ('rnbkqp'.find(element) != -1 or 'RNBKQP'.find(element) != -1):
return element
else:
try:
int(element)
except ValueError:
continue
idx += int(element)
return -1
def __validateKings(self, fen, char):
idx1 = fen.find(char)
idx2 = fen.rfind(char)
if idx1 == -1 or idx1 != idx2:
raise ChessException('kings')
def __validateKingsPosition(self, fen):
rows = fen.split('/')
for i in range(7):
idx1 = rows[i].find('K')
idx2 = rows[i].find('k')
if idx1 != -1 and idx2 == -1:
idx2 = rows[i+1].find('k')
if idx1 == -1 and idx2 != -1:
idx1 = rows[i+1].find('K')
if idx1 != -1 and idx2 != -1 and (abs(idx1 - idx2) == 1 or idx1 - idx2 == 0):
raise ChessException('kings')
idx1 = rows[7].find('K')
idx2 = rows[7].find('k')
if idx1 != -1 and idx2 != -1 and abs(idx1 - idx2) == 1:
raise ChessException('kings')
def __validatePawnsPosition(self, fen):
rows = fen.split('/')
idx1 = rows[0].find('p')
idx2 = rows[0].find('P')
idx3 = rows[7].find('p')
idx4 = rows[7].find('P')
if idx1 != -1 or idx2 != -1 or idx3 != -1 or idx4 != -1:
raise ChessException('pawns')
class ChessScore:
def __init__(self, list):
self.figures = list
self.points = self.__calculatePointsFromFigures(list)
def __calculatePointsFromFigures(self, list):
points = 0
for x in list:
if x == 'r':
points += 5
elif x == 'n':
points += 3
elif x == 'b':
points += 3
elif x == 'k':
points += 4
elif x == 'q':
points += 9
elif x == 'p':
points += 1
return points
def __int__(o):
return o.points
def __add__(self, o):
return self.points + o.points
def __sub__(self, o):
return self.points - o.points
def __lt__(self, o):
return self.points < o.points
def __le__(self, o):
return self.points <= o.points
def __eq__(self, o):
return self.points == o.points
def __ne__(self, o):
return self.points != o.points
def __gt__(self, o):
return self.points > o.points

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

..EEEFE...F.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_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

======================================================================
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: -1 != 'K'

======================================================================
FAIL: test_str (test.TestChessPosition)
Test string representation of the instance.
----------------------------------------------------------------------
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: '<solution.ChessPosition object at 0xffffb2082380>' != '6k1/5p2/6p1/8/7p/8/6PP/6K1'
- <solution.ChessPosition object at 0xffffb2082380>
+ 6k1/5p2/6p1/8/7p/8/6PP/6K1


----------------------------------------------------------------------
Ran 17 tests in 0.164s

FAILED (failures=2, errors=5)

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

Ива обнови решението на 29.11.2022 17:34 (преди почти 2 години)

+import re
+
+class ChessException(Exception):
+ def __init__(self, message='chess error occured'):
+ super().__init__(message)
+
+class ChessPosition:
+ def __init__(self, fen):
+ self.__validateKings(fen, 'K')
+ self.__validateKings(fen, 'k')
+ self.__validateKingsPosition(fen)
+ self.__validatePawnsPosition(fen)
+ self.fen = fen
+
+
+ def get_white_score(self):
+ figures = []
+ for idx in range(0, len(self.fen)):
+ element = self.fen[idx]
+ if 'rnbkqp'.find(element) != -1:
+ figures.append(element)
+ return ChessScore(figures)
+
+ def get_black_score(self):
+ figures = []
+ for idx in range(0, len(self.fen)):
+ element = self.fen[idx]
+ if 'RNBKQP'.find(element) != -1:
+ figures.append(element)
+ return ChessScore(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()
+
+ def __print__(o):
+ print(o.fen)
+
+ def __len__(o):
+ return len(re.sub("[/1-8]", '', o.fen))
+
+ def __getitem__(self, position):
+ row = self.fen.split('/')[ord(position[0]) - ord('A')]
+ for idx in range(0, len(row)):
+ element = row[idx]
+ if idx == position and ('rnbkqp'.find(element) != -1 or 'RNBKQP'.find(element) != -1):
+ return element
+ else:
+ try:
+ int(element)
+ except ValueError:
+ continue
+ idx += int(element)
+ return -1
+
+
+
+ def __validateKings(self, fen, char):
+ idx1 = fen.find(char)
+ idx2 = fen.rfind(char)
+ if idx1 == -1 or idx1 != idx2:
+ raise ChessException('kings')
+
+ def __validateKingsPosition(self, fen):
+ rows = fen.split('/')
+ for i in range(7):
+ idx1 = rows[i].find('K')
+ idx2 = rows[i].find('k')
+ if idx1 != -1 and idx2 == -1:
+ idx2 = rows[i+1].find('k')
+ if idx1 == -1 and idx2 != -1:
+ idx1 = rows[i+1].find('K')
+ if idx1 != -1 and idx2 != -1 and (abs(idx1 - idx2) == 1 or idx1 - idx2 == 0):
+ raise ChessException('kings')
+
+ idx1 = rows[7].find('K')
+ idx2 = rows[7].find('k')
+ if idx1 != -1 and idx2 != -1 and abs(idx1 - idx2) == 1:
+ raise ChessException('kings')
+
+ def __validatePawnsPosition(self, fen):
+ rows = fen.split('/')
+ idx1 = rows[0].find('p')
+ idx2 = rows[0].find('P')
+ idx3 = rows[7].find('p')
+ idx4 = rows[7].find('P')
+ if idx1 != -1 or idx2 != -1 or idx3 != -1 or idx4 != -1:
+ raise ChessException('pawns')
+
+class ChessScore:
+ def __init__(self, list):
+ self.figures = list
+ self.points = self.__calculatePointsFromFigures(list)
+
+ def __calculatePointsFromFigures(self, list):
+ points = 0
+ for x in list:
+ if x == 'r':
+ points += 5
+ elif x == 'n':
+ points += 3
+ elif x == 'b':
+ points += 3
+ elif x == 'k':
+ points += 4
+ elif x == 'q':
+ points += 9
+ elif x == 'p':
+ points += 1
+ return points
+
+ def __int__(o):
+ return o.points
+
+ def __add__(self, o):
+ return self.points + o.points
+
+ def __sub__(self, o):
+ return self.points - o.points
+
+ def __lt__(self, o):
+ return self.points < o.points
+
+ def __le__(self, o):
+ return self.points <= o.points
+
+ def __eq__(self, o):
+ return self.points == o.points
+
+ def __ne__(self, o):
+ return self.points != o.points
+
+ def __gt__(self, o):
+ return self.points > o.points
+