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

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

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

Резултати

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

Код

import re
class ChessException(Exception):
pass
class ChessPosition:
@staticmethod
def chess_desk(string):
for row in string.split('/'):
for column in row:
if column.isdigit():
string = string.replace(column, 'X' * eval(column))
return string.split('/')
@staticmethod
def is_king(piece):
return piece in ('k', 'K')
@staticmethod
def has_adjascent_kings(chessDesk, row, column):
for i in [-1, 0, 1]:
for j in [-1, 0, 1]:
if chessDesk[row + i][column + j] == chessDesk[row][column] and not (i, j) == (0, 0):
return True
return False
@staticmethod
def is_valid_index(row, column):
isInRange = lambda number : 0 <= number - 1 <= number + 1 <= 7
return isInRange(row) and isInRange(column)
def __init__(self, string):
chessDeck = self.chess_desk(string)
for i, row in enumerate(chessDeck):
for j, piece in enumerate(row):
if (self.is_king(piece) and self.is_valid_index(i, j) and self.has_adjascent_kings(chessDeck, i, j)):
raise ChessException("kings")
if (string.count('K'), string.count('k')) != (1, 1):
raise ChessException("kings")
for i, row in enumerate(string.split('/')):
if (re.search(r'[P]{1}', row, re.IGNORECASE) and i in (0, 7)):
raise ChessException("pawns")
self.string = string
self.chessDesk = self.chess_desk(string)
def get_white_score(self):
points = {'R': 5, 'N': 3, 'B': 3, 'Q': 9, 'K': 4, 'P': 1}
return sum([points[char] for char in self.string if char in points.keys()])
def get_black_score(self):
points = {'r': 5, 'n': 3, 'b': 3, 'q': 9, 'k': 4, 'p': 1}
return sum([points[char] for char in self.string if char in points.keys()])
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_black_score() == self.get_white_score()
def __str__(self):
return self.string
def __len__(self):
return len(re.findall(r'[RNBQKP]', self.string, re.IGNORECASE))
def __getitem__(self, key):
row = 8 - int(key[1])
column = ord(key[0]) - ord('A')
return self.chessDesk[row][column] if self.chessDesk[row][column] != 'X' else None
class ChessScore():
def __init__(self, pieces):
assert type(pieces) == list, 'ChessScore must have a list as an argument.'
for piece in pieces:
assert piece in ('r', 'n', 'b', 'q', 'k', 'p'), 'One or more pieces are not identified as a black not-pawn piece.'
self.pieces = pieces
def __int__(self):
points = {'r': 5, 'n': 3, 'b': 3, 'q': 9, 'k': 4, 'p': 1}
return sum([points[char] for char in self.pieces if char in points.keys()])
def __lt__(self, other):
return int(self) < int(other)
def __le__(self, other):
return int(self) <= int(other)
def __gt__(self, other):
return int(self) > int(other)
def __eq__(self, other):
return int(self) == int(other)
def __ne__(self, other):
return int(self) != int(other)
def __add__(self, other):
return int(self) + int(other)
def __sub__(self, other):
return int(self) - int(other)

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

.E..F......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

======================================================================
FAIL: 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
AssertionError: 4 is not an instance of <class 'solution.ChessScore'>

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


======================================================================
FAIL: 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
AssertionError: 4 is not an instance of <class 'solution.ChessScore'>

----------------------------------------------------------------------
Ran 17 tests in 0.190s

FAILED (failures=3, errors=1)

История (14 версии и 6 коментара)

Мариян обнови решението на 27.11.2022 16:31 (преди над 1 година)

+import re
+
+
+class ChessException(Exception):
+ pass
+
+
+class MultipleKings(ChessException):
+ pass
+
+
+class AdjascentKings(ChessException):
+ pass
+
+
+class FirstLinePawn(ChessException):
+ pass
+
+
+def ChessDesk(str):
+ for row in str.split('/'):
+ for column in row:
+ if column.isdigit():
+ str = str.replace(column, 'X' * eval(column))
+ return str.split('/')
+
+
+def isKing(piece):
+ return piece in ('k', 'K')
+
+
+def HasAdjascentKings(chessDesk, row, column):
+ for i in [-1, 0, 1]:
+ for j in [-1, 0, 1]:
+ if chessDesk[row+i][column+j] == chessDesk[row][column] and not (i, j) == (0, 0):
+ return True
+ return False
+
+
+def IsValidIndex(i, j):
+ def isInRange(number):
+ return 0 <= number - 1 <= number + 1 <= 7
+ return isInRange(i) and isInRange(j)
+
+
+class ChessPosition():
+ def __init__(self, str):
+ try:
+ for i, row in enumerate(ChessDesk(str)):
+ for j, piece in enumerate(row):
+ if (isKing(piece) and IsValidIndex(i, j) and HasAdjascentKings(ChessDesk(str), i, j)):
+ raise AdjascentKings
+ if (str.count('K') != 1 or str.count('k') != 1):
+ raise MultipleKings
+ for i, row in enumerate(str.split('/')):
+ if (re.search(r'[P]{1}', row, re.IGNORECASE) and i in (0, 7)):
+ raise FirstLinePawn
+ except AdjascentKings:
+ print("kings")
+ except MultipleKings:
+ print("kings")
+ except FirstLinePawn:
+ print("pawns")
+ else:
+ self.str = str
+ self.chessDesk = ChessDesk(str)
+
+ def get_white_score(self):
+ points = {'R': 5, 'N': 3, 'B': 3, 'Q': 9, 'K': 4, 'P': 1}
+ return sum([points[char] for char in self.str if char in points.keys()])
+
+ def get_black_score(self):
+ points = {'r': 5, 'n': 3, 'b': 3, 'q': 9, 'k': 4, 'p': 1}
+ return sum([points[char] for char in self.str if char in points.keys()])
+
+ 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_black_score() == self.get_white_score()
+
+ def __str__(self):
+ return self.str
+
+ def __len__(self):
+ return len(re.findall(r'[RrNnBbQqKkPp]', self.str))
+
+ def __getitem__(self, key):
+ return self.chessDesk[7-(ord(key[0])-ord('A'))][int(key[1])-1]
+
+
+class ChessScore():
+ def __init__(self, pieces):
+ self.pieces = pieces
+
+ def __int__(self):
+ points = {'r': 5, 'n': 3, 'b': 3, 'q': 9, 'k': 4, 'p': 1}
+ return sum([points[char] for char in self.pieces if char in points.keys()])
+
+ def __lt__(self, other):
+ return int(self) < int(other)
+
+ def __le__(self, other):
+ return int(self) <= int(other)
+
+ def __gt__(self, other):
+ return int(self) > int(other)
+
+ def __eq__(self, other):
+ return int(self) == int(other)
+
+ def __ne__(self, other):
+ return int(self) != int(other)

