Мария обнови решението на 15.04.2013 15:55 (преди над 11 години)
+class InvalidMove(Exception):
+ pass
+
+
+class InvalidValue(Exception):
+ pass
+
+
+class InvalidKey(Exception):
+ pass
+
+
+class NotYourTurn(Exception):
+ pass
+
+
+class TicTacToeBoard:
+
+ def __init__(self):
+
+ self.board = {"A1": " ", "A2": " ", "A3": " ", "B1": " ", "B2": " ",
+ "B3": " ", "C1": " ", "C2": " ", "C3": " "}
+ self.empty_board = ["\n -------------\n", "3 | ", " | ", " | ",
+ " |\n", " -------------\n", "2 | ", " | ",
+ " | ", " |\n", " -------------\n", "1 | ",
+ " | ", " | ", " |\n", " -------------\n",
+ " A B C \n"]
+ self.fill_in_board = {2: "A3", 3: "B3", 4: "C3", 7: "A2", 8: "B2",
+ 9: "C2", 12: "A1", 13: "B1", 14: "C1"}
+ self.last_move = None
+ self.correct_values = ["X", "O"]
+
+ def __setitem__(self, key, value):
+ if key not in self.board:
+ raise InvalidKey
+
+ if value not in self.correct_values:
+ raise InvalidValue
+
+ if self.board[key] is not " ":
+ raise InvalidMove
+
+ if self.last_move == value:
+ raise NotYourTurn
+
+ self.board[key] = value
+ self.last_move = value
+
+ def __getitem__(self, key):
+ return self.board[key]
+
+ def __str__(self):
+ board_print = ""
+ for i in range(0, 17):
+ if i is 2 or i is 3 or i is 4 or i is 7 or i is 8 or i is 9 or i is 12 or i is 13 or i is 14:
+ board_print += self.board[self.fill_in_board[i]] +\
+ self.empty_board[i]
+ else:
+ board_print += self.empty_board[i]
+ return board_print
+
+ def game_status(self):
+
+ def is_board_have_empty_space():
+ for key in self.board:
+ if self.board[key] is " ":
+ return True
+ return False
+
+ board_place = is_board_have_empty_space()
+
+ if self.board["A1"] is self.board["B1"] is self.board["C1"] or self.board["A1"] is self.board["A2"] is self.board["A3"]:
+ if self.board["A1"] is not " ":
+ return self.board["A1"] + " wins!"
+ else:
+ return "Game in progress."
+ elif self.board["A2"] is self.board["B2"] is self.board["C2"] or self.board["B1"] is self.board["B2"] is self.board["B3"]:
+ if self.board["B2"] is not " ":
+ return self.board["B2"] + " wins!"
+ else:
+ return "Game in progress."
+ elif self.board["A3"] is self.board["B3"] is self.board["C3"] or self.board["C1"] is self.board["C2"] is self.board["C3"]:
+ if self.board["C3"] is not " ":
+ return self.board["C3"] + " wins!"
+ else:
+ return "Game in progress."
+ elif self.board["A3"] is self.board["B2"] is self.board["C1"] or self.board["A1"] is self.board["B2"] is self.board["C3"]:
+ if self.board["B2"] is not " ":
+ return self.board["B2"] + " wins!"
+ else:
+ return "Game in progress."
+ elif board_place is True:
+ return "Game in progress."
+ else:
+ return "Draw!"