Йордан обнови решението на 09.04.2013 18:37 (преди над 11 години)
+import re
+
+
+class InvalidMove(Exception):
+ pass
+
+
+class InvalidValue(Exception):
+ pass
+
+
+class InvalidKey(Exception):
+ pass
+
+
+class NotYourTurn(Exception):
+ pass
+
+
+class TicTacToeBoard(dict):
+
+ def __init__(self, players=None, columns=None, rows=None):
+ self.players = ('X', 'O') if players is None else players
+ self.columns = ('A', 'B', 'C') if columns is None else columns
+ self.rows = range(1, 4) if rows is None else rows
+ self.moves = []
+ self.cells_count = len(self.columns) * len(self.rows)
+
+ def __str__(self):
+ row_separator = '\n ' + '-' * 13 + '\n'
+ board_string = row_separator
+ for row in reversed(self.rows):
+ board_string += '%d |' % row
+ for column in self.columns:
+ board_string += ' %s |' % dict(
+ self.moves).get('%s%d' % (column, row), ' ')
+ board_string += row_separator
+ board_string += ' ' + ' '.join(
+ [' %s ' % column for column in self.columns]) + ' \n'
+ return board_string
+
+ def game_status(self):
+ cols = self.columns
+ rows = self.rows
+ win = [['%s%d' % (col, row) for col in cols] for row in rows] +\
+ [['%s%d' % (col, row) for row in rows] for col in cols] +\
+ [['%s%d' % (col, dep+1) for dep, col in enumerate(cols)]] +\
+ [['%s%d' % (col, dep+1)
+ for dep, col in enumerate(reversed(cols))]]
+ for player in self.players:
+ if any([all([dict(self.moves).get(pattern_item) == player
+ for pattern_item in pattern]) for pattern in win]):
+ return '%s wins!' % player
+ if len(self.moves) == self.cells_count:
+ return 'Draw!'
+ else:
+ return 'Game in progress.'
+
+ def __setitem__(self, key, value):
+ if key in self.keys():
+ raise InvalidMove()
+ if value not in self.players:
+ raise InvalidValue()
+ if not re.match('^[%s-%s][1-3]$' % (self.columns[0],
+ self.columns[-1]), key):
+ raise InvalidKey()
+ if self.moves and self.moves[-1][1] == value:
+ raise NotYourTurn()
+
+ self.moves.append((key, value))
+ return dict.__setitem__(self, key, value)
Един от feature-те на моят код е, че може да работи с променлива бройка редове/колони :)
In [6]: board = TicTacToeBoard(columns=('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'), rows=range(1,9))
In [7]: print(board)
---------------------------------
8 | | | | | | | | |
---------------------------------
7 | | | | | | | | |
---------------------------------
6 | | | | | | | | |
---------------------------------
5 | | | | | | | | |
---------------------------------
4 | | | | | | | | |
---------------------------------
3 | | | | | | | | |
---------------------------------
2 | | | | | | | | |
---------------------------------
1 | | | | | | | | |
---------------------------------
A B C D E F G H