Мариян обнови решението на 27.11.2022 16:42 (преди над 1 година)

import re
class ChessException(Exception):
pass
class MultipleKings(ChessException):
pass
class AdjascentKings(ChessException):
pass
class FirstLinePawn(ChessException):
pass
def ChessDesk(str):
for row in str.split('/'):
for column in row:
if column.isdigit():
str = str.replace(column, 'X' * eval(column))
return str.split('/')
def isKing(piece):
return piece in ('k', 'K')
def HasAdjascentKings(chessDesk, row, column):
for i in [-1, 0, 1]:
for j in [-1, 0, 1]:
- if chessDesk[row+i][column+j] == chessDesk[row][column] and not (i, j) == (0, 0):
+ if chessDesk[row + i][column + j] == chessDesk[row][column] and not (i, j) == (0, 0):
return True
return False
def IsValidIndex(i, j):
- def isInRange(number):
- return 0 <= number - 1 <= number + 1 <= 7
+ isInRange = lambda number: 0 <= number - 1 <= number + 1 <= 7
return isInRange(i) and isInRange(j)
class ChessPosition():
def __init__(self, str):
try:
for i, row in enumerate(ChessDesk(str)):
for j, piece in enumerate(row):
if (isKing(piece) and IsValidIndex(i, j) and HasAdjascentKings(ChessDesk(str), i, j)):
raise AdjascentKings
if (str.count('K') != 1 or str.count('k') != 1):
raise MultipleKings
for i, row in enumerate(str.split('/')):
if (re.search(r'[P]{1}', row, re.IGNORECASE) and i in (0, 7)):
raise FirstLinePawn
except AdjascentKings:
print("kings")
except MultipleKings:
print("kings")
except FirstLinePawn:
print("pawns")
else:
self.str = str
self.chessDesk = ChessDesk(str)
def get_white_score(self):
points = {'R': 5, 'N': 3, 'B': 3, 'Q': 9, 'K': 4, 'P': 1}
return sum([points[char] for char in self.str if char in points.keys()])
def get_black_score(self):
points = {'r': 5, 'n': 3, 'b': 3, 'q': 9, 'k': 4, 'p': 1}
return sum([points[char] for char in self.str if char in points.keys()])
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_black_score() == self.get_white_score()
def __str__(self):
return self.str
def __len__(self):
return len(re.findall(r'[RrNnBbQqKkPp]', self.str))
def __getitem__(self, key):
return self.chessDesk[7-(ord(key[0])-ord('A'))][int(key[1])-1]
class ChessScore():
def __init__(self, pieces):
self.pieces = pieces
def __int__(self):
points = {'r': 5, 'n': 3, 'b': 3, 'q': 9, 'k': 4, 'p': 1}
return sum([points[char] for char in self.pieces if char in points.keys()])
def __lt__(self, other):
return int(self) < int(other)
def __le__(self, other):
return int(self) <= int(other)
def __gt__(self, other):
return int(self) > int(other)
def __eq__(self, other):
return int(self) == int(other)
def __ne__(self, other):
- return int(self) != int(other)
+ return int(self) != int(other)

Мариян обнови решението на 27.11.2022 16:44 (преди над 1 година)

import re
class ChessException(Exception):
pass
class MultipleKings(ChessException):
pass
class AdjascentKings(ChessException):
pass
class FirstLinePawn(ChessException):
pass
def ChessDesk(str):
for row in str.split('/'):
for column in row:
if column.isdigit():
str = str.replace(column, 'X' * eval(column))
return str.split('/')
def isKing(piece):
return piece in ('k', 'K')
def HasAdjascentKings(chessDesk, row, column):
for i in [-1, 0, 1]:
for j in [-1, 0, 1]:
if chessDesk[row + i][column + j] == chessDesk[row][column] and not (i, j) == (0, 0):
return True
return False
def IsValidIndex(i, j):
isInRange = lambda number: 0 <= number - 1 <= number + 1 <= 7
return isInRange(i) and isInRange(j)
class ChessPosition():
def __init__(self, str):
try:
for i, row in enumerate(ChessDesk(str)):
for j, piece in enumerate(row):
if (isKing(piece) and IsValidIndex(i, j) and HasAdjascentKings(ChessDesk(str), i, j)):
raise AdjascentKings
if (str.count('K') != 1 or str.count('k') != 1):
raise MultipleKings
for i, row in enumerate(str.split('/')):
if (re.search(r'[P]{1}', row, re.IGNORECASE) and i in (0, 7)):
raise FirstLinePawn
except AdjascentKings:
print("kings")
except MultipleKings:
print("kings")
except FirstLinePawn:
print("pawns")
else:
self.str = str
self.chessDesk = ChessDesk(str)
def get_white_score(self):
points = {'R': 5, 'N': 3, 'B': 3, 'Q': 9, 'K': 4, 'P': 1}
return sum([points[char] for char in self.str if char in points.keys()])
def get_black_score(self):
points = {'r': 5, 'n': 3, 'b': 3, 'q': 9, 'k': 4, 'p': 1}
return sum([points[char] for char in self.str if char in points.keys()])
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_black_score() == self.get_white_score()
def __str__(self):
return self.str
def __len__(self):
return len(re.findall(r'[RrNnBbQqKkPp]', self.str))
def __getitem__(self, key):
- return self.chessDesk[7-(ord(key[0])-ord('A'))][int(key[1])-1]
+ return self.chessDesk[7 - (ord(key[0]) - ord('A'))][int(key[1]) - 1]
class ChessScore():
def __init__(self, pieces):
self.pieces = pieces
def __int__(self):
points = {'r': 5, 'n': 3, 'b': 3, 'q': 9, 'k': 4, 'p': 1}
return sum([points[char] for char in self.pieces if char in points.keys()])
def __lt__(self, other):
return int(self) < int(other)
def __le__(self, other):
return int(self) <= int(other)
def __gt__(self, other):
return int(self) > int(other)
def __eq__(self, other):
return int(self) == int(other)
def __ne__(self, other):
return int(self) != int(other)

Мариян обнови решението на 27.11.2022 16:52 (преди над 1 година)

