Решение на Шахматни фенове от Румен Тошев

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

Към профила на Румен Тошев

Резултати

  • 8 точки от тестове
  • 0 бонус точки
  • 8 точки общо
  • 13 успешни тест(а)
  • 4 неуспешни тест(а)

Код

class ChessException(Exception):
pass
def convert_to_readable(figures):
readable = []
for row in figures:
new_row = []
for figure in row:
if figure in ['1', '2', '3', '4', '5', '6', '7', '8', '9']:

Може да минеш и само със стринг - if figure in '12345678'.
Забележи, че не смъ сложил 9 - това е невалиден инпут. Но ако имаше и девет, дори можеше просто figure.isdigit()

for i in range(int(figure)):
new_row.append('#')
else:
new_row.append(figure)
readable.append(new_row)
return readable
class ChessPosition:
def check_num_of_kings(self, figures):
small_king = 0
big_king = 0
for row in figures:
for figure in row:
if figure == 'k':
small_king += 1
elif figure == 'K':
big_king += 1
if small_king != 1 or big_king != 1:
raise ChessException('kings')
def check_kings_pos(self, figures):
for row in figures:
for figure in row:
if figure == 'k':
small_king = [row.index(figure) + 1,
figures.index(row) + 1]
x = small_king[0]
y = small_king[1]
possible_palces = [[x + 1, y], [x + 1, y + 1], [x + 1, y - 1],
[x - 1, y], [x - 1, y + 1], [x - 1, y + 1],
[x, y + 1], [x, y - 1]]
for pos in possible_palces:
if x >= 1 and x <= 8 and y >= 1 and y <= 8 and figures[y][x] == 'K':
raise ChessException("kings")
def check_pawn(self, figures):
if 'p' in figures[0] or 'p' in figures[7] or 'P' in figures[0] or 'P' in figures[7]:
raise ChessException("pawns")
def __init__(self, fen):
self.fen_untouched = fen
fen_list = fen.split('/')
fen_readable = convert_to_readable(fen_list)
self.check_num_of_kings(fen_list)
self.check_kings_pos(fen_readable)
self.check_pawn(fen_readable)
self.board = fen_readable
def get_white_score(self):
white_fig = ['R', 'N', 'B', 'Q', 'K', 'P']
our_white_fig = []
for row in self.board:
for figure in row:
if figure in white_fig:
our_white_fig.append(figure)
return ChessScore(our_white_fig)
def get_black_score(self):
black_fig = ['r', 'n', 'b', 'q', 'k', 'p']
our_black_fig = []
for row in self.board:
for figure in row:
if figure in black_fig:
our_black_fig.append(figure)
return ChessScore(our_black_fig)
def white_is_winning(self):
white = self.get_white_score()
black = self.get_black_score()
return white > black
def black_is_winning(self):
white = self.get_white_score()
black = self.get_black_score()
return white < black
def is_equal(self):
white = self.get_white_score()
black = self.get_black_score()
return white == black
def repr(self):
return self.fen_untouched
def __len__(self):
lenght = 0
for row in self.board:
for figure in row:
if figure != '#':
lenght += 1
return lenght
def __getitem__(self, str):
row_index = 8 - int(str[1])
cow_index = str[0]
cows_keys = {'A': 0, 'B': 1, 'C': 2, 'D': 3, "E": 4,
'F': 5, 'G': 6, 'H': 7}
figure = self.board[row_index][cows_keys[cow_index]]
return figure if figure != '#' else None
class ChessScore:
def __init__(self, figures):
figs = [x.lower() for x in figures]
values_of_figures = {'r': 5, 'n': 3, 'b': 3, 'q': 9,
'k': 4, 'p': 1}
self.score = sum(values_of_figures[f] for f in figs)
def __add__(self, other):
return self.score + other.score
def __sub__(self, other):
return self.score - other.score
def __lt__(self, other):
return self.score < other.score
def __gt__(self, other):
return self.score > other.score
def __le__(self, other):
return self.score <= other.score
def __ge__(self, other):
return self.score >= other.score
def __eq__(self, other):
return self.score == other.score
def __ne__(self, other):
return self.score != other.score
def __int__(self):
return self.score

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

.E......E.FF.....
======================================================================
ERROR: test_against_touching_kings (test.TestChessPosition)
Test for kings next to each other.
----------------------------------------------------------------------
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: k7/K7/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
IndexError: list index out of range

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

FAILED (failures=2, errors=2)

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

Румен обнови решението на 28.11.2022 23:57 (преди почти 2 години)

