class Game(object): """ The main Tic-Tac-Toe game. This forms the basis of the game & is UI-independent. With light extension, it can do console output, GUI clients, APIs, etc. Because overengineering. """ def __init__(self, size=None, player_marker='X'): super(Game, self).__init__() self.board = Board(size=size) self.player_marker = player_marker if self.player_marker == 'X': self.computer_marker = 'O' else: self.computer_marker = 'X' self.ai = EasyAI() def player_move(self, x, y): self.board.play(x, y, self.player_marker) return self.board.check_for_win() == self.player_marker def computer_move(self): x, y = self.ai.next_move(self.board) self.board.play(x, y, self.computer_marker) return self.board.check_for_win() == self.computer_marker def computer_turn(self): if not self.board.moves_left(): return False if self.computer_move(): return True return False def run(self): while self.board.moves_left(): if self.player_marker == 'X': # If the player is the X, they get to go first. self.draw_board() if self.player_turn(): self.draw_win("You win!") return self.draw_board() if self.computer_turn(): self.draw_win('The AI wins!') return else: # Otherwise, the computer gets to go first. self.draw_board() if self.computer_turn(): self.draw_win('The AI wins!') return self.draw_board() if self.player_turn(): self.draw_win("You win!") return self.draw_win('Tie game. :(') # The unimplemented. def draw_board(self): raise NotImplementedError("Your subclass needs to implement `draw_board`.") def draw_win(self, message): raise NotImplementedError("Your subclass needs to implement `draw_board`.") def player_turn(self): raise NotImplementedError("Your subclass needs to implement `draw_board`.")
def __init__(self, size=None, player_marker='X'): super(Game, self).__init__() self.board = Board(size=size) self.player_marker = player_marker if self.player_marker == 'X': self.computer_marker = 'O' else: self.computer_marker = 'X' self.ai = EasyAI()
def make_move(self, field): coord = EasyAI.generate_move(field) field.modify_field(coord)
class Game(object): """ The main Tic-Tac-Toe game. This forms the basis of the game & is UI-independent. With light extension, it can do console output, GUI clients, APIs, etc. Because overengineering. """ def __init__(self, size=None, player_marker='X'): super(Game, self).__init__() self.board = Board(size=size) self.player_marker = player_marker if self.player_marker == 'X': self.computer_marker = 'O' else: self.computer_marker = 'X' self.ai = EasyAI() def player_move(self, x, y): self.board.play(x, y, self.player_marker) return self.board.check_for_win() == self.player_marker def computer_move(self): x, y = self.ai.next_move(self.board) self.board.play(x, y, self.computer_marker) return self.board.check_for_win() == self.computer_marker def computer_turn(self): if not self.board.moves_left(): return False if self.computer_move(): return True return False def run(self): while self.board.moves_left(): if self.player_marker == 'X': # If the player is the X, they get to go first. self.draw_board() if self.player_turn(): self.draw_win("You win!") return self.draw_board() if self.computer_turn(): self.draw_win('The AI wins!') return else: # Otherwise, the computer gets to go first. self.draw_board() if self.computer_turn(): self.draw_win('The AI wins!') return self.draw_board() if self.player_turn(): self.draw_win("You win!") return self.draw_win('Tie game. :(') # The unimplemented. def draw_board(self): raise NotImplementedError( "Your subclass needs to implement `draw_board`.") def draw_win(self, message): raise NotImplementedError( "Your subclass needs to implement `draw_board`.") def player_turn(self): raise NotImplementedError( "Your subclass needs to implement `draw_board`.")