Ejemplo n.º 1
0
 def draw(self):
     # A wrapper around the `ConnectFourGraphics.draw_board` function that
     # picks all the right components of `self`.
     ConnectFourGraphics.draw_board(self.display, self.board,
             self.score_red, self.score_blue,
             self.selected_index, self.game_running,
             self.human_turn(), self.red_turn, self.winner)
Ejemplo n.º 2
0
    def __init__(self,
            height = 9, width = 9,
            rewards = None,
            winscore = 100,
            red_player = None,
            blue_player = None,
            ai_delay = 60
            ):

        ## initialise pygame
        pygame.init()
        pygame.font.init()

        ## board
        self.board = ConnectFourBoard.EmptyBoard(height, width, rewards, winscore)

        ## interface
        self.selected_index = -1
        self.display = ConnectFourGraphics.setup_display(self.board)

        ### PLAYER SETTINGS ###
        self.red_player = red_player
        self.blue_player = blue_player
        self.ai_delay = ai_delay
        self.time_cumulative = {ConnectFourBoard.RED : 0, ConnectFourBoard.BLUE : 0}

        ## state of the game (scoreboard, who's turn it is, etc.)
        self.score_red = 0
        self.score_blue = 0
        self.winner = 0
        self.game_running = True
        self.red_turn = True

        ## draw initial board
        self.draw()
Ejemplo n.º 3
0
    def game_loop(self):
        while self.game_running:
            # Let the AI play if it's its turn
            if not self.human_turn():
                start_ai_time = pygame.time.get_ticks()
                token = self.turn_token()
                if token == ConnectFourBoard.RED:
                    move = self.red_player(self, token)
                elif token == ConnectFourBoard.BLUE:
                    move = self.blue_player(self, token)
                self.attempt_insert(move)
                stop_ai_time = pygame.time.get_ticks()
                ai_time_span = stop_ai_time - start_ai_time
                if self.game_running:
                    print("Time taken for move: {}".format(ai_time_span))
                if not self.red_turn:
                    self.red_times.append(ai_time_span)
                else:
                    self.blue_times.append(ai_time_span)
                if ai_time_span < self.ai_delay:
                    pygame.time.delay(self.ai_delay - ai_time_span)

            # Process all events, especially mouse events.
            for event in pygame.event.get():
                if event.type == QUIT:
                    pygame.quit()
                    sys.exit(0)
                if event.type == MOUSEMOTION:
                    self.selected_index = \
                        ConnectFourGraphics.hovered_col(self.board)
                if event.type == MOUSEBUTTONDOWN and event.button == 1:
                    if self.human_turn():
                        self.attempt_insert(self.selected_index)

            # Refresh the display and loop back
            self.draw()
            pygame.time.wait(40)

        # Once the game is finish, simply wait for the `QUIT` event
        while True:
            event = pygame.event.wait()
            if event.type == QUIT:
                pygame.quit()
                sys.exit(0)
            pygame.time.wait(60)
Ejemplo n.º 4
0
    def __init__(self,
                 height=9,
                 width=9,
                 rewards=None,
                 red_player=None,
                 blue_player=None,
                 ai_delay=60,
                 winscore=1000):

        self.field_state = [[0, 0, 0, 0, 0, 0], [1, 2, 1, 1, 0, 0],
                            [1, 0, 0, 0, 0, 0], [2, 1, 0, 0, 0, 0],
                            [1, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0],
                            [0, 0, 0, 0, 0, 0]]
        self.col_heights = [0, 4, 1, 2, 1, 0, 0]

        ## initialise pygame
        pygame.init()
        pygame.font.init()

        ## board
        self.board = ConnectFourBoard.EmptyBoard(height, width, rewards,
                                                 winscore)

        ## interface
        self.selected_index = -1
        self.display = \
            ConnectFourGraphics.setup_display(self.board)

        ### PLAYER SETTINGS ###
        self.red_player = red_player
        self.blue_player = blue_player
        self.ai_delay = ai_delay

        ## state of the game (scoreboard, who's turn it is, etc.)
        self.score_red = 0
        self.score_blue = 0
        self.winner = 0
        self.score_win = 1000
        self.game_running = True
        self.red_turn = True

        ## draw initial board
        self.draw()
Ejemplo n.º 5
0
    def game_loop(self):
        while self.game_running:

            # Let the AI play if it's its turn
            if not self.human_turn():
                start_ai_time = pygame.time.get_ticks()
                token = self.turn_token()
                if token == ConnectFourBoard.RED:
                    move = self.red_player(self.board, token, self.red_turn)
                elif token == ConnectFourBoard.BLUE:
                    move = self.blue_player(self.board, token, self.red_turn)
                self.attempt_insert(move)
                stop_ai_time = pygame.time.get_ticks()
                ai_time_span = stop_ai_time - start_ai_time
                self.time_cumulative[token] = self.time_cumulative[token] + ai_time_span
                if ai_time_span < self.ai_delay:
                    pygame.time.delay(self.ai_delay - ai_time_span)

            # Process all events, especially mouse events.
            for event in pygame.event.get():
                if event.type == QUIT:
                    pygame.quit()
                    sys.exit(0)
                if event.type == MOUSEMOTION:
                    self.selected_index = ConnectFourGraphics.hovered_col(self.board)
                if event.type == MOUSEBUTTONDOWN and event.button == 1:
                    if self.human_turn():
                        self.attempt_insert(self.selected_index)

            # Refresh the display and loop back
            self.draw()
            pygame.time.wait(40)

        #return ("Red" if self.winner==1 else "Blue" if self.winner==2 else "None", self.time_cumulative[1], self.time_cumulative[2])
        # Once the game is finish, simply wait for the `QUIT` event
        while True:
            event = pygame.event.wait()
            if event.type == QUIT:
                pygame.quit()
                sys.exit(0)
            pygame.time.wait(60)