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

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

Към профила на Велислава Крумова

Резултати

  • 3 точки от тестове
  • 0 бонус точки
  • 3 точки общо
  • 5 успешни тест(а)
  • 12 неуспешни тест(а)

Код

figures = {'r': 5, 'n': 3, 'b': 3, 'k': 4, 'q': 9, 'p': 1, 'R': 5, 'N': 3, 'B': 3, 'K': 4, 'Q': 9, 'P': 1}
def get_neighbours(king_pos):

Това е прекалено сложно. Опитай да парснеш фен-а към масив/речник... После можеш да опиташ да вземеш координатите на царете по-лесно, след което да изчислиш разстоянието между тях.

neighbours = []
left_figure = chr(ord(king_pos[0]) - 1) + king_pos[1]
right_figure = chr(ord(king_pos[0]) + 1) + king_pos[1]
up_figure = king_pos[0] + str(int(king_pos[1]) + 1)
down_figure = king_pos[0] + str(int(king_pos[1]) - 1)
up_left_figure = chr(ord(king_pos[0]) - 1) + str(int(king_pos[1]) + 1)
up_right_figure = chr(ord(king_pos[0]) + 1) + str(int(king_pos[1]) + 1)
down_left_figure = chr(ord(king_pos[0]) - 1) + str(int(king_pos[1]) - 1)
down_right_figure = chr(ord(king_pos[0]) + 1) + str(int(king_pos[1]) - 1)
if king_pos == 'A1':
neighbours.extend([up_figure, up_right_figure, right_figure])
elif king_pos == 'A8':
neighbours.extend([right_figure, down_right_figure, down_figure])
elif king_pos == 'H8':
neighbours.extend([left_figure, down_left_figure, down_figure])
elif king_pos == 'H1':
neighbours.extend([up_figure, up_left_figure, left_figure])
elif king_pos[1] == '1':
neighbours.extend([up_figure, up_left_figure, left_figure, up_right_figure, right_figure])
elif king_pos[1] == '8':
neighbours.extend([down_figure, down_right_figure, down_left_figure, left_figure, right_figure])
else:
neighbours.extend(
[down_figure, down_right_figure, down_left_figure, left_figure, right_figure, up_figure, up_left_figure,
up_right_figure])
return neighbours
class ChessException(Exception):
pass
class ChessScore:
def __init__(self, figure_list):
self.figure_list = figure_list
def __int__(self):
points = 0
for el in self.figure_list:
if el in figures:
points += figures[el]
return points
def __add__(self, other):
return int(self.figure_list) + int(other.figure_list)
def __sub__(self, other):
return int(self.figure_list) - int(other.figure_list)
def __lt__(self, other):
return int(self.figure_list) < int(other.figure_list)
def __le__(self, other):
return int(self.figure_list) <= int(other.figure_list)
def __eq__(self, other):
return int(self.figure_list) == int(other.figure_list)
def __ne__(self, other):
return int(self.figure_list) != int(other.figure_list)
def __gt__(self, other):
return int(self.figure_list) > int(other.figure_list)
def __ge__(self, other):
return int(self.figure_list) >= int(other.figure_list)
class ChessPosition:
def __init__(self, def_str):
self.def_str = def_str
self._rows = self.def_str.split('/')
white_king = 0
black_king = 0
for row in self._rows:
for char in row:
if char == 'k':
white_king += 1
king_pos = chr(ord('@')+row.find(char) + 1) + str(self._rows.index(row) + 1)

Това с ord хич не ми допада, защото трябва да знам къде е @, за да преценя какво става. +1 причина предварително да парснеш фена в нещо, което после лесно можеш да използваш.