+class ChessException(Exception):
+ pass
+
+
+def check_num_of_kings(figures):
+ small_king = 0
+ big_king = 0
+ for row in figures:
+ for figure in row:
+ if figure == 'k':
+ small_king += 1
+ elif figure == 'K':
+ big_king += 1
+ if small_king != 1 or big_king != 1:
+ raise ChessException('kings')
+
+
+def check_kings_pos(figures):
+
+ for row in figures:
+ for figure in row:
+ if figure == 'k':
+ small_king = [row.index(figure) + 1, figures.index(row) + 1]
+
+ x = small_king[0]
+ y = small_king[1]
+ possible_palces = [[x + 1, y], [x + 1, y + 1], [x + 1, y - 1],
+ [x - 1, y], [x - 1, y + 1], [x - 1, y + 1],
+ [x, y + 1], [x, y - 1]]
+
+ for pos in possible_palces:
+ if x >= 1 and x <= 8 and y >= 1 and y <= 8 and figures[y][x] == 'K':
+ raise ChessException("kings")
+
+def check_pawn(figures):
+ if 'p' in figures[0] or 'p' in figures[7] or 'P' in figures[0] or 'P' in figures[7]:
+ raise ChessException("pawns")
+
+def convert_to_readable(figures):
+ readable = []
+ for row in figures:
+ new_row = []
+ for figure in row:
+ if figure in ['1', '2', '3', '4', '5', '6', '7', '8', '9']:

Може да минеш и само със стринг - if figure in '12345678'.
Забележи, че не смъ сложил 9 - това е невалиден инпут. Но ако имаше и девет, дори можеше просто figure.isdigit()

+ for i in range(int(figure)):
+ new_row.append('#')
+ else:
+ new_row.append(figure)
+ readable.append(new_row)
+
+ return readable
+
+
+class ChessPosition:
+
+ def __init__(self, fen):
+
+ self.fen_untouched = fen
+ fen_list = fen.split('/')
+ fen_readable = convert_to_readable(fen_list)
+
+ check_num_of_kings(fen_list)
+ check_kings_pos(fen_readable)
+ check_pawn(fen_readable)
+
+ self.board = fen_readable
+
+ def get_white_score(self):
+ white_fig = ['R','N','B','Q','K','P']
+ our_white_fig = []
+ for row in self.board:
+ for figure in row:
+ if figure in white_fig:
+ our_white_fig.append(figure)
+ return ChessScore(our_white_fig)
+
+ def get_black_score(self):
+ black_fig = ['r','n','b','q','k','p']
+ our_black_fig = []
+ for row in self.board:
+ for figure in row:
+ if figure in black_fig:
+ our_black_fig.append(figure)
+ return ChessScore(our_black_fig)
+
+ def white_is_winning(self):
+ white = self.get_white_score()
+ black = self.get_black_score()
+
+ return white > black
+
+ def black_is_winning(self):
+ white = self.get_white_score()
+ black = self.get_black_score()
+
+ return white < black
+
+ def is_equal(self):
+ white = self.get_white_score()
+ black = self.get_black_score()
+
+ return white == black
+
+ def repr(self):
+ return self.fen_untouched
+
+ def __len__ (self):
+ lenght = 0
+ for row in self.board:
+ for figure in row:
+ if figure != '#':
+ lenght += 1
+
+ return lenght
+
+ def __getitem__(self,str):
+
+ row_index = 8 - int(str[1])
+ cow_index = str[0]
+
+ cows_keys = {'A': 0,'B':1,'C':2,'D':3,"E":4,
+ 'F':5 ,'G':6, 'H':7}
+
+ figure = self.board[row_index][cows_keys[cow_index]]
+ return figure if figure != '#' else None
+
+
+class ChessScore:
+ def __init__(self, figures):
+ figs = [x.lower() for x in figures]
+ values_of_figures = {'r': 5, 'n': 3, 'b': 3, 'q': 9,
+ 'k': 4, 'p': 1}
+ self.score = sum(values_of_figures [f] for f in figs)
+
+ def __add__(self, other):
+ return self.score + other.score
+
+ def __sub__(self, other):
+ return self.score - other.score
+
+ def __lt__(self, other):
+ return self.score < other.score
+
+ def __gt__(self, other):
+ return self.score > other.score
+
+ def __le__(self, other):
+ return self.score <= other.score
+
+ def __ge__(self, other):
+ return self.score >= other.score
+
+ def __eq__(self, other):
+ return self.score == other.score
+
+ def __ne__(self, other):
+ return self.score != other.score
+
+ def __int__(self):
+ return self.score

Румен обнови решението на 29.11.2022 13:37 (преди почти 2 години)

