Стела обнови решението на 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