Решение на Шахматни фенове от Иван Досев

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

Към профила на Иван Досев

Резултати

  • 6 точки от тестове
  • 0 бонус точки
  • 6 точки общо
  • 11 успешни тест(а)
  • 6 неуспешни тест(а)

Код

import unittest
class ChessException(Exception):
def __init__(self, message):
super().__init__(message)
self.message = message
class ChessPosition:
def __init__(self, fen: str):
self.values = {"p": 1, "k": 4, "q": 9, "b": 3, "n": 3, "r": 5}
self.white_figs = []
self.black_figs = []
black_result = 0
white_result = 0
black_kings = 0
white_kings = 0
black_king_x = 0
black_king_y = 0
white_king_x = 0
white_king_y = 0
self.fen = fen.split(" ")[0]
self.arr = fen.split("/")
self.throwPawn = False
for i in range(8):
for index, letter in enumerate(self.arr[i]):
if letter.islower():
black_result = black_result + self.values[letter]
self.black_figs.append(letter)
if letter.isupper():
white_result = white_result + self.values[letter.lower()]
self.white_figs.append(letter)
if letter == 'k':
black_kings = black_kings + 1
black_king_x = i
black_king_y = index
if letter == 'K':
white_kings = white_kings + 1
white_king_x = i
white_king_y = index
if letter.lower() == 'p' and (i == 0 or i == 7):
self.throwPawn = True
if black_kings > 1 or white_kings > 1 or black_kings == 0 or white_kings == 0:
raise ChessException("kings")
if (black_king_x == white_king_x and abs(black_king_y - white_king_y) < 2) or \
(black_king_y == white_king_y and abs(black_king_x - white_king_x) < 2) or \
(black_king_x == white_king_x + 1 and black_king_y == white_king_y + 1) or \
(black_king_x == white_king_x - 1 and black_king_y == white_king_y - 1) or \
(black_king_x == white_king_x + 1 and black_king_y == white_king_y - 1) or \
(black_king_x == white_king_x - 1 and black_king_y == white_king_y + 1):
raise ChessException("kings")
if self.throwPawn:
raise ChessException("pawns")
def get_white_score(self):
return ChessScore(self.white_figs)
def get_black_score(self):
return ChessScore(self.black_figs)
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.fen
def __len__(self):
return len(self.black_figs) + len(self.white_figs)
def __getitem__(self, item):
width = item[0]
height = item[1]
for i in range(8):
for index, letter in enumerate(self.arr[i]):
if ((8 - i) == int(height)) and (index == (ord(width) - ord('A'))):
if '0' <= letter <= '9':
return None
else:
return letter
class ChessScore:
def __init__(self, pieces: list):
self.values = {"p": 1, "k": 4, "q": 9, "b": 3, "n": 3, "r": 5}
self.result = 0
for piece in pieces:
self.result = self.result + self.values[piece.lower()]
def __int__(self):
return self.result
def __gt__(self, other):
return self.result > other.result
def __lt__(self, other):
return self.result < other.result
def __ge__(self, other):
return self.result >= other.result
def __le__(self, other):
return self.result <= other.result
def __eq__(self, other):
return self.result == other.result
def __ne__(self, other):
return self.result != other.result
def __add__(self, other):
return self.result + other.result
def __sub__(self, other):
return self.result - other.result

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

..EEEFE.....E....
======================================================================
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_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_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

======================================================================
FAIL: 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
AssertionError: None != 'K'

----------------------------------------------------------------------
Ran 17 tests in 0.165s

FAILED (failures=1, errors=5)

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

Иван обнови решението на 29.11.2022 00:12 (преди почти 2 години)

+class ChessException(Exception):
+ pass
+
+
+class ChessPosition:
+ def __init__(self, fen: str):
+ self.values = {"p": 1, "k": 4, "q": 9, "b": 3, "n": 3, "r": 5}
+ self.white_figs = []
+ self.black_figs = []
+ black_result = 0
+ white_result = 0
+ black_kings = 0
+ white_kings = 0
+ black_king_x = 0
+ black_king_y = 0
+ white_king_x = 0
+ white_king_y = 0
+ self.fen = fen.split(" ")[0]
+ self.arr = fen.split("/")
+ self.throwPawn = False
+ for i in range(8):
+ for index, letter in enumerate(self.arr[i]):
+ if letter.isupper():
+ black_result = black_result + self.values[letter.lower()]
+ self.black_figs.append(letter)
+ if letter.islower():
+ white_result = white_result + self.values[letter]
+ self.white_figs.append(letter)
+ if letter == 'k':
+ black_kings = black_kings + 1
+ black_king_x = i
+ black_king_y = index
+ if letter == 'K':
+ white_kings = white_kings + 1
+ black_king_x = i
+ black_king_y = index
+ if letter.lower() == 'p' and (i == 0 or i == 7):
+ self.throwPawn = True
+ if black_kings > 1 or white_kings > 1 or black_kings == 0 or white_kings == 0:
+ raise ChessException("kings")
+ if (black_king_x == white_king_x and abs(black_king_y - white_king_y) < 2) or (
+ black_king_y == white_king_y and abs(black_king_x - white_king_x) < 2) or (
+ black_king_x == white_king_x + 1 and black_king_y == white_king_y + 1) or (
+ black_king_x == white_king_x - 1 and black_king_y == white_king_y - 1) or (
+ black_king_x == white_king_x + 1 and black_king_y == white_king_y - 1) or (
+ black_king_x == white_king_x - 1 and black_king_y == white_king_y + 1):
+ raise ChessException("kings")
+ if self.throwPawn:
+ raise ChessException("pawns")
+
+ def get_white_score(self):
+ return ChessScore(self.white_figs)
+
+ def get_black_score(self):
+ return ChessScore(self.black_figs)
+
+ 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_equals(self):
+ return self.get_black_score() == self.get_white_score()
+
+ def __str__(self):
+ return self.fen
+
+ def __len__(self):
+ return len(self.black_figs) + len(self.white_figs)
+
+ def __getitem__(self, item):
+ width = item[0]
+ height = item[1]
+ for i in range(8):
+ for index, letter in enumerate(self.arr[i]):
+ if ((8 - i) == int(height)) and (index == (ord(width) - ord('A'))):
+ if '0' <= letter <= '9':
+ return None
+ else:
+ return letter
+
+
+class ChessScore:
+ def __init__(self, pieces: list):
+ self.values = {"p": 1, "k": 4, "q": 9, "b": 3, "n": 3, "r": 5}
+ self.result = 0
+ for piece in pieces:
+ self.result = self.result + self.values[piece.lower()]
+
+ def __int__(self):
+ return self.result
+
+ def __gt__(self, other):
+ return self.result > other.result
+
+ def __lt__(self, other):
+ return self.result < other.result
+
+ def __ge__(self, other):
+ return self.result >= other.result
+
+ def __le__(self, other):
+ return self.result <= other.result
+
+ def __eq__(self, other):
+ return self.result == other.result
+
+ def __ne__(self, other):
+ return self.result != other.result
+
+ def __add__(self, other):
+ return self.result + other.result
+
+ def __sub__(self, other):
+ return self.result - other.result

