Михаил обнови решението на 14.04.2013 15:19 (преди над 11 години)
+class TicTacToeBoard:
+ KEYS = ["A1", "A2", "A3", "B1", "B2", "B3", "C1", "C2", "C3"]
+
+ def __init__(self):
+ self.board = {}
+ self.turn = ""
+ self.status = "Game in progress."
+
+ def __str__(self):
+ return self.get_board()
+
+ def __setitem__(self, key, value):
+ if key not in self.KEYS:
+ raise InvalidKey()
+ if key in self.board:
+ raise InvalidMove()
+ if value != "X" and value != "O":
+ raise InvalidValue()
+ if self.turn == value:
+ raise NotYourTurn()
+
+ self.board[key] = value
+ self.turn = value
+
+ def __getitem__(self, key):
+ return self.board[key]
+
+ def get_board(self):
+ a1 = self.board["A1"] if "A1" in self.board else " "
+ a2 = self.board["A2"] if "A2" in self.board else " "
+ a3 = self.board["A3"] if "A3" in self.board else " "
+ b1 = self.board["B1"] if "B1" in self.board else " "
+ b2 = self.board["B2"] if "B2" in self.board else " "
+ b3 = self.board["B3"] if "B3" in self.board else " "
+ c1 = self.board["C1"] if "C1" in self.board else " "
+ c2 = self.board["C2"] if "C2" in self.board else " "
+ c3 = self.board["C3"] if "C3" in self.board else " "
+ return self.print_board(a1, a2, a3, b1, b2, b3, c1, c2, c3)
+
+ def print_board(self, a1, a2, a3, b1, b2, b3, c1, c2, c3):
+ current_board = "\n -------------\n"
+ current_board += "3 | " + a3 + " | " + b3 + " | " + c3 + " |\n"
+ current_board += " -------------\n"
+ current_board += "2 | " + a2 + " | " + b2 + " | " + c2 + " |\n"
+ current_board += " -------------\n"
+ current_board += "1 | " + a1 + " | " + b1 + " | " + c1 + " |\n"
+ current_board += " -------------\n"
+ current_board += " A B C \n"
+ return current_board
+
+ def game_status(self):
+ a1 = self.board["A1"] if "A1" in self.board else "None"
+ a2 = self.board["A2"] if "A2" in self.board else "None"
+ a3 = self.board["A3"] if "A3" in self.board else "None"
+ b1 = self.board["B1"] if "B1" in self.board else "None"
+ b2 = self.board["B2"] if "B2" in self.board else "None"
+ b3 = self.board["B3"] if "B3" in self.board else "None"
+ c1 = self.board["C1"] if "C1" in self.board else "None"
+ c2 = self.board["C2"] if "C2" in self.board else "None"
+ c3 = self.board["C3"] if "C3" in self.board else "None"
+ if self.status == "Game in progress.":
+ self.status = self.check_game_status(a1, a2, a3, b1, b2, b3, c1, c2, c3)
+ return self.status
+ else:
+ return self.status
+
+ def all_same(self, items, value):
+ return all(x == value for x in items)
+
+ def check_game_status(self, a1, a2, a3, b1, b2, b3, c1, c2, c3):
+ a_row = [a1, a2, a3]
+ b_row = [b1, b2, b3]
+ c_row = [c1, c2, c3]
+ a_col = [a1, b1, c1]
+ b_col = [a2, b2, c2]
+ c_col = [a3, b3, c3]
+ first_diagonal = [a1, b2, c3]
+ second_diagonal = [a3, b2, c1]
+ row_positions = [a_row, b_row, c_row]
+ col_positions = [a_col, b_col, c_col]
+ diagonal_positions = [first_diagonal, second_diagonal]
+
+ if any(self.all_same(row, "X") for row in row_positions):
+ return "X wins!"
+ elif any(self.all_same(row, "O") for row in row_positions):
+ return "O wins!"
+ elif any(self.all_same(row, "X") for row in col_positions):
+ return "X wins!"
+ elif any(self.all_same(row, "O") for row in col_positions):
+ return "O wins!"
+ if any(self.all_same(row, "X") for row in diagonal_positions):
+ return "X wins!"
+ elif any(self.all_same(row, "O") for row in diagonal_positions):
+ return "O wins!"
+ if len(self.board) == 9:
+ return "Draw!"
+ else:
+ return "Game in progress."
+
+
+class NotYourTurn(Exception):
+ pass
+
+
+class InvalidMove(Exception):
+ pass
+
+
+class InvalidKey(Exception):
+ pass
+
+
+class InvalidValue(Exception):
+ pass
Имаш доста повтарящ се код. Пробвай да го направиш в цикъл.