Решение на Шахматни фенове от Клементина Картевска

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

Към профила на Клементина Картевска

Резултати

  • 9 точки от тестове
  • 0 бонус точки
  • 9 точки общо
  • 16 успешни тест(а)
  • 1 неуспешни тест(а)

Код

figures_points = {'R' : 5, 'N' : 3, 'B' : 3, 'Q' : 9, 'K' : 4, 'P' : 1}
positions = {'A' : 1, 'B' : 2, 'C' : 3, 'D' : 4, 'E' : 5, 'F' : 6, 'G' : 7, 'H' :8}
class ChessException(Exception):
pass
class ChessPosition:
def __init__(self, position):
self.table = position.split('/')
self.table.reverse()
for index, elems in enumerate(self.table):
elems = list(elems)
ind = 0
while ind<len(elems):
if isinstance(elems[ind], str) and '0' <= elems[ind] <= '8':
new_elems = elems[:ind]
new_elems.extend([None]*int(elems[ind]))
new_elems.extend(elems[ind+1:])
elems = new_elems
ind += 1
self.table[index] = elems
self.position = position
self.__check()
self.whites = []
for letter in self.position:
if 'A' <= letter <= 'Z':
self.whites.append(letter)
self.blacks = []
for letter in self.position:
if 'a' <= letter <= 'z':
self.blacks.append(letter)
def __check(self):
if self.position.count('K') != 1 or self.position.count('k') != 1:
raise ChessException('kings')
pos_k = self.get_position('k')
pos_K = self.get_position('K')
if (pos_K[0] - 1 <= pos_k[0] <= pos_K[0] + 1) and (pos_K[1] - 1 <= pos_k[1] <= pos_K[1] + 1):
raise ChessException('kings')
if('p' in self.table[-1] or 'P' in self.table[0]):
raise ChessException('pawns')
def get_position(self, elem):
for ind, row in enumerate(self.table):
if elem in row:
return (ind, row.index(elem))
def __str__(self):
return self.position
def __len__(self):
return len(self.whites) + len(self.blacks)
def __getitem__(self, pos):
column = positions[pos[0]] - 1
row = int(pos[1]) - 1
return self.table[row][column]
def get_white_score(self):
return ChessScore(self.whites)
def get_black_score(self):
return ChessScore(self.blacks)
def is_equal(self):
return self.get_white_score() == self.get_black_score()
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()
class ChessScore:
def __init__(self, figures):
self.score = 0
for figure in figures:
self.score += figures_points[figure.upper()]
def __lt__(self, other):
return self.score < other.score
def __le__(self, other):
return self.score <= other.score
def __gt__(self, other):
return self.score > other.score
def __ge__(self, other):
return self.score >= other.score
def __ne__(self, other):
return self.score != other.score
def __eq__(self, other):
return self.score == other.score
def __add__(self, other):
return self.score + other.score
def __sub__(self, other):
return self.score - other.score
def __int__(self):
return self.score

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

.........E.......
======================================================================
ERROR: test_pawns_position (test.TestChessPosition)
Test for incorrect pawns.
----------------------------------------------------------------------
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
Exception: No exception raised on: 8/8/k7/8/7K/8/8/p7

----------------------------------------------------------------------
Ran 17 tests in 0.167s

FAILED (errors=1)

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

Клементина обнови решението на 29.11.2022 01:19 (преди почти 2 години)

