Решение на Морски шах от Кристиян Якимов

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

Към профила на Кристиян Якимов

Резултати

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

Код

import re
class InvalidMove(Exception):
pass
class InvalidValue(Exception):
pass
class InvalidKey(Exception):
pass
class NotYourTurn(Exception):
pass
class TicTacToeBoard:
def __init__(self):
self.board = {}
for x in range(9):
self.board[x + 1] = " "
self.winner = " "
self.previous_player = " "
def __setitem__(self, str, val):
try:
if not (re.search(r'^[ABC][123]$', str)):
raise InvalidKey
if re.search(r'^O|X$', self.board[int(ord(str[0]) - ord('A') + 1) +
(3 - (int(str[1]))) * 3]):
raise InvalidMove
if not (re.search(r'^O|X$', val)):
raise InvalidValue
if ord(self.previous_player) - ord(val) == 0:
raise NotYourTurn
else:
self.previous_player = val
self.winner = self.game_status()
except InvalidKey as e:
raise InvalidKey
except InvalidMove as e:
raise InvalidMove
except InvalidValue as e:
raise InvalidValue
except NotYourTurn as e:
raise NotYourTurn
else:
self.board[int(ord(str[0]) - ord(
'A') + 1) + (3 - (int(str[1]))) * 3] = val
def __str__(self):
board_visualisation = '\n -------------\n' + '3 |'
for x in (sorted(self.board.items(), key=lambda t: t[0])):
board_visualisation += ' ' + x[1] + ' |'
if x[0] % 3 == 0 and x[0] != 9:
board_visualisation += '\n -------------\n' + \
str(3 - (x[0] // 3)) + ' |'
board_visualisation += '\n -------------\n' + ' A B C \n'
return board_visualisation
def are_these_equal(self, a, b, c):
return self.board[a] == self.board[b] == self.board[c] != " "
def game_status(self):
if self.winner != " " and self.winner != "Game in progress.":
return self.winner
else:
if self.are_these_equal(1, 5, 9):
self.winner = self.board[1] + ' wins!'
return self.winner
if self.are_these_equal(3, 5, 7):
self.winner = self.board[3] + ' wins!'
return self.winner
for i in range(3):
if self.are_these_equal(3 * i + 1, 3 * i + 2, 3 * i + 3):
self.winner = self.board[3 * i + 1] + ' wins!'
return self.winner
if self.are_these_equal(i + 1, i + 4, i + 7):
self.winner = self.board[3 * i + 1] + ' wins!'
return self.winner
if " " in self.board.values():
return "Game in progress."
return 'Draw!'

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

..E.....
======================================================================
ERROR: test_input_format (test.TicTacHomeworkTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "lib/language/python/runner.py", line 60, in thread
    raise it.exc_info[1]
  File "lib/language/python/runner.py", line 48, in run
    self.result = func(*args, **kwargs)
  File "/tmp/d20130415-29081-k9yukv/test.py", line 57, in test_input_format
    o['A1'] = None
  File "/tmp/d20130415-29081-k9yukv/solution.py", line 39, in __setitem__
    if not (re.search(r'^O|X$', val)):
  File "/opt/python3.3/lib/python3.3/re.py", line 161, in search
    return _compile(pattern, flags).search(string)
TypeError: expected string or buffer

----------------------------------------------------------------------
Ran 8 tests in 0.016s

FAILED (errors=1)

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

Кристиян обнови решението на 14.04.2013 20:44 (преди над 11 години)

+import re
+
+
+class TicTacToeExceptions(Exception):
+
+ def __init__(self):
+ pass
+
+
+class InvalidMove(TicTacToeExceptions):
+
+ def __init__(self):
+ super().__init__()
+
+
+class InvalidValue(TicTacToeExceptions):
+
+ def __init__(self):
+ super().__init__()
+
+
+class InvalidKey(TicTacToeExceptions):
+
+ def __init__(self):
+ super().__init__()
+
+
+class NotYourTurn(TicTacToeExceptions):
+
+ def __init__(self):
+ super().__init__()
+
+
+class TicTacToeBoard:
+
+ def __init__(self):
+ self.board = {}
+ for x in range(9):
+ self.board[x + 1] = " "
+ self.winner = " "
+ self.previous_player = " "
+
+ def __setitem__(self, str, val):
+ try:
+
+ if not (re.search(r'^[ABC][123]$', str)):
+ raise InvalidKey
+
+ if re.search(r'^O|X$', self.board[int(ord(str[0]) - ord('A') + 1) +
+ (3 - (int(str[1]))) * 3]):
+ raise InvalidMove
+
+ if not (re.search(r'^O|X$', val)):
+ raise InvalidValue
+
+ if ord(self.previous_player) - ord(val) == 0:
+ raise NotYourTurn
+ else:
+ self.previous_player = val
+ self.winner = self.game_status()
+
+ except InvalidKey as e:
+ raise InvalidKey
+ except InvalidMove as e:
+ raise InvalidMove
+ except InvalidValue as e:
+ raise InvalidValue
+ except NotYourTurn as e:
+ raise NotYourTurn
+
+ else:
+ self.board[int(ord(str[0]) - ord(
+ 'A') + 1) + (3 - (int(str[1]))) * 3] = val
+
+ def __str__(self):
+ board_visualisation = '\n -------------\n' + '3 |'
+ for x in (sorted(self.board.items(), key=lambda t: t[0])):
+ board_visualisation += ' ' + x[1] + ' |'
+ if x[0] % 3 == 0 and x[0] != 9:
+ board_visualisation += '\n -------------\n' + \
+ str(3 - (x[0] // 3)) + ' |'
+ board_visualisation += '\n -------------\n' + ' A B C \n'
+ return board_visualisation
+
+ def are_these_equal(self, a, b, c):
+ return self.board[a] == self.board[b] == self.board[c] != " "
+
+ def game_status(self):
+ if self.winner != " " and self.winner != "Game in progress.":
+ return self.winner
+ else:
+ if self.are_these_equal(1, 5, 9):
+ self.winner = self.board[1] + ' wins!'
+ return self.winner
+ if self.are_these_equal(3, 5, 7):
+ self.winner = self.board[3] + ' wins!'
+ return self.winner
+ for i in range(3):
+ if self.are_these_equal(3 * i + 1, 3 * i + 2, 3 * i + 3):
+ self.winner = self.board[3 * i + 1] + ' wins!'
+ return self.winner
+ if self.are_these_equal(i + 1, i + 4, i + 7):
+ self.winner = self.board[3 * i + 1] + ' wins!'
+ return self.winner
+ if " " in self.board.values():
+ return "Game in progress."
+ return 'Draw!'

По добре си дефинирай изключенията така:

class InvalidKey(Еxception):
    pass

При положение че нямаш специална функционалност в изключенията няма смисъл да си правиш някаква йерархия от тях. Просто си наследявай Exception.

Кристиян обнови решението на 14.04.2013 23:40 (преди над 11 години)

import re
-class TicTacToeExceptions(Exception):
+class InvalidMove(Exception):
def __init__(self):
pass
-class InvalidMove(TicTacToeExceptions):
+class InvalidValue(Exception):
def __init__(self):
- super().__init__()
+ pass
-class InvalidValue(TicTacToeExceptions):
+class InvalidKey(Exception):
def __init__(self):
- super().__init__()
+ pass
-class InvalidKey(TicTacToeExceptions):
+class NotYourTurn(Exception):
def __init__(self):
- super().__init__()
-
-
-class NotYourTurn(TicTacToeExceptions):
-
- def __init__(self):
- super().__init__()
+ pass
class TicTacToeBoard:
def __init__(self):
self.board = {}
for x in range(9):
self.board[x + 1] = " "
self.winner = " "
self.previous_player = " "
def __setitem__(self, str, val):
try:
if not (re.search(r'^[ABC][123]$', str)):
raise InvalidKey
if re.search(r'^O|X$', self.board[int(ord(str[0]) - ord('A') + 1) +
(3 - (int(str[1]))) * 3]):
raise InvalidMove
if not (re.search(r'^O|X$', val)):
raise InvalidValue
if ord(self.previous_player) - ord(val) == 0:
raise NotYourTurn
else:
self.previous_player = val
self.winner = self.game_status()
except InvalidKey as e:
raise InvalidKey
except InvalidMove as e:
raise InvalidMove
except InvalidValue as e:
raise InvalidValue
except NotYourTurn as e:
raise NotYourTurn
else:
self.board[int(ord(str[0]) - ord(
'A') + 1) + (3 - (int(str[1]))) * 3] = val
def __str__(self):
board_visualisation = '\n -------------\n' + '3 |'
for x in (sorted(self.board.items(), key=lambda t: t[0])):
board_visualisation += ' ' + x[1] + ' |'
if x[0] % 3 == 0 and x[0] != 9:
board_visualisation += '\n -------------\n' + \
str(3 - (x[0] // 3)) + ' |'
board_visualisation += '\n -------------\n' + ' A B C \n'
return board_visualisation
def are_these_equal(self, a, b, c):
return self.board[a] == self.board[b] == self.board[c] != " "
def game_status(self):
if self.winner != " " and self.winner != "Game in progress.":
return self.winner
else:
if self.are_these_equal(1, 5, 9):
self.winner = self.board[1] + ' wins!'
return self.winner
if self.are_these_equal(3, 5, 7):
self.winner = self.board[3] + ' wins!'
return self.winner
for i in range(3):
if self.are_these_equal(3 * i + 1, 3 * i + 2, 3 * i + 3):
self.winner = self.board[3 * i + 1] + ' wins!'
return self.winner
if self.are_these_equal(i + 1, i + 4, i + 7):
self.winner = self.board[3 * i + 1] + ' wins!'
return self.winner
if " " in self.board.values():
return "Game in progress."
return 'Draw!'

Кристиян обнови решението на 14.04.2013 23:42 (преди над 11 години)

import re
class InvalidMove(Exception):
+ pass
- def __init__(self):
- pass
-
class InvalidValue(Exception):
+ pass
- def __init__(self):
- pass
-
class InvalidKey(Exception):
+ pass
- def __init__(self):
- pass
-
class NotYourTurn(Exception):
-
- def __init__(self):
- pass
+ pass
class TicTacToeBoard:
def __init__(self):
self.board = {}
for x in range(9):
self.board[x + 1] = " "
self.winner = " "
self.previous_player = " "
def __setitem__(self, str, val):
try:
if not (re.search(r'^[ABC][123]$', str)):
raise InvalidKey
if re.search(r'^O|X$', self.board[int(ord(str[0]) - ord('A') + 1) +
(3 - (int(str[1]))) * 3]):
raise InvalidMove
if not (re.search(r'^O|X$', val)):
raise InvalidValue
if ord(self.previous_player) - ord(val) == 0:
raise NotYourTurn
else:
self.previous_player = val
self.winner = self.game_status()
except InvalidKey as e:
raise InvalidKey
except InvalidMove as e:
raise InvalidMove
except InvalidValue as e:
raise InvalidValue
except NotYourTurn as e:
raise NotYourTurn
else:
self.board[int(ord(str[0]) - ord(
'A') + 1) + (3 - (int(str[1]))) * 3] = val
def __str__(self):
board_visualisation = '\n -------------\n' + '3 |'
for x in (sorted(self.board.items(), key=lambda t: t[0])):
board_visualisation += ' ' + x[1] + ' |'
if x[0] % 3 == 0 and x[0] != 9:
board_visualisation += '\n -------------\n' + \
str(3 - (x[0] // 3)) + ' |'
board_visualisation += '\n -------------\n' + ' A B C \n'
return board_visualisation
def are_these_equal(self, a, b, c):
return self.board[a] == self.board[b] == self.board[c] != " "
def game_status(self):
if self.winner != " " and self.winner != "Game in progress.":
return self.winner
else:
if self.are_these_equal(1, 5, 9):
self.winner = self.board[1] + ' wins!'
return self.winner
if self.are_these_equal(3, 5, 7):
self.winner = self.board[3] + ' wins!'
return self.winner
for i in range(3):
if self.are_these_equal(3 * i + 1, 3 * i + 2, 3 * i + 3):
self.winner = self.board[3 * i + 1] + ' wins!'
return self.winner
if self.are_these_equal(i + 1, i + 4, i + 7):
self.winner = self.board[3 * i + 1] + ' wins!'
return self.winner
if " " in self.board.values():
return "Game in progress."
return 'Draw!'