class ChessException(Exception):
pass
-def check_num_of_kings(figures):
- small_king = 0
- big_king = 0
- for row in figures:
- for figure in row:
- if figure == 'k':
- small_king += 1
- elif figure == 'K':
- big_king += 1
- if small_king != 1 or big_king != 1:
- raise ChessException('kings')
-
-
-def check_kings_pos(figures):
-
- for row in figures:
- for figure in row:
- if figure == 'k':
- small_king = [row.index(figure) + 1, figures.index(row) + 1]
-
- x = small_king[0]
- y = small_king[1]
- possible_palces = [[x + 1, y], [x + 1, y + 1], [x + 1, y - 1],
- [x - 1, y], [x - 1, y + 1], [x - 1, y + 1],
- [x, y + 1], [x, y - 1]]
-
- for pos in possible_palces:
- if x >= 1 and x <= 8 and y >= 1 and y <= 8 and figures[y][x] == 'K':
- raise ChessException("kings")
-
-def check_pawn(figures):
- if 'p' in figures[0] or 'p' in figures[7] or 'P' in figures[0] or 'P' in figures[7]:
- raise ChessException("pawns")
-
def convert_to_readable(figures):
readable = []
for row in figures:
new_row = []
for figure in row:
if figure in ['1', '2', '3', '4', '5', '6', '7', '8', '9']:
for i in range(int(figure)):
new_row.append('#')
else:
new_row.append(figure)
readable.append(new_row)
return readable
class ChessPosition:
+ def check_num_of_kings(self, figures):
+ small_king = 0
+ big_king = 0
+ for row in figures:
+ for figure in row:
+ if figure == 'k':
+ small_king += 1
+ elif figure == 'K':
+ big_king += 1
+ if small_king != 1 or big_king != 1:
+ raise ChessException('kings')
+
+ def check_kings_pos(self, figures):
+
+ for row in figures:
+ for figure in row:
+ if figure == 'k':
+ small_king = [row.index(figure) + 1,
+ figures.index(row) + 1]
+
+ x = small_king[0]
+ y = small_king[1]
+ possible_palces = [[x + 1, y], [x + 1, y + 1], [x + 1, y - 1],
+ [x - 1, y], [x - 1, y + 1], [x - 1, y + 1],
+ [x, y + 1], [x, y - 1]]
+
+ for pos in possible_palces:
+ if x >= 1 and x <= 8 and y >= 1 and y <= 8 and figures[y][x] == 'K':
+ raise ChessException("kings")
+
+ def check_pawn(self, figures):
+ if 'p' in figures[0] or 'p' in figures[7] or 'P' in figures[0] or 'P' in figures[7]:
+ raise ChessException("pawns")
+
def __init__(self, fen):
self.fen_untouched = fen
fen_list = fen.split('/')
fen_readable = convert_to_readable(fen_list)
- check_num_of_kings(fen_list)
- check_kings_pos(fen_readable)
- check_pawn(fen_readable)
+ self.check_num_of_kings(fen_list)
+ self.check_kings_pos(fen_readable)
+ self.check_pawn(fen_readable)
self.board = fen_readable
def get_white_score(self):
- white_fig = ['R','N','B','Q','K','P']
+ white_fig = ['R', 'N', 'B', 'Q', 'K', 'P']
our_white_fig = []
for row in self.board:
for figure in row:
if figure in white_fig:
our_white_fig.append(figure)
return ChessScore(our_white_fig)
def get_black_score(self):
- black_fig = ['r','n','b','q','k','p']
+ black_fig = ['r', 'n', 'b', 'q', 'k', 'p']
our_black_fig = []
for row in self.board:
for figure in row:
if figure in black_fig:
our_black_fig.append(figure)
return ChessScore(our_black_fig)
def white_is_winning(self):
white = self.get_white_score()
black = self.get_black_score()
return white > black
def black_is_winning(self):
white = self.get_white_score()
black = self.get_black_score()
return white < black
def is_equal(self):
white = self.get_white_score()
black = self.get_black_score()
return white == black
def repr(self):
return self.fen_untouched
- def __len__ (self):
+ def __len__(self):
lenght = 0
for row in self.board:
for figure in row:
if figure != '#':
lenght += 1
return lenght
- def __getitem__(self,str):
+ def __getitem__(self, str):
row_index = 8 - int(str[1])
cow_index = str[0]
- cows_keys = {'A': 0,'B':1,'C':2,'D':3,"E":4,
- 'F':5 ,'G':6, 'H':7}
+ cows_keys = {'A': 0, 'B': 1, 'C': 2, 'D': 3, "E": 4,
+ 'F': 5, 'G': 6, 'H': 7}
figure = self.board[row_index][cows_keys[cow_index]]
return figure if figure != '#' else None
class ChessScore:
def __init__(self, figures):
figs = [x.lower() for x in figures]
values_of_figures = {'r': 5, 'n': 3, 'b': 3, 'q': 9,
'k': 4, 'p': 1}
- self.score = sum(values_of_figures [f] for f in figs)
+ self.score = sum(values_of_figures[f] for f in figs)
def __add__(self, other):
return self.score + other.score
def __sub__(self, other):
return self.score - other.score
def __lt__(self, other):
return self.score < other.score
def __gt__(self, other):
return self.score > other.score
def __le__(self, other):
return self.score <= other.score
def __ge__(self, other):
return self.score >= other.score
def __eq__(self, other):
return self.score == other.score
def __ne__(self, other):
return self.score != other.score
def __int__(self):
return self.score