Решение на Шахматни фенове от Адриана Атанасова

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

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

Резултати

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

Код

scores = {'r': 5, 'n': 3, 'b': 3, 'k': 4, 'q': 9, 'p': 1}
class ChessException(Exception):
pass
class ChessPosition:
def __init__(self, string):
self.string = string
self.splitted_string = string.split("/")
king_white = 0
king_black = 0
position_white_king = 0
position_black_king = 0
row_position_white = 0
row_position_black = 0
row = 0
for elements in self.splitted_string:
count = 0
for letter in elements:
count += 1
if letter == 'K':
position_white_king = count
row_position_white = row
king_white += 1
if letter == 'k':
position_black_king = count
row_position_black = row
king_black += 1
row += 1
if (position_white_king == position_black_king - 1 or position_white_king == position_black_king + 1
or position_white_king == position_black_king) \
and (row_position_black == row_position_white or row_position_black == row_position_white + 1
or row_position_black == row_position_white - 1):
raise ChessException("kings")
elif king_black != 1 or king_white != 1:
raise ChessException("kings")
elif 'p' in self.splitted_string[0] or 'p' in self.splitted_string[7]:
raise ChessException("pawns")
elif 'P' in self.splitted_string[0] or 'P' in self.splitted_string[7]:
raise ChessException("pawns")
def len(self):
length = 0
for elements in self.splitted_string:
for letter in elements:
for find_same_element in scores:
if letter.lower() == find_same_element:
length += 1
return length
def __str__(self):
return self.string
def print(self):
print(self.string)
def position(self, pos):

Тук предполагам се опитваш да имплементираш синтаксис от вида x['E2']. Убеди се, че работи. Това, което ти очакваш с този метод, е да извикам x.pos('E2'), което е различно.

row = int(pos[1]) - 1
column = ord(pos[0]) - 65
counter_row = 0
counter_column = 0
for element in self.splitted_string:
if row == counter_row:
for letter in element:
if column == counter_column and letter.isalpha():
return print(letter)
elif column != counter_column and letter.isnumeric():
return None
counter_column += 1
counter_row += 1
return None
def get_white_score(self):
white_figures = []
for elements in self.splitted_string:
for letter in elements:
for find_same_element in scores:
if letter == find_same_element.upper():
white_figures.append(letter)
return ChessScore.__int__(ChessScore(white_figures))
def get_black_score(self):
black_figures = []
for elements in self.splitted_string:
for letter in elements:
for find_same_element in scores:
if letter == find_same_element:
black_figures.append(letter)
return ChessScore.__int__(ChessScore(black_figures))
def white_is_winning(self):
scores_white = self.get_white_score()

По-добре сетни ChessScore за белите и черните като атрибути на текущия клса, за да не се налага да го изчисляваш и реинициализираш при всяко извикване на тези функции.

scores_black = self.get_black_score()
return scores_white > scores_black
def black_is_winning(self):
scores_white = self.get_white_score()
scores_black = self.get_black_score()
return scores_black > scores_white
def is_equal(self):
scores_white = self.get_white_score()
scores_black = self.get_black_score()
return scores_black == scores_white
class ChessScore:
def __init__(self, figures):
self.figures = figures
def __int__(self):
count = 0
for element in self.figures:
for find_same_element in scores:
if element.lower() == find_same_element:
count += scores[find_same_element]
return count
def __gt__(self, other):
return int(ChessScore(self.figures)) > int(ChessScore(other.figures))
def __ge__(self, other):
return int(ChessScore(self.figures)) >= int(ChessScore(other.figures))
def __lt__(self, other):
return int(ChessScore(self.figures)) < int(ChessScore(other.figures))
def __le__(self, other):
return int(ChessScore(self.figures)) <= int(ChessScore(other.figures))
def __eq__(self, other):
return int(ChessScore(self.figures)) == int(ChessScore(other.figures))
def __ne__(self, other):
return int(ChessScore(self.figures)) != int(ChessScore(other.figures))
def __add__(self, other):
return int(ChessScore(self.figures)) + int(ChessScore(other.figures))
def __sub__(self, other):
return int(ChessScore(self.figures)) - int(ChessScore(other.figures))

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

..EEEEE.E...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_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
TypeError: 'ChessPosition' object is not subscriptable

======================================================================
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_len (test.TestChessPosition)
Test number of pieces for a position.
----------------------------------------------------------------------
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
TypeError: object of type 'ChessPosition' has no len()

======================================================================
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

----------------------------------------------------------------------
Ran 17 tests in 0.161s

FAILED (errors=7)

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

Адриана обнови решението на 25.11.2022 18:07 (преди почти 2 години)

