def game_onchildwin(self, board, player): """Called when a child board is won""" # Draw a line through the winning location winning_line = self.game.main_board[board].child.winning_line start_delta = CanvasHelper.get_midpoint(self.gameboard, winning_line[0][0], winning_line[0][1]) end_delta = CanvasHelper.get_midpoint(self.gameboard, winning_line[1][0], winning_line[1][1]) start_point = CanvasHelper.get_bbox(self.gameboard, board[0], board[1], 3, 3) line = (start_point[0] + start_delta[0], start_point[1] + start_delta[1], start_point[0] + end_delta[0], start_point[1] + end_delta[1]) extension = (1.1 * (line[2] - line[0]) / 8, 1.1 * (line[3] - line[1]) / 8) self.gameboard.create_line(line[0] - extension[0], line[1] - extension[1], line[2] + extension[0], line[3] + extension[1], fill="black", width=5, capstyle=tkinter.ROUND, tag=("resize", )) # Draw a big X or O over the square they won if player == game.SquareState.X: CanvasHelper.draw_x(self.gameboard, board[0], board[1], 3, 3, 10, 0.75) else: CanvasHelper.draw_o(self.gameboard, board[0], board[1], 3, 3, 10, 0.75)
def game_onchildwin(self, board, player): """Called when a child board is won""" # Draw a line through the winning location winning_line = self.game.main_board[board].child.winning_line start_delta = CanvasHelper.get_midpoint(self.gameboard, winning_line[0][0], winning_line[0][1]) end_delta = CanvasHelper.get_midpoint(self.gameboard, winning_line[1][0], winning_line[1][1]) start_point = CanvasHelper.get_bbox(self.gameboard, board[0], board[1], 3, 3) line = ( start_point[0] + start_delta[0], start_point[1] + start_delta[1], start_point[0] + end_delta[0], start_point[1] + end_delta[1]) extension = ( 1.1 * (line[2] - line[0]) / 8, 1.1 * (line[3] - line[1]) / 8) self.gameboard.create_line( line[0] - extension[0], line[1] - extension[1], line[2] + extension[0], line[3] + extension[1], fill="black", width=5, capstyle=tkinter.ROUND, tag=("resize",)) # Draw a big X or O over the square they won if player == game.SquareState.X: CanvasHelper.draw_x(self.gameboard, board[0], board[1], 3, 3, 10, 0.75) else: CanvasHelper.draw_o(self.gameboard, board[0], board[1], 3, 3, 10, 0.75)
def gameboard_onclick(self, e): """The callback for clicks on the gameboard. Calls Game.play() at the appropriate place, and returns feedback to the user.""" try: (row, col) = CanvasHelper.get_square(self.gameboard, e.x, e.y) (outer_row, outer_col, inner_row, inner_col) = (row // 3, col // 3, row % 3, col % 3) self.game.play((outer_row, outer_col), (inner_row, inner_col)) # self.set_status("Played at ({}, {}), ({}, {})".format(outer_row, outer_col, inner_row, inner_col)) # That self.game.play(...) has changed self.game.active_player so # this is backwards if self.game.active_player == game.SquareState.X: CanvasHelper.draw_o(self.gameboard, row, col) else: CanvasHelper.draw_x(self.gameboard, row, col) if self.game.child_win is not None: self.game_onchildwin(self.game.child_win[0], self.game.child_win[1]) self.game.child_win = None if self.game.overall_win is not None: CanvasHelper.higlight_available_boards(self.gameboard, ()) else: CanvasHelper.higlight_available_boards( self.gameboard, self.game.available_boards()) except game.InvalidMoveException: # self.set_status("({}, {}), ({}, {}) is an invalid move".format(outer_row, outer_col, inner_row, inner_col)) pass except CanvasHelper.OnGridException: pass
def gameboard_onclick(self, e): """The callback for clicks on the gameboard. Calls Game.play() at the appropriate place, and returns feedback to the user.""" try: (row, col) = CanvasHelper.get_square(self.gameboard, e.x, e.y) (outer_row, outer_col, inner_row, inner_col) = (row // 3, col // 3, row % 3, col % 3) self.game.play((outer_row, outer_col), (inner_row, inner_col)) # self.set_status("Played at ({}, {}), ({}, {})".format(outer_row, outer_col, inner_row, inner_col)) # That self.game.play(...) has changed self.game.active_player so # this is backwards if self.game.active_player == game.SquareState.X: CanvasHelper.draw_o(self.gameboard, row, col) else: CanvasHelper.draw_x(self.gameboard, row, col) if self.game.child_win is not None: self.game_onchildwin(self.game.child_win[0], self.game.child_win[1]) self.game.child_win = None if self.game.overall_win is not None: CanvasHelper.higlight_available_boards(self.gameboard, ()) else: CanvasHelper.higlight_available_boards(self.gameboard, self.game.available_boards()) except game.InvalidMoveException: # self.set_status("({}, {}), ({}, {}) is an invalid move".format(outer_row, outer_col, inner_row, inner_col)) pass except CanvasHelper.OnGridException: pass