Иван обнови решението на 29.11.2022 16:08 (преди почти 2 години)

+import unittest
+
+
class ChessException(Exception):
- pass
+ def __init__(self, message):
+ super().__init__(message)
+ self.message = message
class ChessPosition:
def __init__(self, fen: str):
self.values = {"p": 1, "k": 4, "q": 9, "b": 3, "n": 3, "r": 5}
self.white_figs = []
self.black_figs = []
black_result = 0
white_result = 0
black_kings = 0
white_kings = 0
black_king_x = 0
black_king_y = 0
white_king_x = 0
white_king_y = 0
self.fen = fen.split(" ")[0]
self.arr = fen.split("/")
self.throwPawn = False
for i in range(8):
for index, letter in enumerate(self.arr[i]):
- if letter.isupper():
- black_result = black_result + self.values[letter.lower()]
- self.black_figs.append(letter)
if letter.islower():
- white_result = white_result + self.values[letter]
+ black_result = black_result + self.values[letter]
+ self.black_figs.append(letter)
+ if letter.isupper():
+ white_result = white_result + self.values[letter.lower()]
self.white_figs.append(letter)
if letter == 'k':
black_kings = black_kings + 1
black_king_x = i
black_king_y = index
if letter == 'K':
white_kings = white_kings + 1
- black_king_x = i
- black_king_y = index
+ white_king_x = i
+ white_king_y = index
if letter.lower() == 'p' and (i == 0 or i == 7):
self.throwPawn = True
if black_kings > 1 or white_kings > 1 or black_kings == 0 or white_kings == 0:
raise ChessException("kings")
- if (black_king_x == white_king_x and abs(black_king_y - white_king_y) < 2) or (
- black_king_y == white_king_y and abs(black_king_x - white_king_x) < 2) or (
- black_king_x == white_king_x + 1 and black_king_y == white_king_y + 1) or (
- black_king_x == white_king_x - 1 and black_king_y == white_king_y - 1) or (
- black_king_x == white_king_x + 1 and black_king_y == white_king_y - 1) or (
- black_king_x == white_king_x - 1 and black_king_y == white_king_y + 1):
+ if (black_king_x == white_king_x and abs(black_king_y - white_king_y) < 2) or \
+ (black_king_y == white_king_y and abs(black_king_x - white_king_x) < 2) or \
+ (black_king_x == white_king_x + 1 and black_king_y == white_king_y + 1) or \
+ (black_king_x == white_king_x - 1 and black_king_y == white_king_y - 1) or \
+ (black_king_x == white_king_x + 1 and black_king_y == white_king_y - 1) or \
+ (black_king_x == white_king_x - 1 and black_king_y == white_king_y + 1):
raise ChessException("kings")
if self.throwPawn:
raise ChessException("pawns")
def get_white_score(self):
return ChessScore(self.white_figs)
def get_black_score(self):
return ChessScore(self.black_figs)
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_equals(self):
+ def is_equal(self):
return self.get_black_score() == self.get_white_score()
def __str__(self):
return self.fen
def __len__(self):
return len(self.black_figs) + len(self.white_figs)
def __getitem__(self, item):
width = item[0]
height = item[1]
for i in range(8):
for index, letter in enumerate(self.arr[i]):
if ((8 - i) == int(height)) and (index == (ord(width) - ord('A'))):
if '0' <= letter <= '9':
return None
else:
return letter
class ChessScore:
def __init__(self, pieces: list):
self.values = {"p": 1, "k": 4, "q": 9, "b": 3, "n": 3, "r": 5}
self.result = 0
for piece in pieces:
self.result = self.result + self.values[piece.lower()]
def __int__(self):
return self.result
def __gt__(self, other):
return self.result > other.result
def __lt__(self, other):
return self.result < other.result
def __ge__(self, other):
return self.result >= other.result
def __le__(self, other):
return self.result <= other.result
def __eq__(self, other):
return self.result == other.result
def __ne__(self, other):
return self.result != other.result
def __add__(self, other):
return self.result + other.result
def __sub__(self, other):
return self.result - other.result