Решение на Морски шах от Николай Манолов

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

Към профила на Николай Манолов

Резултати

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

Код

class InvalidKey(Exception):
def __init__(self, message):
self.message = message
class InvalidValue(Exception):
def __init__(self, message):
self.message = message
class InvalidMove(Exception):
def __init__(self, message):
self.message = message
class NotYourTurn(Exception):
def __init__(self, message):
self.message = message
class TicTacToeBoard:
def __init__(self):
self.board = {'A1': ' ', 'A2': ' ', 'A3': ' ', 'B1': ' ',
'B2': ' ', 'B3': ' ', 'C1': ' ', 'C2': ' ', 'C3': ' '}
self.values = ('X', 'O')
self.last_move = ' '
self.winner_determined = False
self.winner = ' '
def __setitem__(self, key, value):
if key in self.board.keys():
if value in self.values:
if self.board[key] == ' ':
if value != self.last_move:
self.board[key] = value
self.last_move = value
else:
raise NotYourTurn('Not your turn')
else:
raise InvalidMove('An invalid move has been attempted')
else:
raise InvalidValue('An invalid value has been entered')
else:
raise InvalidKey('An invalid key has been entered')
def __getitem__(self, key):
return self.board[key]
def __str__(self):
return '\n -------------\n' +\
'3 | | | |\n' +\
' -------------\n' +\
'2 | | | |\n' +\
' -------------\n' +\
'1 | | | |\n' +\
' -------------\n' +\
' A B C \n'
def check_draw(self):
for position in self.board.values():
if position == ' ':
return False
return True
def game_status(self):
'''Determine the status of the game'''
winning_combinations = [['A1', 'A2', 'A3'], ['B1', 'B2', 'B3'],
['C1', 'C2', 'C3'], ['A1', 'B1', 'C1'],
['A2', 'B2', 'C2'], ['A3', 'B3', 'C3'],
['A1', 'B2', 'C3'], ['A3', 'B2', 'C1']]
if not self.winner_determined:
if self.check_draw():
return 'Draw!'
else:
for winnig_combination in winning_combinations:
outcome = [self.board[position]
for position in winnig_combination
if self.board[position] != ' ']
if len(outcome) != 0 and outcome.count(outcome[0]) == 3:
self.winner_determined = True
self.winner = outcome[0]
return ('{} wins!').format(self.winner)
else:
('{} wins!').format(self.winner)
return 'Game in progress.'

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

......F.
======================================================================
FAIL: test_tostring_full (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-btpudf/test.py", line 40, in test_tostring_full
    self.assertEqual(full_board, d.__str__())
AssertionError: '\n  -------------\n3 | O | O | X |\n  -------------\n2 | X | X | O |\n  ------- [truncated]... != '\n  -------------\n3 |   |   |   |\n  -------------\n2 |   |   |   |\n  ------- [truncated]...
  
    -------------
- 3 | O | O | X |
?     ^   ^   ^
+ 3 |   |   |   |
?     ^   ^   ^
    -------------
- 2 | X | X | O |
?     ^   ^   ^
+ 2 |   |   |   |
?     ^   ^   ^
    -------------
- 1 | O | X | O |
?     ^   ^   ^
+ 1 |   |   |   |
?     ^   ^   ^
    -------------
      A   B   C  


----------------------------------------------------------------------
Ran 8 tests in 0.009s

FAILED (failures=1)

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

Николай обнови решението на 15.04.2013 16:52 (преди над 11 години)

+class InvalidKey(Exception):
+ def __init__(self, message):
+ self.message = message
+
+
+class InvalidValue(Exception):
+ def __init__(self, message):
+ self.message = message
+
+
+class InvalidMove(Exception):
+ def __init__(self, message):
+ self.message = message
+
+
+class NotYourTurn(Exception):
+ def __init__(self, message):
+ self.message = message
+
+
+class TicTacToeBoard:
+ def __init__(self):
+ self.board = {'A1': ' ', 'A2': ' ', 'A3': ' ', 'B1': ' ',
+ 'B2': ' ', 'B3': ' ', 'C1': ' ', 'C2': ' ', 'C3': ' '}
+ self.values = ('X', 'O')
+ self.last_move = ' '
+ self.winner_determined = False
+ self.winner = ' '
+
+ def __setitem__(self, key, value):
+ if key in self.board.keys():
+ if value in self.values:
+ if self.board[key] == ' ':
+ if value != self.last_move:
+ self.board[key] = value
+ self.last_move = value
+ else:
+ raise NotYourTurn('Not your turn')
+ else:
+ raise InvalidMove('An invalid move has been attempted')
+ else:
+ raise InvalidValue('An invalid value has been entered')
+ else:
+ raise InvalidKey('An invalid key has been entered')
+
+ def __getitem__(self, key):
+ return self.board[key]
+
+ def __str__(self):
+ return '\n -------------\n' +\
+ '3 | | | |\n' +\
+ ' -------------\n' +\
+ '2 | | | |\n' +\
+ ' -------------\n' +\
+ '1 | | | |\n' +\
+ ' -------------\n' +\
+ ' A B C \n'
+
+ def check_draw(self):
+ for position in self.board.values():
+ if position == ' ':
+ return False
+ return True
+
+ def game_status(self):
+ '''Determine the status of the game'''
+
+ winning_combinations = [['A1', 'A2', 'A3'], ['B1', 'B2', 'B3'],
+ ['C1', 'C2', 'C3'], ['A1', 'B1', 'C1'],
+ ['A2', 'B2', 'C2'], ['A3', 'B3', 'C3'],
+ ['A1', 'B2', 'C3'], ['A3', 'B2', 'C1']]
+
+ if not self.winner_determined:
+ if self.check_draw():
+ return 'Draw!'
+ else:
+ for winnig_combination in winning_combinations:
+ outcome = [self.board[position]
+ for position in winnig_combination
+ if self.board[position] != ' ']
+
+ if len(outcome) != 0 and outcome.count(outcome[0]) == 3:
+ self.winner_determined = True
+ self.winner = outcome[0]
+ return ('{} wins!').format(self.winner)
+ else:
+ ('{} wins!').format(self.winner)
+
+ return 'Game in progress.'