Решение на Морски шах от Стела Живкова

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

Към профила на Стела Живкова

Резултати

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

Код

from collections import OrderedDict
class TicTacToeBoard:
def __init__(self):
self.last_move = ' '
self.board_status = 'Game in progress.'
self.lined_board = str()
self.positions_in_board = OrderedDict()
self.positions_in_board['A1'] = ' '
self.positions_in_board['A2'] = ' '
self.positions_in_board['A3'] = ' '
self.positions_in_board['B1'] = ' '
self.positions_in_board['B2'] = ' '
self.positions_in_board['B3'] = ' '
self.positions_in_board['C1'] = ' '
self.positions_in_board['C2'] = ' '
self.positions_in_board['C3'] = ' '
def __setitem__(self, value, result):
if value not in self.positions_in_board.keys():
raise InvalidKey('This is not a valid key!')
elif result != 'O' and result != 'X':
raise InvalidValue('This is not a valid value!')
elif self.positions_in_board[value] != ' ':
raise InvalidMove('The attempted move is invalid!')
elif self.last_move == result:
raise NotYourTurn('Not your turn!')
else:
self.positions_in_board[value] = result
self.last_move = result
def __str__(self):
return('\n -------------\n' +
'3 | {} | {} | {} |\n'.format(self.positions_in_board['A3'],
self.positions_in_board['B3'],
self.positions_in_board['C3']) +
' -------------\n' +
'2 | {} | {} | {} |\n'.format(self.positions_in_board['A2'],
self.positions_in_board['B2'],
self.positions_in_board['C2']) +
' -------------\n' +
'1 | {} | {} | {} |\n'.format(self.positions_in_board['A1'],
self.positions_in_board['B1'],
self.positions_in_board['C1']) +
' -------------\n' +
' A B C \n'
)
def game_status(self):
return self.board_status
class InvalidMove(Exception):
def __init__(self, message):
pass
class InvalidValue(Exception):
def __init__(self, message):
pass
class InvalidKey(Exception):
def __init__(self, message):
pass
class NotYourTurn(Exception):
def __init__(self, message):
pass

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