+figures_points = {'R' : 5, 'N' : 3, 'B' : 3, 'Q' : 9, 'K' : 4, 'P' : 1}
+positions = {'A' : 1, 'B' : 2, 'C' : 3, 'D' : 4, 'E' : 5, 'F' : 6, 'G' : 7, 'H' :8}
+
+def get_position(table, elem):
+ for row in table:
+ if elem in row:
+ return (row, row.index(elem))
+
+
+class ChessException(Exception):
+
+ def __init__(self, msg):
+ self.msg = msg
+ super().__init__(msg)
+
+ pass
+
+
+class ChessPosition:
+
+ def __init__(self, position):
+ self.table = position.split('/')
+ self.table.reverse()
+ for index, elems in enumerate(self.table):
+ elems = list(elems)
+ ind = 0
+ while(ind<len(elems)):
+ if isinstance(elems[ind], str) and '0' <= elems[ind] <= '9':
+ new_elems = elems[:ind]
+ new_elems.extend([None]*int(elems[ind]))
+ new_elems.extend(elems[ind+1:])
+ elems = new_elems
+ ind += 1
+ self.table[index] = elems
+ if self.position.count('K') != 1 or self.position.count('k') != 1:
+ raise ChessException('kings')
+ pos_k = get_position(self.table, 'k')
+ pos_K = get_position(self.table, 'K')
+ if (pos_K[0] - 1 <= pos_k[0] <= pos_K[0] + 1) and (pos_K[1] - 1 <= pos_k[1] <= pos_K[1] + 1):
+ raise ChessException('kings')
+ if('p' in self.table[-1] or 'P' in self.table[0]):
+ raise ChessException('pawns')
+ self.position = position
+ self.whites = []
+ for letter in self.position:
+ if 'A' <= letter <= 'Z':
+ self.whites.append(letter)
+ self.blacks = []
+ for letter in self.position:
+ if 'a' <= letter <= 'z':
+ self.blacks.append(letter)
+
+ def __str__(self):
+ return self.position
+
+ def __len__(self):
+ return len(self.whites) + len(self.blacks)
+
+ def __getitem__(self, pos):
+ column = positions[pos[0]] - 1
+ row = pos[1] - 1
+ return self.table[row][column]
+
+ def get_white_score(self):
+ return ChessScore(self.whites)
+
+ def get_black_score(self):
+ return ChessScore(self.blacks)
+
+ def is_equal(self):
+ return self.get_white_score() == self.get_black_score()
+
+ 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()
+
+
+class ChessScore:
+
+ def __init__(self, figures):
+ self.score = 0
+ for figure in figures.upper():
+ self.score += figures_points[figure]
+
+ def __lt__(self, other):
+ return self.score < other.score
+
+ def __le__(self, other):
+ return self.score <= other.score
+
+ def __gt__(self, other):
+ return self.score > other.score
+
+ def __ge__(self, other):
+ return self.score >= other.score
+
+ def __ne__(self, other):
+ return self.score != other.score
+
+ def __eq__(self, other):
+ return self.score == other.score
+
+ def __add__(self, other):
+ return self.score + other.score
+
+ def __sub__(self, other):
+ return self.score - other.score
+
+ def __int__(self):
+ return self.score

Клементина обнови решението на 29.11.2022 01:38 (преди почти 2 години)

figures_points = {'R' : 5, 'N' : 3, 'B' : 3, 'Q' : 9, 'K' : 4, 'P' : 1}
positions = {'A' : 1, 'B' : 2, 'C' : 3, 'D' : 4, 'E' : 5, 'F' : 6, 'G' : 7, 'H' :8}
def get_position(table, elem):
- for row in table:
+ for ind, row in enumerate(table):
if elem in row:
- return (row, row.index(elem))
+ return (ind, row.index(elem))
class ChessException(Exception):
def __init__(self, msg):
self.msg = msg
super().__init__(msg)
pass
class ChessPosition:
def __init__(self, position):
self.table = position.split('/')
self.table.reverse()
for index, elems in enumerate(self.table):
elems = list(elems)
ind = 0
while(ind<len(elems)):
if isinstance(elems[ind], str) and '0' <= elems[ind] <= '9':
new_elems = elems[:ind]
new_elems.extend([None]*int(elems[ind]))
new_elems.extend(elems[ind+1:])
elems = new_elems
ind += 1
self.table[index] = elems
- if self.position.count('K') != 1 or self.position.count('k') != 1:
+ if position.count('K') != 1 or position.count('k') != 1:
raise ChessException('kings')
pos_k = get_position(self.table, 'k')
pos_K = get_position(self.table, 'K')
if (pos_K[0] - 1 <= pos_k[0] <= pos_K[0] + 1) and (pos_K[1] - 1 <= pos_k[1] <= pos_K[1] + 1):
raise ChessException('kings')
if('p' in self.table[-1] or 'P' in self.table[0]):
raise ChessException('pawns')
self.position = position
self.whites = []
for letter in self.position:
if 'A' <= letter <= 'Z':
self.whites.append(letter)
self.blacks = []
for letter in self.position:
if 'a' <= letter <= 'z':
self.blacks.append(letter)
def __str__(self):
return self.position
def __len__(self):
return len(self.whites) + len(self.blacks)
def __getitem__(self, pos):
column = positions[pos[0]] - 1
- row = pos[1] - 1
+ row = int(pos[1]) - 1
return self.table[row][column]
def get_white_score(self):
return ChessScore(self.whites)
def get_black_score(self):
return ChessScore(self.blacks)
def is_equal(self):
return self.get_white_score() == self.get_black_score()
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()
class ChessScore:
def __init__(self, figures):
self.score = 0
- for figure in figures.upper():
- self.score += figures_points[figure]
+ for figure in figures:
+ self.score += figures_points[figure.upper()]
def __lt__(self, other):
return self.score < other.score
def __le__(self, other):
return self.score <= other.score
def __gt__(self, other):
return self.score > other.score
def __ge__(self, other):
return self.score >= other.score
def __ne__(self, other):
return self.score != other.score
def __eq__(self, other):
return self.score == other.score
def __add__(self, other):
return self.score + other.score
def __sub__(self, other):
return self.score - other.score
def __int__(self):
return self.score