import re
class ChessException(Exception):
pass
class MultipleKings(ChessException):
pass
class AdjascentKings(ChessException):
pass
class FirstLinePawn(ChessException):
pass
def ChessDesk(str):
for row in str.split('/'):
for column in row:
if column.isdigit():
str = str.replace(column, 'X' * eval(column))
return str.split('/')
def isKing(piece):
return piece in ('k', 'K')
def HasAdjascentKings(chessDesk, row, column):
for i in [-1, 0, 1]:
for j in [-1, 0, 1]:
if chessDesk[row + i][column + j] == chessDesk[row][column] and not (i, j) == (0, 0):
return True
return False
def IsValidIndex(i, j):
isInRange = lambda number: 0 <= number - 1 <= number + 1 <= 7
return isInRange(i) and isInRange(j)
class ChessPosition():
def __init__(self, str):
try:
for i, row in enumerate(ChessDesk(str)):
for j, piece in enumerate(row):
if (isKing(piece) and IsValidIndex(i, j) and HasAdjascentKings(ChessDesk(str), i, j)):
raise AdjascentKings
if (str.count('K') != 1 or str.count('k') != 1):
raise MultipleKings
for i, row in enumerate(str.split('/')):
if (re.search(r'[P]{1}', row, re.IGNORECASE) and i in (0, 7)):
raise FirstLinePawn
except AdjascentKings:
print("kings")
except MultipleKings:
print("kings")
except FirstLinePawn:
print("pawns")
else:
self.str = str
self.chessDesk = ChessDesk(str)
def get_white_score(self):
points = {'R': 5, 'N': 3, 'B': 3, 'Q': 9, 'K': 4, 'P': 1}
return sum([points[char] for char in self.str if char in points.keys()])
def get_black_score(self):
points = {'r': 5, 'n': 3, 'b': 3, 'q': 9, 'k': 4, 'p': 1}
return sum([points[char] for char in self.str if char in points.keys()])
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_black_score() == self.get_white_score()
def __str__(self):
return self.str
def __len__(self):
return len(re.findall(r'[RrNnBbQqKkPp]', self.str))
def __getitem__(self, key):
return self.chessDesk[7 - (ord(key[0]) - ord('A'))][int(key[1]) - 1]
class ChessScore():
def __init__(self, pieces):
self.pieces = pieces
def __int__(self):
points = {'r': 5, 'n': 3, 'b': 3, 'q': 9, 'k': 4, 'p': 1}
return sum([points[char] for char in self.pieces if char in points.keys()])
def __lt__(self, other):
return int(self) < int(other)
def __le__(self, other):
return int(self) <= int(other)
def __gt__(self, other):
return int(self) > int(other)
def __eq__(self, other):
return int(self) == int(other)
def __ne__(self, other):
- return int(self) != int(other)
+ return int(self) != int(other)
+
+ def __add__(self, other):
+ return int(self) + int(other)
+
+ def __sub__(self, other):
+ return int(self) - int(other)
+

Мариян обнови решението на 27.11.2022 17:09 (преди над 1 година)

import re
class ChessException(Exception):
pass
class MultipleKings(ChessException):
pass
class AdjascentKings(ChessException):
pass
class FirstLinePawn(ChessException):
pass
def ChessDesk(str):
for row in str.split('/'):
for column in row:
if column.isdigit():
str = str.replace(column, 'X' * eval(column))
return str.split('/')
def isKing(piece):
return piece in ('k', 'K')
def HasAdjascentKings(chessDesk, row, column):
for i in [-1, 0, 1]:
for j in [-1, 0, 1]:
if chessDesk[row + i][column + j] == chessDesk[row][column] and not (i, j) == (0, 0):
return True
return False
def IsValidIndex(i, j):
isInRange = lambda number: 0 <= number - 1 <= number + 1 <= 7
return isInRange(i) and isInRange(j)
class ChessPosition():
def __init__(self, str):
try:
for i, row in enumerate(ChessDesk(str)):
for j, piece in enumerate(row):
if (isKing(piece) and IsValidIndex(i, j) and HasAdjascentKings(ChessDesk(str), i, j)):
raise AdjascentKings
- if (str.count('K') != 1 or str.count('k') != 1):
+ if (str.count('K'), str.count('k')) != (1, 1):
raise MultipleKings
for i, row in enumerate(str.split('/')):
if (re.search(r'[P]{1}', row, re.IGNORECASE) and i in (0, 7)):
raise FirstLinePawn
except AdjascentKings:
print("kings")
except MultipleKings:
print("kings")
except FirstLinePawn:
print("pawns")
else:
self.str = str
self.chessDesk = ChessDesk(str)
def get_white_score(self):
points = {'R': 5, 'N': 3, 'B': 3, 'Q': 9, 'K': 4, 'P': 1}
return sum([points[char] for char in self.str if char in points.keys()])
def get_black_score(self):
points = {'r': 5, 'n': 3, 'b': 3, 'q': 9, 'k': 4, 'p': 1}
return sum([points[char] for char in self.str if char in points.keys()])
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_black_score() == self.get_white_score()
def __str__(self):
return self.str
def __len__(self):
return len(re.findall(r'[RrNnBbQqKkPp]', self.str))
def __getitem__(self, key):
- return self.chessDesk[7 - (ord(key[0]) - ord('A'))][int(key[1]) - 1]
+ return self.chessDesk[7 - (ord(key[0]) - ord('A'))][int(key[1]) - 1] if self.chessDesk[7 - (ord(key[0]) - ord('A'))][int(key[1]) - 1] == 'X' else None
class ChessScore():
def __init__(self, pieces):
self.pieces = pieces
def __int__(self):
points = {'r': 5, 'n': 3, 'b': 3, 'q': 9, 'k': 4, 'p': 1}
return sum([points[char] for char in self.pieces if char in points.keys()])
def __lt__(self, other):
return int(self) < int(other)
def __le__(self, other):
return int(self) <= int(other)
def __gt__(self, other):
return int(self) > int(other)
def __eq__(self, other):
return int(self) == int(other)
def __ne__(self, other):
return int(self) != int(other)
def __add__(self, other):
return int(self) + int(other)
def __sub__(self, other):
return int(self) - int(other)
-
+
+ a = ChessPosition('rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR')

Мариян обнови решението на 27.11.2022 17:23 (преди над 1 година)

