Георги обнови решението на 15.04.2013 15:08 (преди над 11 години)
+import re
+
+
+class InvalidMove(Exception):
+
+ def __init__(self):
+ self.message = "Invalid move! Field already contains a value!"
+
+
+class InvalidValue(Exception):
+
+ def __init__(self):
+ self.message = "Invalid value! Allowed values are 'X' and 'O'!"
+
+
+class InvalidKey(Exception):
+
+ def __init__(self):
+ self.message = ("Invalid coordinates! Vallid coordinates are cappital"
+ "letters A-C followed by numbers 1-3")
+
+
+class NotYourTurn(Exception):
+
+ def __init__(self):
+ self.message = "It is the other players turn!"
+
+
+class TicTacToeBoard(object):
+
+ def __init__(self):
+ self.table = [[" ", " ", " "], [" ", " ", " "], [" ", " ", " "]]
+ self.lastTurn = ""
+ self.first = 1
+ self.gameStatus = 'Game in progress.'
+
+ def __str__(self):
+ return ('\n -------------\n'
+ '3 | {0} | {1} | {2} |\n'
+ ' -------------\n'
+ '2 | {3} | {4} | {5} |\n'
+ ' -------------\n'
+ '1 | {6} | {7} | {8} |\n'
+ ' -------------\n'
+ ' A B C \n'.format(self.table[0][0], self.table[0][1],
+ self.table[0][2], self.table[1][0], self.table[1][1],
+ self.table[1][2], self.table[2][0], self.table[2][1],
+ self.table[2][2]))
+
+ def __setitem__(self, name, value):
+ if (name is not None and value is not None):
+ keyMatches = bool(re.search(r'^[ABC][123]$', name.__str__()))
+ valueMatches = bool(re.search(r'^[XO]$', value.__str__()))
+ if (not keyMatches):
+ raise InvalidKey
+ elif (not valueMatches):
+ raise InvalidValue
+ else:
+ if (self.first):
+ self.first = 0
+ elif (self.lastTurn == value.__str__()):
+ raise NotYourTurn
+ self.setField(name.__str__()[0],
+ name.__str__()[1], value.__str__())
+
+ def __getitem__(self, name):
+ if (name is not None):
+ keyMatches = bool(re.search(r'^[ABC][123]$', name.__str__()))
+ if (not keyMatches):
+ raise InvalidKey
+ else:
+ self.getField(name.__str__()[0], name.__str__()[1])
+
+ def setField(self, collumn, row, value):
+ realCollumn = ord(collumn) - ord('A')
+ realRow = 3 - (ord(row) - ord('0'))
+ field = self.table[realRow][realCollumn]
+ if (field != " "):
+ raise InvalidMove
+ else:
+ self.table[realRow][realCollumn] = value
+ self.lastTurn = value
+ self.checkState(realRow, realCollumn, value)
+
+ def getField(self, collumn, row):
+ realCollumn = ord(collumn) - ord('A')
+ realRow = 3 - (ord(row) - ord('0'))
+ print ("'{0}'".format(self.table[realRow][realCollumn]))
+
+ def game_status(self):
+ return self.gameStatus
+
+ def checkState(self, row, collumn, value):
+ if (self.gameStatus == 'Game in progress.'):
+ win = [value, value, value]
+ a = []
+ [a.extend(x) for x in self.table]
+ if (" " not in a):
+ self.gameStatus = 'Draw!'
+ elif (
+ win in [self.table[row], [self.table[collumn][0],
+ self.table[collumn][1], self.table[collumn][2]],
+ [self.table[0][0], self.table[1][1], self.table[2][2]],
+ [self.table[2][0], self.table[1][1], self.table[0][2]]]):
+ self.gameStatus = '{0} wins!'.format(
+ self.table[row][collumn])
Засичането ти на вертикална победа не работи.