neighbour_fig = get_neighbours(king_pos)
if white_king > 1 or white_king < 1:
raise ChessException("kings")
for fig in neighbour_fig:
if self.position(fig) == 'K':
raise ChessException("kings")
elif char == 'K':
black_king += 1
king_pos = chr(ord('@') + row.find(char) + 1) + str(self._rows.index(row) + 1)
neighbour_figs = get_neighbours(king_pos)
for figure in neighbour_figs:
if self.position(figure) == 'k':
raise ChessException("kings")
if black_king > 1 or black_king < 1:
raise ChessException("kings")
elif char == 'p' or char == 'P':
pawn_row = self._rows.index(row) + 1
if pawn_row == 1 or pawn_row == 8:
raise ChessException("pawns")
def print(self):
print(self.def_str)
def get_white_score(self):
all_whites = []
for row in self._rows:
for char in row:
if char.isalpha() and char.isupper():
all_whites.append(char)
return ChessScore.__int__(ChessScore(all_whites))
def get_black_score(self):
all_black = []
for row in self._rows:
for char in row:
if char.isalpha() and char.islower():
all_black.append(char)
return ChessScore.__int__(ChessScore(all_black))
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 position(self, pos_check):
column = ord(pos_check[0]) - 65
row = int(pos_check[1]) - 1
if column < len(self._rows[row]):
if (self._rows[row])[column] in '12345678':
return None
else:
return (self._rows[row])[column]
else:
return None
def len(self):
figure_count = 0
for row in self._rows:
for char in row:
if char.isalpha():
figure_count += 1
return figure_count

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

..EEEEEEE.FFEEE..
======================================================================
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_king_count (test.TestChessPosition)
Test for missing or multiple kings.
----------------------------------------------------------------------
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/8/8/8/8/8/8

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

======================================================================
ERROR: test_basic_arithmetic (test.TestChessScore)
Test additiona and subtraction of ChessScores.
----------------------------------------------------------------------
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: int() argument must be a string, a bytes-like object or a real number, not 'list'

======================================================================
ERROR: test_comparison (test.TestChessScore)
Test correct comparison on a pair of scores.
----------------------------------------------------------------------
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: int() argument must be a string, a bytes-like object or a real number, not 'list'

======================================================================
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 0xffff9a0b6380>' != '6k1/5p2/6p1/8/7p/8/6PP/6K1'
- <solution.ChessPosition object at 0xffff9a0b6380>
+ 6k1/5p2/6p1/8/7p/8/6PP/6K1


======================================================================
FAIL: test_validation_conflict (test.TestChessPosition)
Test for correct Exception on multiple validation fails.
----------------------------------------------------------------------
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: 'pawns' != 'kings'
- pawns
+ kings


----------------------------------------------------------------------
Ran 17 tests in 0.169s

FAILED (failures=2, errors=10)

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

Велислава обнови решението на 26.11.2022 10:40 (преди почти 2 години)

+figures = {'r': 5, 'n': 3, 'b': 3, 'k': 4, 'q': 9, 'p': 1, 'R': 5, 'N': 3, 'B': 3, 'K': 4, 'Q': 9, 'P': 1}
+
+
+def get_neighbours(king_pos):

Това е прекалено сложно. Опитай да парснеш фен-а към масив/речник... После можеш да опиташ да вземеш координатите на царете по-лесно, след което да изчислиш разстоянието между тях.