import re
class ChessException(Exception):
pass
class MultipleKings(ChessException):
pass
class AdjascentKings(ChessException):
pass
class FirstLinePawn(ChessException):
pass
def ChessDesk(str):
for row in str.split('/'):
for column in row:
if column.isdigit():
str = str.replace(column, 'X' * eval(column))
return str.split('/')
def isKing(piece):
return piece in ('k', 'K')
def HasAdjascentKings(chessDesk, row, column):
for i in [-1, 0, 1]:
for j in [-1, 0, 1]:
if chessDesk[row + i][column + j] == chessDesk[row][column] and not (i, j) == (0, 0):
return True
return False
def IsValidIndex(i, j):
isInRange = lambda number: 0 <= number - 1 <= number + 1 <= 7
return isInRange(i) and isInRange(j)
class ChessPosition():
def __init__(self, str):
try:
for i, row in enumerate(ChessDesk(str)):
for j, piece in enumerate(row):
if (isKing(piece) and IsValidIndex(i, j) and HasAdjascentKings(ChessDesk(str), i, j)):
raise AdjascentKings
if (str.count('K'), str.count('k')) != (1, 1):
raise MultipleKings
for i, row in enumerate(str.split('/')):
if (re.search(r'[P]{1}', row, re.IGNORECASE) and i in (0, 7)):
raise FirstLinePawn
except AdjascentKings:
print("kings")
except MultipleKings:
print("kings")
except FirstLinePawn:
print("pawns")
else:
self.str = str
self.chessDesk = ChessDesk(str)
def get_white_score(self):
points = {'R': 5, 'N': 3, 'B': 3, 'Q': 9, 'K': 4, 'P': 1}
return sum([points[char] for char in self.str if char in points.keys()])
def get_black_score(self):
points = {'r': 5, 'n': 3, 'b': 3, 'q': 9, 'k': 4, 'p': 1}
return sum([points[char] for char in self.str if char in points.keys()])
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_black_score() == self.get_white_score()
def __str__(self):
return self.str
def __len__(self):
return len(re.findall(r'[RrNnBbQqKkPp]', self.str))
def __getitem__(self, key):
- return self.chessDesk[7 - (ord(key[0]) - ord('A'))][int(key[1]) - 1] if self.chessDesk[7 - (ord(key[0]) - ord('A'))][int(key[1]) - 1] == 'X' else None
+ return self.chessDesk[8 -int(key[1])][(ord(key[0]) - ord('A'))] if self.chessDesk[8 - int(key[1])][(ord(key[0]) - ord('A'))] != 'X' else None
class ChessScore():
def __init__(self, pieces):
self.pieces = pieces
def __int__(self):
points = {'r': 5, 'n': 3, 'b': 3, 'q': 9, 'k': 4, 'p': 1}
return sum([points[char] for char in self.pieces if char in points.keys()])
def __lt__(self, other):
return int(self) < int(other)
def __le__(self, other):
return int(self) <= int(other)
def __gt__(self, other):
return int(self) > int(other)
def __eq__(self, other):
return int(self) == int(other)
def __ne__(self, other):
return int(self) != int(other)
def __add__(self, other):
return int(self) + int(other)
def __sub__(self, other):
return int(self) - int(other)
-
- a = ChessPosition('rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR')
+

Мариян обнови решението на 27.11.2022 17:24 (преди над 1 година)

import re
class ChessException(Exception):
pass
class MultipleKings(ChessException):
pass
class AdjascentKings(ChessException):
pass
class FirstLinePawn(ChessException):
pass
def ChessDesk(str):
for row in str.split('/'):
for column in row:
if column.isdigit():
str = str.replace(column, 'X' * eval(column))
return str.split('/')
def isKing(piece):
return piece in ('k', 'K')
def HasAdjascentKings(chessDesk, row, column):
for i in [-1, 0, 1]:
for j in [-1, 0, 1]:
if chessDesk[row + i][column + j] == chessDesk[row][column] and not (i, j) == (0, 0):
return True
return False
def IsValidIndex(i, j):
isInRange = lambda number: 0 <= number - 1 <= number + 1 <= 7
return isInRange(i) and isInRange(j)
class ChessPosition():
def __init__(self, str):
try:
for i, row in enumerate(ChessDesk(str)):
for j, piece in enumerate(row):
if (isKing(piece) and IsValidIndex(i, j) and HasAdjascentKings(ChessDesk(str), i, j)):
raise AdjascentKings
if (str.count('K'), str.count('k')) != (1, 1):
raise MultipleKings
for i, row in enumerate(str.split('/')):
if (re.search(r'[P]{1}', row, re.IGNORECASE) and i in (0, 7)):
raise FirstLinePawn
except AdjascentKings:
print("kings")
except MultipleKings:
print("kings")
except FirstLinePawn:
print("pawns")
else:
self.str = str
self.chessDesk = ChessDesk(str)
def get_white_score(self):
points = {'R': 5, 'N': 3, 'B': 3, 'Q': 9, 'K': 4, 'P': 1}
return sum([points[char] for char in self.str if char in points.keys()])
def get_black_score(self):
points = {'r': 5, 'n': 3, 'b': 3, 'q': 9, 'k': 4, 'p': 1}
return sum([points[char] for char in self.str if char in points.keys()])
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_black_score() == self.get_white_score()
def __str__(self):
return self.str
def __len__(self):
return len(re.findall(r'[RrNnBbQqKkPp]', self.str))
def __getitem__(self, key):
- return self.chessDesk[8 -int(key[1])][(ord(key[0]) - ord('A'))] if self.chessDesk[8 - int(key[1])][(ord(key[0]) - ord('A'))] != 'X' else None
+ return self.chessDesk[8 - int(key[1])][(ord(key[0]) - ord('A'))] if self.chessDesk[8 - int(key[1])][(ord(key[0]) - ord('A'))] != 'X' else None
class ChessScore():
def __init__(self, pieces):
self.pieces = pieces
def __int__(self):
points = {'r': 5, 'n': 3, 'b': 3, 'q': 9, 'k': 4, 'p': 1}
return sum([points[char] for char in self.pieces if char in points.keys()])
def __lt__(self, other):
return int(self) < int(other)
def __le__(self, other):
return int(self) <= int(other)
def __gt__(self, other):
return int(self) > int(other)
def __eq__(self, other):
return int(self) == int(other)
def __ne__(self, other):
return int(self) != int(other)
def __add__(self, other):
return int(self) + int(other)
def __sub__(self, other):
return int(self) - int(other)

Мариян обнови решението на 27.11.2022 21:17 (преди над 1 година)

