Решение на Шахматни фенове от Сузана Петкова

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

Към профила на Сузана Петкова

Резултати

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

Код

class ChessException(Exception):
pass
class ChessPosition(object):
def __init__(self, position):
self._position = self.is_valid(position)
def transform_position(self, position):
list_of_rows = position.split('/')
for i in range(len(list_of_rows)):
if len(list_of_rows[i]) != 8:
new_row = ''
for el in list_of_rows[i]:
if el.isdigit():
for _ in range(int(el)):
new_row += '1'
else:
new_row += el
list_of_rows[i] = new_row
return list_of_rows
def is_valid(self, position):
list_of_rows = self.transform_position(position)
index_k = ()
index_K = ()
count_K = 0
count_k = 0
for i in range(len(list_of_rows)):
count_K += list_of_rows[i].count('K')
count_k += list_of_rows[i].count('k')
for row in list_of_rows:
if 'k' in row:
col = row.index('k')
index_k = list_of_rows.index(row), col
if 'K' in row:
col = row.index('K')
index_K = list_of_rows.index(row), col
if count_K != 1 or count_k != 1:
raise ChessException('kings')
elif index_k[0] == index_K[0] or index_k[0] == index_K[0] + 1 or index_k[0] == index_K[0] - 1:
if index_k[1] == index_K[1] + 1 or index_k[1] == index_K[1] - 1 or index_k[1] == index_K[1]:
raise ChessException('kings')
elif 'P' in list_of_rows[0] or 'P' in list_of_rows[-1]:
raise ChessException('pawns')
elif 'p' in list_of_rows[0] or 'p' in list_of_rows[-1]:
raise ChessException('pawns')
return position
def __getitem__(self, location):
columns = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H']
list_of_rows = self.transform_position(self._position)
col = columns.index(location[0])
row = len(list_of_rows) - int(location[1])
if list_of_rows[row][col].isdigit():
return None
else:
return list_of_rows[row][col]
def __len__(self):
count = 0
for figure in self._position:
if figure.isalpha():
count += 1
return count
def get_white_score(self):
figures = []
for el in self._position:
if el.isupper():
figures.append(el)
return ChessScore(figures)
def get_black_score(self):
figures = []
for el in self._position:
if el.islower():
figures.append(el)
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_black_score() > self.get_white_score()
def is_equal(self):
return self.get_white_score() == self.get_black_score()
def __repr__(self):
return self._position
class ChessScore:
points = {'r': '5', 'k': '4', 'q': '9', 'p': '1', 'n': '3', 'b': '3'}
def __init__(self, figures):
self.figures = figures
def count_points(self):
score = 0
for el in self.figures:
el = el.lower()
if el in self.points:
score += int(self.points[el])
return str(score)
def __int__(self):
return int(self.count_points())
def __lt__(self, other):
return self.count_points() < other.count_points()
def __le__(self, other):
return self.count_points() <= other.count_points()
def __gt__(self, other):
return self.count_points() > other.count_points()
def __ge__(self, other):
return self.count_points() >= other.count_points()
def __eq__(self, other):
return self.count_points() == other.count_points()
def __ne__(self, other):
return self.count_points() != other.count_points()
def __add__(self, other):
return int(self.count_points()) + int(other.count_points())
def __sub__(self, other):
return int(self.count_points()) - int(other.count_points())
def __repr__(self):
return self.count_points()

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

.................
----------------------------------------------------------------------
Ran 17 tests in 0.172s

OK

История (2 версии и 8 коментара)

Сузана обнови решението на 27.11.2022 23:39 (преди почти 2 години)