Клементина обнови решението на 29.11.2022 01:41 (преди почти 2 години)

figures_points = {'R' : 5, 'N' : 3, 'B' : 3, 'Q' : 9, 'K' : 4, 'P' : 1}
positions = {'A' : 1, 'B' : 2, 'C' : 3, 'D' : 4, 'E' : 5, 'F' : 6, 'G' : 7, 'H' :8}
def get_position(table, elem):
for ind, row in enumerate(table):
if elem in row:
return (ind, row.index(elem))
class ChessException(Exception):
def __init__(self, msg):
self.msg = msg
super().__init__(msg)
- pass
-
class ChessPosition:
def __init__(self, position):
self.table = position.split('/')
self.table.reverse()
for index, elems in enumerate(self.table):
elems = list(elems)
ind = 0
while(ind<len(elems)):
if isinstance(elems[ind], str) and '0' <= elems[ind] <= '9':

Принципно трябва да е до 8, а не до 9, но ние няма да тестваме срещу такива неща. Въпросът, обаче, е, че ако ще го пишеш до 9, можеш да използваш elems[ind].isgidit()

new_elems = elems[:ind]
new_elems.extend([None]*int(elems[ind]))
new_elems.extend(elems[ind+1:])
elems = new_elems
ind += 1
self.table[index] = elems
if position.count('K') != 1 or position.count('k') != 1:
raise ChessException('kings')
pos_k = get_position(self.table, 'k')
pos_K = get_position(self.table, 'K')
if (pos_K[0] - 1 <= pos_k[0] <= pos_K[0] + 1) and (pos_K[1] - 1 <= pos_k[1] <= pos_K[1] + 1):
raise ChessException('kings')
if('p' in self.table[-1] or 'P' in self.table[0]):
raise ChessException('pawns')
self.position = position
self.whites = []
for letter in self.position:
if 'A' <= letter <= 'Z':
self.whites.append(letter)
self.blacks = []
for letter in self.position:
if 'a' <= letter <= 'z':
self.blacks.append(letter)
def __str__(self):
return self.position
def __len__(self):
return len(self.whites) + len(self.blacks)
def __getitem__(self, pos):
column = positions[pos[0]] - 1
row = int(pos[1]) - 1
return self.table[row][column]
def get_white_score(self):
return ChessScore(self.whites)
def get_black_score(self):
return ChessScore(self.blacks)
def is_equal(self):
return self.get_white_score() == self.get_black_score()
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()
class ChessScore:
def __init__(self, figures):
self.score = 0
for figure in figures:
self.score += figures_points[figure.upper()]
def __lt__(self, other):
return self.score < other.score
def __le__(self, other):
return self.score <= other.score
def __gt__(self, other):
return self.score > other.score
def __ge__(self, other):
return self.score >= other.score
def __ne__(self, other):
return self.score != other.score
def __eq__(self, other):
return self.score == other.score
def __add__(self, other):
return self.score + other.score
def __sub__(self, other):
return self.score - other.score
def __int__(self):
return self.score

Клементина обнови решението на 29.11.2022 15:39 (преди почти 2 години)