+scores = {'r': 5, 'n': 3, 'b': 3, 'k': 4, 'q': 9, 'p': 1}
+
+
+class ChessException(Exception):
+ pass
+
+
+class ChessPosition:
+ def __init__(self, string):
+ self.string = string
+ self.splitted_string = string.split("/")
+ king_white = 0
+ king_black = 0
+ position_white_king = 0
+ position_black_king = 0
+ row_position_white = 0
+ row_position_black = 0
+ row = 0
+ for elements in self.splitted_string:
+ count = 0
+ for letter in elements:
+ count += 1
+ if letter == 'K':
+ position_white_king = count
+ row_position_white = row
+ king_white += 1
+ if letter == 'k':
+ position_black_king = count
+ row_position_black = row
+ king_black += 1
+ row += 1
+
+ if (position_white_king == position_black_king - 1 or position_white_king == position_black_king + 1
+ or position_white_king == position_black_king) \
+ and (row_position_black == row_position_white or row_position_black == row_position_white + 1
+ or row_position_black == row_position_white - 1):
+ raise ChessException("kings")
+ elif king_black != 1 or king_white != 1:
+ raise ChessException("kings")
+ elif 'p' in self.splitted_string[0] or 'p' in self.splitted_string[7]:
+ raise ChessException("pawns")
+ elif 'P' in self.splitted_string[0] or 'P' in self.splitted_string[7]:
+ raise ChessException("pawns")
+
+ def len(self):
+ length = 0
+ for elements in self.splitted_string:
+ for letter in elements:
+ for find_same_element in scores:
+ if letter.lower() == find_same_element:
+ length += 1
+ return length
+
+ def __str__(self):
+ return self.string
+
+ def print(self):
+ print(self.string)
+
+ def position(self, pos):

Тук предполагам се опитваш да имплементираш синтаксис от вида x['E2']. Убеди се, че работи. Това, което ти очакваш с този метод, е да извикам x.pos('E2'), което е различно.

+ row = int(pos[1]) - 1
+ column = ord(pos[0]) - 65
+ counter_row = 0
+ counter_column = 0
+ for element in self.splitted_string:
+ if row == counter_row:
+ for letter in element:
+ if column == counter_column and letter.isalpha():
+ return print(letter)
+ elif column != counter_column and letter.isnumeric():
+ return None
+ counter_column += 1
+ counter_row += 1
+ return None
+
+ def get_white_score(self):
+ white_figures = []
+ for elements in self.splitted_string:
+ for letter in elements:
+ for find_same_element in scores:
+ if letter == find_same_element.upper():
+ white_figures.append(letter)
+ return ChessScore.__int__(ChessScore(white_figures))
+
+ def get_black_score(self):
+ black_figures = []
+ for elements in self.splitted_string:
+ for letter in elements:
+ for find_same_element in scores:
+ if letter == find_same_element:
+ black_figures.append(letter)
+ return ChessScore.__int__(ChessScore(black_figures))
+
+ def white_is_winning(self):
+ scores_white = self.get_white_score()

По-добре сетни ChessScore за белите и черните като атрибути на текущия клса, за да не се налага да го изчисляваш и реинициализираш при всяко извикване на тези функции.

+ scores_black = self.get_black_score()
+ return scores_white > scores_black
+
+ def black_is_winning(self):
+ scores_white = self.get_white_score()
+ scores_black = self.get_black_score()
+ return scores_black > scores_white
+
+ def is_equal(self):
+ scores_white = self.get_white_score()
+ scores_black = self.get_black_score()
+ return scores_black == scores_white
+
+
+class ChessScore:
+ def __init__(self, figures):
+ self.figures = figures
+
+ def __int__(self):
+ count = 0
+ for element in self.figures:
+ for find_same_element in scores:
+ if element.lower() == find_same_element:
+ count += scores[find_same_element]
+ return count
+
+ def __gt__(self, other):
+ return int(ChessScore(self.figures)) > int(ChessScore(other.figures))
+
+ def __ge__(self, other):
+ return int(ChessScore(self.figures)) >= int(ChessScore(other.figures))
+
+ def __lt__(self, other):
+ return int(ChessScore(self.figures)) < int(ChessScore(other.figures))
+
+ def __le__(self, other):
+ return int(ChessScore(self.figures)) <= int(ChessScore(other.figures))
+
+ def __eq__(self, other):
+ return int(ChessScore(self.figures)) == int(ChessScore(other.figures))
+
+ def __ne__(self, other):
+ return int(ChessScore(self.figures)) != int(ChessScore(other.figures))
+
+ def __add__(self, other):
+ return int(ChessScore(self.figures)) + int(ChessScore(other.figures))
+
+ def __sub__(self, other):
+ return int(ChessScore(self.figures)) - int(ChessScore(other.figures))