Решение на Шахматни фенове от Минчо Христов

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

Към профила на Минчо Христов

Резултати

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

Код

import re
class ChessException(Exception):
def __init__(self, message=""):
self._message = message
super().__init__(self._message)
class ChessPosition:
def __init__(self, fen):
self._whites = ChessScore(re.findall("[A-Z]", fen))
self._blacks = ChessScore(re.findall("[a-z]", fen))
self._fen = fen
self._board = []
lst_of_lines = fen.split("/")
if fen.count("k") != 1 or fen.count("K") != 1:
raise ChessException("kings")
for lst in lst_of_lines:
tmp_lst = []
for piece in lst:
if re.findall("[a-zA-Z]", piece):
tmp_lst.append(piece)
elif re.findall("[1-8]", piece):
tmp_lst.extend(["_" for _ in range(int(piece))])
self._board.append(tmp_lst)
self._board.reverse()
y_cord = 0
for row in self._board:
if row.count("k") > 0:
x_cord_left = row.index("k") - 1 if row.index("k") - 1 >= 0 else row.index("k")
x_cord_right = row.index("k") + 2 if row.index("k") + 2 <= 7 else row.index("k")
y_cord_up = y_cord - 1 if y_cord - 1 >= 0 else y_cord
y_cord_down = y_cord + 1 if y_cord + 1 <= 7 else y_cord
"K" in self._board[y_cord_down][x_cord_left:x_cord_right]
or "K" in self._board[y_cord][x_cord_left:x_cord_right]
or "K" in self._board[y_cord_up][x_cord_left:x_cord_right]
):
raise ChessException("kings")
else:
y_cord += 1
if lst_of_lines[0].count("p") > 0 or lst_of_lines[0].count("P") > 0 or \
lst_of_lines[7].count("p") > 0 or lst_of_lines[7].count("P") > 0:
raise ChessException("pawns")
def __str__(self):
return self._fen
def __len__(self):
return len(re.findall("[A-Z]", self._fen)) + len(re.findall("[A-Z]", self._fen))
def __getitem__(self, key):
x_comp, y_comp = key
if 1 < int(y_comp) or int(y_comp) > 7:
raise IndexError
x_comp = x_comp.upper()
translate = {"A": 0, "B": 1, "C": 2, "D": 3, "E": 4, "F": 5, "G": 6, "H": 7}
x_comp = translate[x_comp]
y_comp = int(y_comp) - 1
piece = self._board[y_comp][x_comp]
if piece == "_":
return None
else:
return piece
def get_white_score(self):
return self._whites
def get_black_score(self):
return self._blacks
def white_is_winning(self):
return self._whites > self._blacks
def black_is_winning(self):
return self._blacks > self._whites
def is_equal(self):
return self._whites == self._blacks
class ChessScore:
def __init__(self, figures):
self.score = 0
self._fig_to_int = {
"r": 5,
"n": 3,
"b": 3,
"k": 4,
"q": 9,
"p": 1,
"R": 5,
"N": 3,
"B": 3,
"K": 4,
"Q": 9,
"P": 1,
}
for fig in figures:
self.score += self._fig_to_int[fig]
def __lt__(self, other):
return self.score < other.score
def __le__(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 __gt__(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..F........
======================================================================
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
IndexError

======================================================================
FAIL: 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
AssertionError: 8 != 7

----------------------------------------------------------------------
Ran 17 tests in 0.183s

FAILED (failures=1, errors=1)

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

Минчо обнови решението на 24.11.2022 18:10 (преди около 2 години)

+import re
+
+
+class ChessException(Exception):
+ def __init__(self, message=""):
+ self._message = message
+ super().__init__(self._message)
+
+
+class ChessPosition:
+ def __init__(self, fen):
+ self._whites = re.findall("[A-Z]", fen)
+ self._blacks = re.findall("[a-z]", fen)
+ self._fen = fen
+ self._board = []
+ lst_of_lines = fen.split("/")
+
+ if fen.count("k") > 1 or fen.count("K") > 1:
+ raise ChessException("kings")
+
+ for lst in lst_of_lines:
+ tmp_lst = []
+ for piece in lst:
+ if re.findall("[a-zA-Z]", piece):
+ tmp_lst.append(piece)
+ elif re.findall("[1-8]", piece):
+ tmp_lst.extend(["_" for _ in range(int(piece))])
+ self._board.append(tmp_lst)
+ self._board.reverse()
+
+ y_cord = 0
+ for row in self._board:
+ if row.count("k") > 0:
+ x_cord_left = (
+ row.index("k") - 1 if row.index("k") - 1 >= 0 else row.index("k")
+ )
+ x_cord_right = (
+ row.index("k") + 2 if row.index("k") + 2 <= 7 else row.index("k")
+ )
+ y_cord_up = y_cord - 1 if y_cord - 1 >= 0 else y_cord
+ y_cord_down = y_cord + 1 if y_cord + 1 <= 7 else y_cord
+ if (
+ "K" in self._board[y_cord_down][x_cord_left:x_cord_right]
+ or "K" in self._board[y_cord][x_cord_left:x_cord_right]
+ or "K" in self._board[y_cord_up][x_cord_left:x_cord_right]
+ ):
+ raise ChessException("kings")
+ else:
+ y_cord += 1
+
+ if (
+ lst_of_lines[0].count("p") > 0
+ or lst_of_lines[0].count("P") > 0
+ or lst_of_lines[7].count("p") > 0
+ or lst_of_lines[7].count("P") > 0
+ ):
+ raise ChessException("pawns")
+
+ def _print_b(self):
+ for lst in self._board:
+ print(lst)
+
+ def __str__(self):
+ return self._fen
+
+ def __len__(self):
+ return len(self._blacks) + len(self._whites)
+
+ def __getitem__(self, key):
+ x_comp, y_comp = key
+ if 1 < int(y_comp) or int(y_comp) > 7:
+ raise IndexError
+ x_comp = x_comp.upper()
+ translate = {"A": 0, "B": 1, "C": 2, "D": 3, "E": 4, "F": 5, "G": 6, "H": 7}
+ x_comp = translate[x_comp]
+ y_comp = int(y_comp) - 1
+ piece = self._board[y_comp][x_comp]
+ if piece == "_":
+ return "None"
+ else:
+ return piece
+
+ def get_white_score(self):
+ return ChessScore(self._whites)
+
+ def get_black_score(self):
+ return ChessScore(self._blacks)
+
+ def white_is_winning(self):
+ return ChessScore(self._whites) > ChessScore(self._blacks)
+
+ def black_is_winning(self):
+ return ChessScore(self._blacks) > ChessScore(self._whites)
+
+ def is_equal(self):
+ return ChessScore(self._whites) == ChessScore(self._blacks)
+
+
+class ChessScore:
+ def __init__(self, figures):
+ self.score = 0
+ self._fig_to_int = {"r": 5, "n": 3, "b": 3, "k": 4, "q": 9, "p": 1,
+ "R": 5, "N": 3, "B": 3, "K": 4, "Q": 9, "P": 1}
+ for fig in figures:
+ self.score += self._fig_to_int[fig]
+
+ def __lt__(self, other):
+ return self.score < other.score
+
+ def __le__(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 __gt__(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

Минчо обнови решението на 24.11.2022 23:15 (преди около 2 години)

import re
class ChessException(Exception):
def __init__(self, message=""):
self._message = message
super().__init__(self._message)
class ChessPosition:
def __init__(self, fen):
- self._whites = re.findall("[A-Z]", fen)
- self._blacks = re.findall("[a-z]", fen)
+ self._whites = ChessScore(re.findall("[A-Z]", fen))
+ self._blacks = ChessScore(re.findall("[a-z]", fen))
self._fen = fen
self._board = []
lst_of_lines = fen.split("/")
- if fen.count("k") > 1 or fen.count("K") > 1:
+ if fen.count("k") != 1 or fen.count("K") != 1:
raise ChessException("kings")
for lst in lst_of_lines:
tmp_lst = []
for piece in lst:
if re.findall("[a-zA-Z]", piece):
tmp_lst.append(piece)
elif re.findall("[1-8]", piece):
tmp_lst.extend(["_" for _ in range(int(piece))])
self._board.append(tmp_lst)
self._board.reverse()
y_cord = 0
for row in self._board:
if row.count("k") > 0:
- x_cord_left = (
- row.index("k") - 1 if row.index("k") - 1 >= 0 else row.index("k")
- )
- x_cord_right = (
- row.index("k") + 2 if row.index("k") + 2 <= 7 else row.index("k")
- )
+ x_cord_left = row.index("k") - 1 if row.index("k") - 1 >= 0 else row.index("k")
+ x_cord_right = row.index("k") + 2 if row.index("k") + 2 <= 7 else row.index("k")
y_cord_up = y_cord - 1 if y_cord - 1 >= 0 else y_cord
y_cord_down = y_cord + 1 if y_cord + 1 <= 7 else y_cord
if (
"K" in self._board[y_cord_down][x_cord_left:x_cord_right]
or "K" in self._board[y_cord][x_cord_left:x_cord_right]
or "K" in self._board[y_cord_up][x_cord_left:x_cord_right]
):
raise ChessException("kings")
else:
y_cord += 1
- if (
- lst_of_lines[0].count("p") > 0
- or lst_of_lines[0].count("P") > 0
- or lst_of_lines[7].count("p") > 0
- or lst_of_lines[7].count("P") > 0
- ):
+ if lst_of_lines[0].count("p") > 0 or lst_of_lines[0].count("P") > 0 or \
+ lst_of_lines[7].count("p") > 0 or lst_of_lines[7].count("P") > 0:
raise ChessException("pawns")
- def _print_b(self):
- for lst in self._board:
- print(lst)
-
def __str__(self):
return self._fen
def __len__(self):
- return len(self._blacks) + len(self._whites)
+ return len(re.findall("[A-Z]", self._fen)) + len(re.findall("[A-Z]", self._fen))
def __getitem__(self, key):
x_comp, y_comp = key
if 1 < int(y_comp) or int(y_comp) > 7:
raise IndexError
x_comp = x_comp.upper()
translate = {"A": 0, "B": 1, "C": 2, "D": 3, "E": 4, "F": 5, "G": 6, "H": 7}
x_comp = translate[x_comp]
y_comp = int(y_comp) - 1
piece = self._board[y_comp][x_comp]
if piece == "_":
- return "None"
+ return None
else:
return piece
def get_white_score(self):
- return ChessScore(self._whites)
+ return self._whites
def get_black_score(self):
- return ChessScore(self._blacks)
+ return self._blacks
def white_is_winning(self):
- return ChessScore(self._whites) > ChessScore(self._blacks)
+ return self._whites > self._blacks
def black_is_winning(self):
- return ChessScore(self._blacks) > ChessScore(self._whites)
+ return self._blacks > self._whites
def is_equal(self):
- return ChessScore(self._whites) == ChessScore(self._blacks)
+ return self._whites == self._blacks
class ChessScore:
def __init__(self, figures):
self.score = 0
- self._fig_to_int = {"r": 5, "n": 3, "b": 3, "k": 4, "q": 9, "p": 1,
- "R": 5, "N": 3, "B": 3, "K": 4, "Q": 9, "P": 1}
+ self._fig_to_int = {
+ "r": 5,
+ "n": 3,
+ "b": 3,
+ "k": 4,
+ "q": 9,
+ "p": 1,
+ "R": 5,
+ "N": 3,
+ "B": 3,
+ "K": 4,
+ "Q": 9,
+ "P": 1,
+ }
for fig in figures:
- fig = fig
self.score += self._fig_to_int[fig]
def __lt__(self, other):
return self.score < other.score
def __le__(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 __gt__(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