+class ChessException(Exception):
+ def __init__(self, message):
+ self.message = message
+ super().__init__(self.message)
+
+
+class ChessPosition(object):
+ def __init__(self, position):
+ self._position = self.is_it_valid(position)
+
+ def transform_position(self, position):
+ list_of_rows = position.split('/')
+ for i in range(len(list_of_rows)):
+ if len(list_of_rows[i]) != 8:
+ new_row = ''
+ for el in list_of_rows[i]:
+ if el.isdigit():
+ for _ in range(int(el)):
+ new_row += '1'
+ else:
+ new_row += el
+ list_of_rows[i] = new_row
+ return list_of_rows
+
+ def is_it_valid(self, position):
+ list_of_rows = self.transform_position(position)
+ index_k = ()
+ index_K = ()
+ count_K = 0
+ count_k = 0
+ for i in range(len(list_of_rows)):
+ count_K += list_of_rows[i].count('K')
+ count_k += list_of_rows[i].count('k')
+
+ for row in list_of_rows:
+ if 'k' in row:
+ col = row.index('k')
+ index_k = list_of_rows.index(row), col
+ if 'K' in row:
+ col = row.index('K')
+ index_K = list_of_rows.index(row), col
+
+ if count_K != 1 or count_k != 1:
+ raise ChessException('kings')
+ elif index_k[0] == index_K[0] or index_k[0] == index_K[0] + 1 or index_k[0] == index_K[0] - 1:
+ if index_k[1] == index_K[1] + 1 or index_k[1] == index_K[1] - 1 or index_k[1] == index_K[1]:
+ raise ChessException('kings')
+ elif 'P' in list_of_rows[0] or 'P' in list_of_rows[-1]:
+ raise ChessException('pawns')
+ elif 'p' in list_of_rows[0] or 'p' in list_of_rows[-1]:
+ raise ChessException('pawns')
+
+ return position
+
+ def __getitem__(self, location):
+ columns = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H']
+ list_of_rows = self.transform_position(self._position)
+ col = columns.index(location[0])
+ row = len(list_of_rows) - int(location[1])
+
+ for i in range(len(list_of_rows)):

Не е нужно да циклиш в списъците, за да вземеш търсения индекс: list_of_rows[row] ще даде същото (не съм сигурен дали това е list, но дори да не е, можеш да го кастнеш).

+ if row == i:
+ for j in range(len(list_of_rows[i])):
+ if col == j:
+ if list_of_rows[i][j].isdigit():
+ return None
+ return list_of_rows[i][j]
+
+ def __len__(self):
+ count = 0
+ for _ in self._position:
+ if _ in ['r', 'R', 'k', 'K', 'q', 'Q', 'p', 'P', 'b', 'B', 'n', 'N']:
+ count += 1
+ return count
+
+ def get_white_score(self):
+ figures = []
+ for el in self._position:
+ if el in ['P', 'K', 'Q', 'B', 'N', 'R']:
+ figures.append(el)
+ return ChessScore(figures)
+
+ def get_black_score(self):
+ figures = []
+ for el in self._position:
+ if el in ['p', 'k', 'q', 'b', 'n', 'r']:
+ figures.append(el)
+ return ChessScore(figures)
+
+ def white_is_winning(self):
+ return int(self.get_white_score()) > int(self.get_black_score())
+
+ def black_is_winning(self):
+ return int(self.get_black_score()) > int(self.get_white_score())
+
+ def is_equal(self):
+ return int(self.get_white_score()) == int(self.get_black_score())
+
+ def __repr__(self):
+ return self._position
+
+
+class ChessScore:
+ points = {'r': '5', 'k': '4', 'q': '9', 'p': '1', 'n': '3', 'b': '3'}
+
+ def __init__(self, figures):
+ self.figures = figures
+
+ def count_points(self):
+ score = 0
+ for el in self.figures:
+ el = el.lower()
+ if el in self.points:
+ score += int(self.points[el])
+ return str(score)
+
+ def __int__(self):
+ return int(self.count_points())
+
+ def __lt__(self, other):
+ return self.count_points() < other.count_points()
+
+ def __le__(self, other):
+ return self.count_points() <= other.count_points()
+
+ def __gt__(self, other):
+ return self.count_points() > other.count_points()
+
+ def __ge__(self, other):
+ return self.count_points() >= other.count_points()
+
+ def __eq__(self, other):
+ return self.count_points() == other.count_points()
+
+ def __ne__(self, other):
+ return self.count_points() != other.count_points()
+
+ def __add__(self, other):
+ return int(self.count_points()) + int(other.count_points())
+
+ def __sub__(self, other):
+ return int(self.count_points()) - int(other.count_points())
+
+ def __repr__(self):
+ return self.count_points()

Сузана обнови решението на 28.11.2022 16:30 (преди почти 2 години)