figures_points = {'R' : 5, 'N' : 3, 'B' : 3, 'Q' : 9, 'K' : 4, 'P' : 1}
positions = {'A' : 1, 'B' : 2, 'C' : 3, 'D' : 4, 'E' : 5, 'F' : 6, 'G' : 7, 'H' :8}
-def get_position(table, elem):
- for ind, row in enumerate(table):
- if elem in row:
- return (ind, row.index(elem))
-
class ChessException(Exception):
+ pass
- def __init__(self, msg):
- self.msg = msg
- super().__init__(msg)
-
class ChessPosition:
def __init__(self, position):
self.table = position.split('/')
self.table.reverse()
- for index, elems in enumerate(self.table):
+ for index, elems in enumerate(self.table):
elems = list(elems)
ind = 0
- while(ind<len(elems)):
+ while ind<len(elems):
if isinstance(elems[ind], str) and '0' <= elems[ind] <= '9':
new_elems = elems[:ind]
new_elems.extend([None]*int(elems[ind]))
new_elems.extend(elems[ind+1:])
elems = new_elems
- ind += 1
+ ind += 1
self.table[index] = elems
- if position.count('K') != 1 or position.count('k') != 1:
- raise ChessException('kings')
- pos_k = get_position(self.table, 'k')
- pos_K = get_position(self.table, 'K')
- if (pos_K[0] - 1 <= pos_k[0] <= pos_K[0] + 1) and (pos_K[1] - 1 <= pos_k[1] <= pos_K[1] + 1):
- raise ChessException('kings')
- if('p' in self.table[-1] or 'P' in self.table[0]):
- raise ChessException('pawns')
self.position = position
+ self.check()
self.whites = []
for letter in self.position:
if 'A' <= letter <= 'Z':
self.whites.append(letter)
self.blacks = []
for letter in self.position:
if 'a' <= letter <= 'z':
self.blacks.append(letter)
+
+ def __check(self):
+ if self.position.count('K') != 1 or self.position.count('k') != 1:
+ raise ChessException('kings')
+ pos_k = self.get_position('k')
+ pos_K = self.get_position('K')
+ if (pos_K[0] - 1 <= pos_k[0] <= pos_K[0] + 1) and (pos_K[1] - 1 <= pos_k[1] <= pos_K[1] + 1):
+ raise ChessException('kings')
+ if('p' in self.table[-1] or 'P' in self.table[0]):
+ raise ChessException('pawns')
+
+ def get_position(self, elem):
+ for ind, row in enumerate(self.table):
+ if elem in row:
+ return (ind, row.index(elem))
def __str__(self):
return self.position
def __len__(self):
return len(self.whites) + len(self.blacks)
def __getitem__(self, pos):
column = positions[pos[0]] - 1
row = int(pos[1]) - 1
return self.table[row][column]
def get_white_score(self):
return ChessScore(self.whites)
def get_black_score(self):
return ChessScore(self.blacks)
def is_equal(self):
return self.get_white_score() == self.get_black_score()
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()
class ChessScore:
def __init__(self, figures):
self.score = 0
for figure in figures:
self.score += figures_points[figure.upper()]
def __lt__(self, other):
return self.score < other.score
def __le__(self, other):
return self.score <= other.score
def __gt__(self, other):
return self.score > other.score
def __ge__(self, other):
return self.score >= other.score
def __ne__(self, other):
return self.score != other.score
def __eq__(self, other):
return self.score == other.score
def __add__(self, other):
return self.score + other.score
def __sub__(self, other):
return self.score - other.score
def __int__(self):
return self.score

Клементина обнови решението на 29.11.2022 16:44 (преди почти 2 години)

figures_points = {'R' : 5, 'N' : 3, 'B' : 3, 'Q' : 9, 'K' : 4, 'P' : 1}
positions = {'A' : 1, 'B' : 2, 'C' : 3, 'D' : 4, 'E' : 5, 'F' : 6, 'G' : 7, 'H' :8}
class ChessException(Exception):
pass
class ChessPosition:
def __init__(self, position):
self.table = position.split('/')
self.table.reverse()
for index, elems in enumerate(self.table):
elems = list(elems)
ind = 0
while ind<len(elems):
- if isinstance(elems[ind], str) and '0' <= elems[ind] <= '9':
+ if isinstance(elems[ind], str) and '0' <= elems[ind] <= '8':
new_elems = elems[:ind]
new_elems.extend([None]*int(elems[ind]))
new_elems.extend(elems[ind+1:])
elems = new_elems
ind += 1
self.table[index] = elems
self.position = position
- self.check()
+ self.__check()
self.whites = []
for letter in self.position:
if 'A' <= letter <= 'Z':
self.whites.append(letter)
self.blacks = []
for letter in self.position:
if 'a' <= letter <= 'z':
self.blacks.append(letter)
def __check(self):
if self.position.count('K') != 1 or self.position.count('k') != 1:
raise ChessException('kings')
pos_k = self.get_position('k')
pos_K = self.get_position('K')
if (pos_K[0] - 1 <= pos_k[0] <= pos_K[0] + 1) and (pos_K[1] - 1 <= pos_k[1] <= pos_K[1] + 1):
raise ChessException('kings')
if('p' in self.table[-1] or 'P' in self.table[0]):
raise ChessException('pawns')
def get_position(self, elem):
for ind, row in enumerate(self.table):
if elem in row:
return (ind, row.index(elem))
def __str__(self):
return self.position
def __len__(self):
return len(self.whites) + len(self.blacks)
def __getitem__(self, pos):
column = positions[pos[0]] - 1
row = int(pos[1]) - 1
return self.table[row][column]
def get_white_score(self):
return ChessScore(self.whites)
def get_black_score(self):
return ChessScore(self.blacks)
def is_equal(self):
return self.get_white_score() == self.get_black_score()
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()
class ChessScore:
def __init__(self, figures):
self.score = 0
for figure in figures:
self.score += figures_points[figure.upper()]
def __lt__(self, other):
return self.score < other.score
def __le__(self, other):
return self.score <= other.score
def __gt__(self, other):
return self.score > other.score
def __ge__(self, other):
return self.score >= other.score
def __ne__(self, other):
return self.score != other.score
def __eq__(self, other):
return self.score == other.score
def __add__(self, other):
return self.score + other.score
def __sub__(self, other):
return self.score - other.score
def __int__(self):
return self.score