import re
class ChessException(Exception):
pass
class MultipleKings(ChessException):
pass
class AdjascentKings(ChessException):
pass
class FirstLinePawn(ChessException):
pass
def ChessDesk(str):
for row in str.split('/'):
for column in row:
if column.isdigit():
str = str.replace(column, 'X' * eval(column))
return str.split('/')
def isKing(piece):
return piece in ('k', 'K')
def HasAdjascentKings(chessDesk, row, column):
for i in [-1, 0, 1]:
for j in [-1, 0, 1]:
if chessDesk[row + i][column + j] == chessDesk[row][column] and not (i, j) == (0, 0):
return True
return False
def IsValidIndex(i, j):
isInRange = lambda number: 0 <= number - 1 <= number + 1 <= 7
return isInRange(i) and isInRange(j)
class ChessPosition():
def __init__(self, str):
try:
for i, row in enumerate(ChessDesk(str)):
for j, piece in enumerate(row):
if (isKing(piece) and IsValidIndex(i, j) and HasAdjascentKings(ChessDesk(str), i, j)):
raise AdjascentKings
if (str.count('K'), str.count('k')) != (1, 1):
raise MultipleKings
for i, row in enumerate(str.split('/')):
if (re.search(r'[P]{1}', row, re.IGNORECASE) and i in (0, 7)):
raise FirstLinePawn
except AdjascentKings:
print("kings")
except MultipleKings:
print("kings")
except FirstLinePawn:
print("pawns")
else:
self.str = str
self.chessDesk = ChessDesk(str)
def get_white_score(self):
points = {'R': 5, 'N': 3, 'B': 3, 'Q': 9, 'K': 4, 'P': 1}
return sum([points[char] for char in self.str if char in points.keys()])
def get_black_score(self):
points = {'r': 5, 'n': 3, 'b': 3, 'q': 9, 'k': 4, 'p': 1}
return sum([points[char] for char in self.str if char in points.keys()])
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_black_score() == self.get_white_score()
def __str__(self):
return self.str
def __len__(self):
- return len(re.findall(r'[RrNnBbQqKkPp]', self.str))
+ return len(re.findall(r'[RNBQKP]', self.str, re.IGNORECASE))
def __getitem__(self, key):
- return self.chessDesk[8 - int(key[1])][(ord(key[0]) - ord('A'))] if self.chessDesk[8 - int(key[1])][(ord(key[0]) - ord('A'))] != 'X' else None
+ row = 8 - int(key[1])
+ column = (ord(key[0]) - ord('A'))
+ return self.chessDesk[row][column] if self.chessDesk[row][column] != 'X' else None
class ChessScore():
def __init__(self, pieces):
self.pieces = pieces
def __int__(self):
points = {'r': 5, 'n': 3, 'b': 3, 'q': 9, 'k': 4, 'p': 1}
return sum([points[char] for char in self.pieces if char in points.keys()])
def __lt__(self, other):
return int(self) < int(other)
def __le__(self, other):
return int(self) <= int(other)
def __gt__(self, other):
return int(self) > int(other)
def __eq__(self, other):
return int(self) == int(other)
def __ne__(self, other):
return int(self) != int(other)
def __add__(self, other):
return int(self) + int(other)
def __sub__(self, other):
- return int(self) - int(other)
-
+ return int(self) - int(other)

Мариян обнови решението на 27.11.2022 21:22 (преди над 1 година)

import re
class ChessException(Exception):
pass
class MultipleKings(ChessException):
pass
class AdjascentKings(ChessException):
pass
class FirstLinePawn(ChessException):
pass
def ChessDesk(str):
for row in str.split('/'):
for column in row:
if column.isdigit():
str = str.replace(column, 'X' * eval(column))
return str.split('/')
def isKing(piece):
return piece in ('k', 'K')
def HasAdjascentKings(chessDesk, row, column):
for i in [-1, 0, 1]:
for j in [-1, 0, 1]:
if chessDesk[row + i][column + j] == chessDesk[row][column] and not (i, j) == (0, 0):
return True
return False
def IsValidIndex(i, j):
isInRange = lambda number: 0 <= number - 1 <= number + 1 <= 7
return isInRange(i) and isInRange(j)
class ChessPosition():
def __init__(self, str):
try:
for i, row in enumerate(ChessDesk(str)):
for j, piece in enumerate(row):
if (isKing(piece) and IsValidIndex(i, j) and HasAdjascentKings(ChessDesk(str), i, j)):
raise AdjascentKings
if (str.count('K'), str.count('k')) != (1, 1):
raise MultipleKings
for i, row in enumerate(str.split('/')):
if (re.search(r'[P]{1}', row, re.IGNORECASE) and i in (0, 7)):
raise FirstLinePawn
except AdjascentKings:
print("kings")
except MultipleKings:
print("kings")
except FirstLinePawn:
print("pawns")
else:
self.str = str
self.chessDesk = ChessDesk(str)
def get_white_score(self):
points = {'R': 5, 'N': 3, 'B': 3, 'Q': 9, 'K': 4, 'P': 1}
return sum([points[char] for char in self.str if char in points.keys()])
def get_black_score(self):
points = {'r': 5, 'n': 3, 'b': 3, 'q': 9, 'k': 4, 'p': 1}
return sum([points[char] for char in self.str if char in points.keys()])
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_black_score() == self.get_white_score()
def __str__(self):
return self.str
def __len__(self):
return len(re.findall(r'[RNBQKP]', self.str, re.IGNORECASE))
def __getitem__(self, key):
row = 8 - int(key[1])
- column = (ord(key[0]) - ord('A'))
+ column = ord(key[0]) - ord('A')
return self.chessDesk[row][column] if self.chessDesk[row][column] != 'X' else None
class ChessScore():
def __init__(self, pieces):
self.pieces = pieces
def __int__(self):
points = {'r': 5, 'n': 3, 'b': 3, 'q': 9, 'k': 4, 'p': 1}
return sum([points[char] for char in self.pieces if char in points.keys()])
def __lt__(self, other):
return int(self) < int(other)
def __le__(self, other):
return int(self) <= int(other)
def __gt__(self, other):
return int(self) > int(other)
def __eq__(self, other):
return int(self) == int(other)
def __ne__(self, other):
return int(self) != int(other)
def __add__(self, other):
return int(self) + int(other)
def __sub__(self, other):
return int(self) - int(other)

Мариян обнови решението на 27.11.2022 22:05 (преди над 1 година)