+ neighbours = []
+ left_figure = chr(ord(king_pos[0]) - 1) + king_pos[1]
+ right_figure = chr(ord(king_pos[0]) + 1) + king_pos[1]
+ up_figure = king_pos[0] + str(int(king_pos[1]) + 1)
+ down_figure = king_pos[0] + str(int(king_pos[1]) - 1)
+ up_left_figure = chr(ord(king_pos[0]) - 1) + str(int(king_pos[1]) + 1)
+ up_right_figure = chr(ord(king_pos[0]) + 1) + str(int(king_pos[1]) + 1)
+ down_left_figure = chr(ord(king_pos[0]) - 1) + str(int(king_pos[1]) - 1)
+ down_right_figure = chr(ord(king_pos[0]) + 1) + str(int(king_pos[1]) - 1)
+ if king_pos == 'A1':
+ neighbours.extend([up_figure, up_right_figure, right_figure])
+ elif king_pos == 'A8':
+ neighbours.extend([right_figure, down_right_figure, down_figure])
+ elif king_pos == 'H8':
+ neighbours.extend([left_figure, down_left_figure, down_figure])
+ elif king_pos == 'H1':
+ neighbours.extend([up_figure, up_left_figure, left_figure])
+ elif king_pos[1] == '1':
+ neighbours.extend([up_figure, up_left_figure, left_figure, up_right_figure, right_figure])
+ elif king_pos[1] == '8':
+ neighbours.extend([down_figure, down_right_figure, down_left_figure, left_figure, right_figure])
+ else:
+ neighbours.extend(
+ [down_figure, down_right_figure, down_left_figure, left_figure, right_figure, up_figure, up_left_figure,
+ up_right_figure])
+ return neighbours
+
+
+class ChessException(Exception):
+ pass
+
+
+class ChessScore:
+ def __init__(self, figure_list):
+ self.figure_list = figure_list
+
+ def __int__(self):
+ points = 0
+ for el in self.figure_list:
+ if el in figures:
+ points += figures[el]
+ return points
+
+ def __add__(self, other):
+ return int(self.figure_list) + int(other.figure_list)
+
+ def __sub__(self, other):
+ return int(self.figure_list) - int(other.figure_list)
+
+ def __lt__(self, other):
+ return int(self.figure_list) < int(other.figure_list)
+
+ def __le__(self, other):
+ return int(self.figure_list) <= int(other.figure_list)
+
+ def __eq__(self, other):
+ return int(self.figure_list) == int(other.figure_list)
+
+ def __ne__(self, other):
+ return int(self.figure_list) != int(other.figure_list)
+
+ def __gt__(self, other):
+ return int(self.figure_list) > int(other.figure_list)
+
+ def __ge__(self, other):
+ return int(self.figure_list) >= int(other.figure_list)
+
+
+class ChessPosition:
+
+ def __init__(self, def_str):
+ self.def_str = def_str
+ self._rows = self.def_str.split('/')
+ white_king = 0
+ black_king = 0
+ for row in self._rows:
+ for char in row:
+ if char == 'k':
+ white_king += 1
+ king_pos = chr(ord('@')+row.find(char) + 1) + str(self._rows.index(row) + 1)

Това с ord хич не ми допада, защото трябва да знам къде е @, за да преценя какво става. +1 причина предварително да парснеш фена в нещо, което после лесно можеш да използваш.

+ neighbour_fig = get_neighbours(king_pos)
+ if white_king > 1 or white_king < 1:
+ raise ChessException("kings")
+ for fig in neighbour_fig:
+ if self.position(fig) == 'K':
+ raise ChessException("kings")
+ elif char == 'K':
+ black_king += 1
+ king_pos = chr(ord('@') + row.find(char) + 1) + str(self._rows.index(row) + 1)
+ neighbour_figs = get_neighbours(king_pos)
+ for figure in neighbour_figs:
+ if self.position(figure) == 'k':
+ raise ChessException("kings")
+ if black_king > 1 or black_king < 1:
+ raise ChessException("kings")
+ elif char == 'p' or char == 'P':
+ pawn_row = self._rows.index(row) + 1
+ if pawn_row == 1 or pawn_row == 8:
+ raise ChessException("pawns")
+
+ def print(self):
+ print(self.def_str)
+
+ def get_white_score(self):
+ all_whites = []
+ for row in self._rows:
+ for char in row:
+ if char.isalpha() and char.isupper():
+ all_whites.append(char)
+ return ChessScore.__int__(ChessScore(all_whites))
+
+ def get_black_score(self):
+ all_black = []
+ for row in self._rows:
+ for char in row:
+ if char.isalpha() and char.islower():
+ all_black.append(char)
+ return ChessScore.__int__(ChessScore(all_black))
+
+ 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 position(self, pos_check):
+ column = ord(pos_check[0]) - 65
+ row = int(pos_check[1]) - 1
+ if column < len(self._rows[row]):
+ if (self._rows[row])[column] in '12345678':
+ return None
+ else:
+ return (self._rows[row])[column]
+ else:
+ return None
+
+ def len(self):
+ figure_count = 0
+ for row in self._rows:
+ for char in row:
+ if char.isalpha():
+ figure_count += 1
+ return figure_count