F..F...F
======================================================================
FAIL: test_draw (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-5ixj58/test.py", line 144, in test_draw
    self.assertEqual('Draw!', d.game_status())
AssertionError: 'Draw!' != 'Game in progress.'
- Draw!
+ Game in progress.


======================================================================
FAIL: test_o_wins (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-5ixj58/test.py", line 110, in test_o_wins
    self.assertEqual('O wins!', h.game_status())
AssertionError: 'O wins!' != 'Game in progress.'
- O wins!
+ Game in progress.


======================================================================
FAIL: test_x_wins (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-5ixj58/test.py", line 79, in test_x_wins
    self.assertEqual('X wins!', h.game_status())
AssertionError: 'X wins!' != 'Game in progress.'
- X wins!
+ Game in progress.


----------------------------------------------------------------------
Ran 8 tests in 0.008s

FAILED (failures=3)

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

Стела обнови решението на 15.04.2013 16:30 (преди над 11 години)

+class TicTacToeBoard:
+ def __init__(self):
+ self.last_move = ' '
+ self.board_status = 'Game in progress.'
+ self.line_board = str()
+ self.positions_in_board = dict()
+ self.positions_in_board['A1'] = ' '
+ self.positions_in_board['A2'] = ' '
+ self.positions_in_board['A3'] = ' '
+ self.positions_in_board['B1'] = ' '
+ self.positions_in_board['B2'] = ' '
+ self.positions_in_board['B3'] = ' '
+ self.positions_in_board['C1'] = ' '
+ self.positions_in_board['C2'] = ' '
+ self.positions_in_board['C3'] = ' '
+
+ def __setitem__(self, value, result):
+ if value not in self.positions_in_board.keys():
+ raise InvalidKey('The key is invalid!')
+ elif result != 'O' and result != 'X':
+ raise InvalidValue('The value is invalid!')
+ elif self.positions_in_board[value] != ' ':
+ raise InvalidMove('The attemted move is invalid!')
+ elif self.last_move == result:
+ raise NotYourTurn('It is not your turn!')
+ else:
+ self.positions_in_board[value] = result
+ self.last_move = result
+
+ def __str__(self):
+ return('\n -------------\n' +
+ '3 | {} | {} | {} |\n'.format(self.positions_in_board['A3'],
+ self.positions_in_board['B3'],
+ self.positions_in_board['C3']) +
+ ' -------------\n' +
+ '2 | {} | {} | {} |\n'.format(self.positions_in_board['A2'],
+ self.positions_in_board['B2'],
+ self.positions_in_board['C2']) +
+ ' -------------\n' +
+ '1 | {} | {} | {} |\n'.format(self.positions_in_board['A1'],
+ self.positions_in_board['B1'],
+ self.positions_in_board['C1']) +
+ ' -------------\n' +
+ ' A B C \n'
+ )
+
+ def game_status(self):
+ return self.board_status
+
+
+class InvalidMove(Exception):
+ def __init__(self, message):
+ pass
+
+
+class InvalidValue(Exception):
+ def __init__(self, message):
+ pass
+
+
+class InvalidKey(Exception):
+ def __init__(self, message):
+ pass
+
+
+class NotYourTurn(Exception):
+ def __init__(self, message):
+ pass

Стела обнови решението на 15.04.2013 16:34 (преди над 11 години)

+from collections import OrderedDict
+
+
class TicTacToeBoard:
def __init__(self):
self.last_move = ' '
self.board_status = 'Game in progress.'
- self.line_board = str()
- self.positions_in_board = dict()
+ self.lined_board = str()
+ self.positions_in_board = OrderedDict()
self.positions_in_board['A1'] = ' '
self.positions_in_board['A2'] = ' '
self.positions_in_board['A3'] = ' '
self.positions_in_board['B1'] = ' '
self.positions_in_board['B2'] = ' '
self.positions_in_board['B3'] = ' '
self.positions_in_board['C1'] = ' '
self.positions_in_board['C2'] = ' '
self.positions_in_board['C3'] = ' '
def __setitem__(self, value, result):
if value not in self.positions_in_board.keys():
raise InvalidKey('The key is invalid!')
elif result != 'O' and result != 'X':
raise InvalidValue('The value is invalid!')
elif self.positions_in_board[value] != ' ':
raise InvalidMove('The attemted move is invalid!')
elif self.last_move == result:
raise NotYourTurn('It is not your turn!')
else:
self.positions_in_board[value] = result
self.last_move = result
def __str__(self):
return('\n -------------\n' +
'3 | {} | {} | {} |\n'.format(self.positions_in_board['A3'],
self.positions_in_board['B3'],
self.positions_in_board['C3']) +
' -------------\n' +
'2 | {} | {} | {} |\n'.format(self.positions_in_board['A2'],
self.positions_in_board['B2'],
self.positions_in_board['C2']) +
' -------------\n' +
'1 | {} | {} | {} |\n'.format(self.positions_in_board['A1'],
self.positions_in_board['B1'],
self.positions_in_board['C1']) +
' -------------\n' +
' A B C \n'
)
def game_status(self):
return self.board_status
class InvalidMove(Exception):
def __init__(self, message):
pass
class InvalidValue(Exception):
def __init__(self, message):
pass
class InvalidKey(Exception):
def __init__(self, message):
pass
class NotYourTurn(Exception):
def __init__(self, message):
pass

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

from collections import OrderedDict
class TicTacToeBoard:
def __init__(self):
self.last_move = ' '
self.board_status = 'Game in progress.'
self.lined_board = str()
self.positions_in_board = OrderedDict()
self.positions_in_board['A1'] = ' '
self.positions_in_board['A2'] = ' '
self.positions_in_board['A3'] = ' '
self.positions_in_board['B1'] = ' '
self.positions_in_board['B2'] = ' '
self.positions_in_board['B3'] = ' '
self.positions_in_board['C1'] = ' '
self.positions_in_board['C2'] = ' '
self.positions_in_board['C3'] = ' '
def __setitem__(self, value, result):
if value not in self.positions_in_board.keys():
- raise InvalidKey('The key is invalid!')
+ raise InvalidKey('This is not a valid key!')
elif result != 'O' and result != 'X':
- raise InvalidValue('The value is invalid!')
+ raise InvalidValue('This is not a valid value!')
elif self.positions_in_board[value] != ' ':
- raise InvalidMove('The attemted move is invalid!')
+ raise InvalidMove('The attempted move is invalid!')
elif self.last_move == result:
- raise NotYourTurn('It is not your turn!')
+ raise NotYourTurn('Not your turn!')
else:
self.positions_in_board[value] = result
self.last_move = result
def __str__(self):
return('\n -------------\n' +
'3 | {} | {} | {} |\n'.format(self.positions_in_board['A3'],
self.positions_in_board['B3'],
self.positions_in_board['C3']) +
' -------------\n' +
'2 | {} | {} | {} |\n'.format(self.positions_in_board['A2'],
self.positions_in_board['B2'],
self.positions_in_board['C2']) +
' -------------\n' +
'1 | {} | {} | {} |\n'.format(self.positions_in_board['A1'],
self.positions_in_board['B1'],
self.positions_in_board['C1']) +
' -------------\n' +
' A B C \n'
)
def game_status(self):
return self.board_status
class InvalidMove(Exception):
def __init__(self, message):
pass
class InvalidValue(Exception):
def __init__(self, message):
pass
class InvalidKey(Exception):
def __init__(self, message):
pass
class NotYourTurn(Exception):
def __init__(self, message):
pass