import re
-
class ChessException(Exception):
pass
class MultipleKings(ChessException):
pass
class AdjascentKings(ChessException):
pass
class FirstLinePawn(ChessException):
pass
+class InvalidPieces(ChessException):
+ pass
def ChessDesk(str):
for row in str.split('/'):
for column in row:
if column.isdigit():
str = str.replace(column, 'X' * eval(column))
return str.split('/')
def isKing(piece):
return piece in ('k', 'K')
def HasAdjascentKings(chessDesk, row, column):
for i in [-1, 0, 1]:
for j in [-1, 0, 1]:
if chessDesk[row + i][column + j] == chessDesk[row][column] and not (i, j) == (0, 0):
return True
return False
-def IsValidIndex(i, j):
+def IsValidIndex(row, column):
isInRange = lambda number: 0 <= number - 1 <= number + 1 <= 7
- return isInRange(i) and isInRange(j)
+ return isInRange(row) and isInRange(column)
class ChessPosition():
def __init__(self, str):
try:
- for i, row in enumerate(ChessDesk(str)):
+ chessDeck = ChessDesk(str)
+ for i, row in enumerate(chessDeck):
for j, piece in enumerate(row):
- if (isKing(piece) and IsValidIndex(i, j) and HasAdjascentKings(ChessDesk(str), i, j)):
+ if (isKing(piece) and IsValidIndex(i, j) and HasAdjascentKings(chessDeck, i, j)):
raise AdjascentKings
if (str.count('K'), str.count('k')) != (1, 1):
raise MultipleKings
for i, row in enumerate(str.split('/')):
if (re.search(r'[P]{1}', row, re.IGNORECASE) and i in (0, 7)):
raise FirstLinePawn
except AdjascentKings:
print("kings")
except MultipleKings:
print("kings")
except FirstLinePawn:
print("pawns")
else:
self.str = str
self.chessDesk = ChessDesk(str)
def get_white_score(self):
points = {'R': 5, 'N': 3, 'B': 3, 'Q': 9, 'K': 4, 'P': 1}
return sum([points[char] for char in self.str if char in points.keys()])
def get_black_score(self):
points = {'r': 5, 'n': 3, 'b': 3, 'q': 9, 'k': 4, 'p': 1}
return sum([points[char] for char in self.str if char in points.keys()])
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_black_score() == self.get_white_score()
def __str__(self):
return self.str
def __len__(self):
return len(re.findall(r'[RNBQKP]', self.str, re.IGNORECASE))
def __getitem__(self, key):
row = 8 - int(key[1])
column = ord(key[0]) - ord('A')
return self.chessDesk[row][column] if self.chessDesk[row][column] != 'X' else None
class ChessScore():
def __init__(self, pieces):
- self.pieces = pieces
+ try:
+ if(type(pieces)!=list):
+ raise TypeError
+ for piece in pieces:
+ if(piece not in ('r','n','b','q','k','p')):
+ raise InvalidPieces
+ except TypeError:
+ print('Error : ChessScore must have a list as an argument.')
+ except InvalidPieces:
+ print('Error : One or more pieces are not identified as a black not-pawn piece.')
+ else:
+ self.pieces = pieces
def __int__(self):
points = {'r': 5, 'n': 3, 'b': 3, 'q': 9, 'k': 4, 'p': 1}
return sum([points[char] for char in self.pieces if char in points.keys()])
def __lt__(self, other):
return int(self) < int(other)
def __le__(self, other):
return int(self) <= int(other)
def __gt__(self, other):
return int(self) > int(other)
def __eq__(self, other):
return int(self) == int(other)
def __ne__(self, other):
return int(self) != int(other)
def __add__(self, other):
return int(self) + int(other)
def __sub__(self, other):
return int(self) - int(other)

Мариян обнови решението на 27.11.2022 22:06 (преди над 1 година)

import re
+
class ChessException(Exception):
pass
class MultipleKings(ChessException):
pass
class AdjascentKings(ChessException):
pass
class FirstLinePawn(ChessException):
pass
+
class InvalidPieces(ChessException):
pass
+
def ChessDesk(str):
for row in str.split('/'):
for column in row:
if column.isdigit():
str = str.replace(column, 'X' * eval(column))
return str.split('/')
def isKing(piece):
return piece in ('k', 'K')
def HasAdjascentKings(chessDesk, row, column):
for i in [-1, 0, 1]:
for j in [-1, 0, 1]:
if chessDesk[row + i][column + j] == chessDesk[row][column] and not (i, j) == (0, 0):
return True
return False
def IsValidIndex(row, column):
- isInRange = lambda number: 0 <= number - 1 <= number + 1 <= 7
+ def isInRange(number): return 0 <= number - 1 <= number + 1 <= 7
return isInRange(row) and isInRange(column)
class ChessPosition():
def __init__(self, str):
try:
chessDeck = ChessDesk(str)
for i, row in enumerate(chessDeck):
for j, piece in enumerate(row):
if (isKing(piece) and IsValidIndex(i, j) and HasAdjascentKings(chessDeck, i, j)):
raise AdjascentKings
if (str.count('K'), str.count('k')) != (1, 1):
raise MultipleKings
for i, row in enumerate(str.split('/')):
if (re.search(r'[P]{1}', row, re.IGNORECASE) and i in (0, 7)):
raise FirstLinePawn
except AdjascentKings:
print("kings")
except MultipleKings:
print("kings")
except FirstLinePawn:
print("pawns")
else:
self.str = str
self.chessDesk = ChessDesk(str)
def get_white_score(self):
points = {'R': 5, 'N': 3, 'B': 3, 'Q': 9, 'K': 4, 'P': 1}
return sum([points[char] for char in self.str if char in points.keys()])
def get_black_score(self):
points = {'r': 5, 'n': 3, 'b': 3, 'q': 9, 'k': 4, 'p': 1}
return sum([points[char] for char in self.str if char in points.keys()])
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_black_score() == self.get_white_score()
def __str__(self):
return self.str
def __len__(self):
return len(re.findall(r'[RNBQKP]', self.str, re.IGNORECASE))
def __getitem__(self, key):
row = 8 - int(key[1])
column = ord(key[0]) - ord('A')
return self.chessDesk[row][column] if self.chessDesk[row][column] != 'X' else None
class ChessScore():
def __init__(self, pieces):
try:
- if(type(pieces)!=list):
+ if (type(pieces) != list):
raise TypeError
for piece in pieces:
- if(piece not in ('r','n','b','q','k','p')):
+ if (piece not in ('r', 'n', 'b', 'q', 'k', 'p')):
raise InvalidPieces
except TypeError:
print('Error : ChessScore must have a list as an argument.')
except InvalidPieces:
- print('Error : One or more pieces are not identified as a black not-pawn piece.')
+ print(
+ 'Error : One or more pieces are not identified as a black not-pawn piece.')
else:
self.pieces = pieces
def __int__(self):
points = {'r': 5, 'n': 3, 'b': 3, 'q': 9, 'k': 4, 'p': 1}
return sum([points[char] for char in self.pieces if char in points.keys()])
def __lt__(self, other):
return int(self) < int(other)
def __le__(self, other):
return int(self) <= int(other)
def __gt__(self, other):
return int(self) > int(other)
def __eq__(self, other):
return int(self) == int(other)
def __ne__(self, other):
return int(self) != int(other)
-
+
def __add__(self, other):
return int(self) + int(other)
-
+
def __sub__(self, other):
- return int(self) - int(other)
+ return int(self) - int(other)