class ChessException(Exception):
- def __init__(self, message):
- self.message = message
- super().__init__(self.message)
+ pass
class ChessPosition(object):
def __init__(self, position):
- self._position = self.is_it_valid(position)
+ self._position = self.is_valid(position)
def transform_position(self, position):
list_of_rows = position.split('/')
for i in range(len(list_of_rows)):
if len(list_of_rows[i]) != 8:
new_row = ''
for el in list_of_rows[i]:
if el.isdigit():
for _ in range(int(el)):
new_row += '1'
else:
new_row += el
list_of_rows[i] = new_row
return list_of_rows
- def is_it_valid(self, position):
+ def is_valid(self, position):
list_of_rows = self.transform_position(position)
index_k = ()
index_K = ()
count_K = 0
count_k = 0
for i in range(len(list_of_rows)):
count_K += list_of_rows[i].count('K')
count_k += list_of_rows[i].count('k')
for row in list_of_rows:
if 'k' in row:
col = row.index('k')
index_k = list_of_rows.index(row), col
if 'K' in row:
col = row.index('K')
index_K = list_of_rows.index(row), col
if count_K != 1 or count_k != 1:
raise ChessException('kings')
elif index_k[0] == index_K[0] or index_k[0] == index_K[0] + 1 or index_k[0] == index_K[0] - 1:
if index_k[1] == index_K[1] + 1 or index_k[1] == index_K[1] - 1 or index_k[1] == index_K[1]:
raise ChessException('kings')
elif 'P' in list_of_rows[0] or 'P' in list_of_rows[-1]:
raise ChessException('pawns')
elif 'p' in list_of_rows[0] or 'p' in list_of_rows[-1]:
raise ChessException('pawns')
return position
def __getitem__(self, location):
columns = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H']
list_of_rows = self.transform_position(self._position)
col = columns.index(location[0])
row = len(list_of_rows) - int(location[1])
- for i in range(len(list_of_rows)):
- if row == i:
- for j in range(len(list_of_rows[i])):
- if col == j:
- if list_of_rows[i][j].isdigit():
- return None
- return list_of_rows[i][j]
+ if list_of_rows[row][col].isdigit():
+ return None
+ else:
+ return list_of_rows[row][col]
def __len__(self):
count = 0
- for _ in self._position:
- if _ in ['r', 'R', 'k', 'K', 'q', 'Q', 'p', 'P', 'b', 'B', 'n', 'N']:
+ for figure in self._position:
+ if figure.isalpha():
count += 1
return count
def get_white_score(self):
figures = []
for el in self._position:
- if el in ['P', 'K', 'Q', 'B', 'N', 'R']:
+ if el.isupper():
figures.append(el)
return ChessScore(figures)
def get_black_score(self):
figures = []
for el in self._position:
- if el in ['p', 'k', 'q', 'b', 'n', 'r']:
+ if el.islower():
figures.append(el)
return ChessScore(figures)
def white_is_winning(self):
- return int(self.get_white_score()) > int(self.get_black_score())
+ return self.get_white_score() > self.get_black_score()
def black_is_winning(self):
- return int(self.get_black_score()) > int(self.get_white_score())
+ return self.get_black_score() > self.get_white_score()
def is_equal(self):
- return int(self.get_white_score()) == int(self.get_black_score())
+ return self.get_white_score() == self.get_black_score()
def __repr__(self):
return self._position
class ChessScore:
points = {'r': '5', 'k': '4', 'q': '9', 'p': '1', 'n': '3', 'b': '3'}
def __init__(self, figures):
self.figures = figures
def count_points(self):
score = 0
for el in self.figures:
el = el.lower()
if el in self.points:
score += int(self.points[el])
return str(score)
def __int__(self):
return int(self.count_points())
def __lt__(self, other):
return self.count_points() < other.count_points()
def __le__(self, other):
return self.count_points() <= other.count_points()
def __gt__(self, other):
return self.count_points() > other.count_points()
def __ge__(self, other):
return self.count_points() >= other.count_points()
def __eq__(self, other):
return self.count_points() == other.count_points()
def __ne__(self, other):
return self.count_points() != other.count_points()
def __add__(self, other):
return int(self.count_points()) + int(other.count_points())
def __sub__(self, other):
return int(self.count_points()) - int(other.count_points())
def __repr__(self):
- return self.count_points()
+ return self.count_points()