Ния обнови решението на 13.04.2013 01:40 (преди над 11 години)
+class TicTacToeBoard:
+ def __init__(self):
+ self.fields = {'A1': ' ', 'A2': ' ', 'A3': ' ',
+ 'B1': ' ', 'B2': ' ', 'B3': ' ',
+ 'C1': ' ', 'C2': ' ', 'C3': ' '}
+ self.values = ('X', 'O')
+ self.count = 0
+ self.last_move = None
+
+ def __setitem__(self, field, value):
+ if field not in self.fields:
+ raise InvalidKey
+ if value not in self.values:
+ raise InvalidValue
+ if self.fields[field] != ' ':
+ raise InvalidMove
+ if self.last_move == value:
+ raise NotYourTurn
+
+ self.fields[field] = value
+ self.last_move = value
+ self.count += 1
+
+ def __getitem__(self, key):
+ return self.fields[key]
+
+ def __str__(self):
+ board = '\n -------------\n' +\
+ '3 | ' + self.fields['A3'] + ' | ' + self.fields['B3'] +\
+ ' | ' + self.fields['C3'] + ' |\n' +\
+ ' -------------\n' +\
+ '2 | ' + self.fields['A2'] + ' | ' + self.fields['B2'] +\
+ ' | ' + self.fields['C2'] + ' |\n' +\
+ ' -------------\n' +\
+ '1 | ' + self.fields['A1'] + ' | ' + self.fields['B1'] +\
+ ' | ' + self.fields['C1'] + ' |\n' +\
+ ' -------------\n' +\
+ ' A B C \n'
+ return board
+
+ def check_for_win(self, mark):
+ win_rows = [[self.fields['A1'], self.fields['B1'], self.fields['C1']],
+ [self.fields['A2'], self.fields['B2'], self.fields['C2']],
+ [self.fields['A3'], self.fields['B3'], self.fields['C3']],
+ [self.fields['A1'], self.fields['A2'], self.fields['A3']],
+ [self.fields['B1'], self.fields['B2'], self.fields['B3']],
+ [self.fields['C1'], self.fields['C2'], self.fields['C3']],
+ [self.fields['C1'], self.fields['B2'], self.fields['A3']],
+ [self.fields['A1'], self.fields['B2'], self.fields['C3']]]
+
+ for row in win_rows:
+ if row[0] == row[1] == row[2] == mark:
+ return True
+ return False
+
+ def game_status(self):
+ if self.check_for_win('X'):
+ return 'X wins!'
+ elif self.check_for_win('O'):
+ return 'O wins!'
+ elif self.count == 9:
+ return 'Draw!'
+ else:
+ return 'Game in progress.'
+
+
+class InvalidKey(Exception):
+ pass
+
+
+class InvalidValue(Exception):
+ pass
+
+
+class InvalidMove(Exception):
+ pass
+
+
+class NotYourTurn(Exception):
+ pass
+
Можеш да направиш __str__
метода ти още по-красив с .format()
:-)