Мариян обнови решението на 27.11.2022 22:06 (преди над 1 година)

import re
class ChessException(Exception):
pass
class MultipleKings(ChessException):

Виждам няколко специфични дефиниции на изключения, но никъде не виждам дефиниран текста, който очакваме по условие. Например, казано е, че при проблем с царете трбява да има ChessException с текст "kings". В момента виждам само принтове, но това не е действието, което тестовете очакват.

pass
class AdjascentKings(ChessException):
pass
class FirstLinePawn(ChessException):
pass
class InvalidPieces(ChessException):
pass
def ChessDesk(str):
for row in str.split('/'):
for column in row:
if column.isdigit():
str = str.replace(column, 'X' * eval(column))
return str.split('/')
def isKing(piece):
return piece in ('k', 'K')
def HasAdjascentKings(chessDesk, row, column):
for i in [-1, 0, 1]:
for j in [-1, 0, 1]:
if chessDesk[row + i][column + j] == chessDesk[row][column] and not (i, j) == (0, 0):
return True
return False
def IsValidIndex(row, column):
def isInRange(number): return 0 <= number - 1 <= number + 1 <= 7
return isInRange(row) and isInRange(column)
class ChessPosition():
def __init__(self, str):
try:
chessDeck = ChessDesk(str)
for i, row in enumerate(chessDeck):
for j, piece in enumerate(row):
if (isKing(piece) and IsValidIndex(i, j) and HasAdjascentKings(chessDeck, i, j)):
raise AdjascentKings
if (str.count('K'), str.count('k')) != (1, 1):
raise MultipleKings
for i, row in enumerate(str.split('/')):
if (re.search(r'[P]{1}', row, re.IGNORECASE) and i in (0, 7)):
raise FirstLinePawn
except AdjascentKings:
print("kings")
except MultipleKings:
print("kings")
except FirstLinePawn:
print("pawns")
else:
self.str = str
self.chessDesk = ChessDesk(str)
def get_white_score(self):
points = {'R': 5, 'N': 3, 'B': 3, 'Q': 9, 'K': 4, 'P': 1}
return sum([points[char] for char in self.str if char in points.keys()])
def get_black_score(self):
points = {'r': 5, 'n': 3, 'b': 3, 'q': 9, 'k': 4, 'p': 1}
return sum([points[char] for char in self.str if char in points.keys()])
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_black_score() == self.get_white_score()
def __str__(self):
return self.str
def __len__(self):
return len(re.findall(r'[RNBQKP]', self.str, re.IGNORECASE))
def __getitem__(self, key):
row = 8 - int(key[1])
column = ord(key[0]) - ord('A')
return self.chessDesk[row][column] if self.chessDesk[row][column] != 'X' else None
class ChessScore():
def __init__(self, pieces):
try:
if (type(pieces) != list):
raise TypeError
for piece in pieces:
if (piece not in ('r', 'n', 'b', 'q', 'k', 'p')):
raise InvalidPieces
except TypeError:
print('Error : ChessScore must have a list as an argument.')
except InvalidPieces:
- print(
- 'Error : One or more pieces are not identified as a black not-pawn piece.')
+ print('Error : One or more pieces are not identified as a black not-pawn piece.')
else:
self.pieces = pieces
def __int__(self):
points = {'r': 5, 'n': 3, 'b': 3, 'q': 9, 'k': 4, 'p': 1}
return sum([points[char] for char in self.pieces if char in points.keys()])
def __lt__(self, other):
return int(self) < int(other)
def __le__(self, other):
return int(self) <= int(other)
def __gt__(self, other):
return int(self) > int(other)
def __eq__(self, other):
return int(self) == int(other)
def __ne__(self, other):
return int(self) != int(other)
def __add__(self, other):
return int(self) + int(other)
def __sub__(self, other):
return int(self) - int(other)

Мариян обнови решението на 28.11.2022 22:23 (преди над 1 година)

import re
class ChessException(Exception):
pass
-class MultipleKings(ChessException):
- pass
+class ChessPosition:
+
+ @staticmethod
+ def chess_desk(string):
+ for row in string.split('/'):
+ for column in row:
+ if column.isdigit():
+ string = string.replace(column, 'X' * eval(column))
+ return string.split('/')
+
+ @staticmethod
+ def is_king(piece):
+ return piece in ('k', 'K')
+
+ @staticmethod
+ def has_adjascent_kings(chessDesk, row, column):
+ for i in [-1, 0, 1]:
+ for j in [-1, 0, 1]:
+ if chessDesk[row + i][column + j] == chessDesk[row][column] and not (i, j) == (0, 0):
+ return True
+ return False
+
+ @staticmethod
+ def is_valid_index(row, column):
+ isInRange = lambda number : 0 <= number - 1 <= number + 1 <= 7
+ return isInRange(row) and isInRange(column)
+
+ def __init__(self, string):
+ chessDeck = self.chess_desk(string)
+ for i, row in enumerate(chessDeck):
+ for j, piece in enumerate(row):
+ if (self.is_king(piece) and self.is_valid_index(i, j) and self.has_adjascent_kings(chessDeck, i, j)):
+ raise ChessException("kings")
+ if (string.count('K'), string.count('k')) != (1, 1):
+ raise ChessException("kings")
+ for i, row in enumerate(string.split('/')):
+ if (re.search(r'[P]{1}', row, re.IGNORECASE) and i in (0, 7)):
+ raise ChessException("pawns")
+ self.string = string
+ self.chessDesk = self.chess_desk(string)
-
-class AdjascentKings(ChessException):
- pass
-
-
-class FirstLinePawn(ChessException):
- pass
-
-
-class InvalidPieces(ChessException):
- pass
-
-
-def ChessDesk(str):
- for row in str.split('/'):
- for column in row:
- if column.isdigit():
- str = str.replace(column, 'X' * eval(column))
- return str.split('/')
-
-
-def isKing(piece):
- return piece in ('k', 'K')
-
-
-def HasAdjascentKings(chessDesk, row, column):
- for i in [-1, 0, 1]:
- for j in [-1, 0, 1]:
- if chessDesk[row + i][column + j] == chessDesk[row][column] and not (i, j) == (0, 0):
- return True
- return False
-
-
-def IsValidIndex(row, column):
- def isInRange(number): return 0 <= number - 1 <= number + 1 <= 7
- return isInRange(row) and isInRange(column)
-
-
-class ChessPosition():
- def __init__(self, str):
- try:
- chessDeck = ChessDesk(str)
- for i, row in enumerate(chessDeck):
- for j, piece in enumerate(row):
- if (isKing(piece) and IsValidIndex(i, j) and HasAdjascentKings(chessDeck, i, j)):
- raise AdjascentKings
- if (str.count('K'), str.count('k')) != (1, 1):
- raise MultipleKings
- for i, row in enumerate(str.split('/')):
- if (re.search(r'[P]{1}', row, re.IGNORECASE) and i in (0, 7)):
- raise FirstLinePawn
- except AdjascentKings:
- print("kings")
- except MultipleKings:
- print("kings")
- except FirstLinePawn:
- print("pawns")
- else:
- self.str = str
- self.chessDesk = ChessDesk(str)
-
def get_white_score(self):
points = {'R': 5, 'N': 3, 'B': 3, 'Q': 9, 'K': 4, 'P': 1}
- return sum([points[char] for char in self.str if char in points.keys()])
+ return sum([points[char] for char in self.string if char in points.keys()])
def get_black_score(self):
points = {'r': 5, 'n': 3, 'b': 3, 'q': 9, 'k': 4, 'p': 1}
- return sum([points[char] for char in self.str if char in points.keys()])
+ return sum([points[char] for char in self.string if char in points.keys()])
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_black_score() == self.get_white_score()
def __str__(self):
- return self.str
+ return self.string
def __len__(self):
- return len(re.findall(r'[RNBQKP]', self.str, re.IGNORECASE))
+ return len(re.findall(r'[RNBQKP]', self.string, re.IGNORECASE))
def __getitem__(self, key):
row = 8 - int(key[1])
column = ord(key[0]) - ord('A')
return self.chessDesk[row][column] if self.chessDesk[row][column] != 'X' else None
class ChessScore():
def __init__(self, pieces):
- try:
- if (type(pieces) != list):
- raise TypeError
+ assert type(pieces) == list, 'ChessScore must have a list as an argument.'
for piece in pieces:
- if (piece not in ('r', 'n', 'b', 'q', 'k', 'p')):
- raise InvalidPieces
- except TypeError:
- print('Error : ChessScore must have a list as an argument.')
- except InvalidPieces:
- print('Error : One or more pieces are not identified as a black not-pawn piece.')
- else:
+ assert piece not in ('r', 'n', 'b', 'q', 'k', 'p'), 'One or more pieces are not identified as a black not-pawn piece.'
self.pieces = pieces
def __int__(self):
points = {'r': 5, 'n': 3, 'b': 3, 'q': 9, 'k': 4, 'p': 1}
return sum([points[char] for char in self.pieces if char in points.keys()])
def __lt__(self, other):
return int(self) < int(other)
def __le__(self, other):
return int(self) <= int(other)
def __gt__(self, other):
return int(self) > int(other)
def __eq__(self, other):
return int(self) == int(other)
def __ne__(self, other):
return int(self) != int(other)
def __add__(self, other):
return int(self) + int(other)
def __sub__(self, other):
- return int(self) - int(other)
+ return int(self) - int(other)

Мариян обнови решението на 28.11.2022 22:28 (преди над 1 година)

import re
class ChessException(Exception):
pass
class ChessPosition:
@staticmethod
def chess_desk(string):
for row in string.split('/'):
for column in row:
if column.isdigit():
string = string.replace(column, 'X' * eval(column))
return string.split('/')
@staticmethod
def is_king(piece):
return piece in ('k', 'K')
@staticmethod
def has_adjascent_kings(chessDesk, row, column):
for i in [-1, 0, 1]:
for j in [-1, 0, 1]:
if chessDesk[row + i][column + j] == chessDesk[row][column] and not (i, j) == (0, 0):
return True
return False
@staticmethod
def is_valid_index(row, column):
isInRange = lambda number : 0 <= number - 1 <= number + 1 <= 7
return isInRange(row) and isInRange(column)
def __init__(self, string):
chessDeck = self.chess_desk(string)
for i, row in enumerate(chessDeck):
for j, piece in enumerate(row):
if (self.is_king(piece) and self.is_valid_index(i, j) and self.has_adjascent_kings(chessDeck, i, j)):
raise ChessException("kings")
if (string.count('K'), string.count('k')) != (1, 1):
raise ChessException("kings")
for i, row in enumerate(string.split('/')):
if (re.search(r'[P]{1}', row, re.IGNORECASE) and i in (0, 7)):
raise ChessException("pawns")
self.string = string
self.chessDesk = self.chess_desk(string)
def get_white_score(self):
points = {'R': 5, 'N': 3, 'B': 3, 'Q': 9, 'K': 4, 'P': 1}
return sum([points[char] for char in self.string if char in points.keys()])
def get_black_score(self):
points = {'r': 5, 'n': 3, 'b': 3, 'q': 9, 'k': 4, 'p': 1}
return sum([points[char] for char in self.string if char in points.keys()])
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_black_score() == self.get_white_score()
def __str__(self):
return self.string
def __len__(self):
return len(re.findall(r'[RNBQKP]', self.string, re.IGNORECASE))
def __getitem__(self, key):
row = 8 - int(key[1])
column = ord(key[0]) - ord('A')
return self.chessDesk[row][column] if self.chessDesk[row][column] != 'X' else None
class ChessScore():
def __init__(self, pieces):
assert type(pieces) == list, 'ChessScore must have a list as an argument.'
for piece in pieces:
- assert piece not in ('r', 'n', 'b', 'q', 'k', 'p'), 'One or more pieces are not identified as a black not-pawn piece.'
+ assert piece in ('r', 'n', 'b', 'q', 'k', 'p'), 'One or more pieces are not identified as a black not-pawn piece.'
self.pieces = pieces
def __int__(self):
points = {'r': 5, 'n': 3, 'b': 3, 'q': 9, 'k': 4, 'p': 1}
return sum([points[char] for char in self.pieces if char in points.keys()])
def __lt__(self, other):
return int(self) < int(other)
def __le__(self, other):
return int(self) <= int(other)
def __gt__(self, other):
return int(self) > int(other)
def __eq__(self, other):
return int(self) == int(other)
def __ne__(self, other):
return int(self) != int(other)
def __add__(self, other):
return int(self) + int(other)
def __sub__(self, other):
return int(self) - int(other)