board.py
def heuristic_value(self) -> float:
        """Returns the heuristic value for the board"""

        # score the board position
        score = 0

        # set players for maximizing and minimizing
        maximizing_player, minimizing_player = self.__players

        if self.__check_for_draw():
            return 0

        # score based on how many pieces are connected or close
        # the more connected pieces you have, the closer you are to winning

        def count_symbol(player: Player, *cells: Player) -> int:
            """Returns number of cells matching player"""
            return sum(cell == player for cell in cells if cell is not None)

        def score_group(*group: Player) -> int:
            """Return score to add for a group of cells"""
            # the more connected pieces you have, the closer you are to winning
            # each pair represents the number of your symbol and number of opponent's symbol
            POINTS_BY_COUNT = {
                # player has four in a row
                (self.SEQ_LENGTH, 0): 2 * self.VICTORY_VALUE,
                # player has three in a row
                (self.SEQ_LENGTH - 1, 0): 300,
                # player has two in a row
                (self.SEQ_LENGTH - 2, 0): 50,
                # player has one in a row
                (self.SEQ_LENGTH - 3, 0): 10,
            }
            # get number of max's pieces in the group
            count_max = count_symbol(maximizing_player, *group)
            # get number of min's pieces in the group
            count_min = count_symbol(minimizing_player, *group)
            # compute how much of an advantage each player has in the group
            max_advantage = POINTS_BY_COUNT.get((count_max, count_min), 0)
            min_advantage = POINTS_BY_COUNT.get((count_min, count_max), 0)
            return max_advantage - min_advantage

        # vertical groups
        for row in range(self.__rows - self.SEQ_LENGTH + 1):
            for col in range(self.__columns):
                group = [self.__grid[row + i][col] for i in range(self.SEQ_LENGTH)]
                score += score_group(*group)

        # horizontal groups
        for row in range(self.__rows):
            for col in range(self.__columns - self.SEQ_LENGTH + 1):
                group = [self.__grid[row][col + i] for i in range(self.SEQ_LENGTH)]
                score += score_group(*group)

        # diagonal down to the right
        for row in range(self.__rows - self.SEQ_LENGTH + 1):
            for col in range(self.__columns - self.SEQ_LENGTH + 1):
                group = [self.__grid[row + i][col + i] for i in range(self.SEQ_LENGTH)]
                score += score_group(*group)

        # diagonal down to the left
        for row in range(self.__rows - self.SEQ_LENGTH + 1):
            for col in range(self.SEQ_LENGTH - 1, self.__columns):
                group = [self.__grid[row + i][col - i] for i in range(self.SEQ_LENGTH)]
                score += score_group(*group)

        return score
Returns the heuristic value for the board
Returns the heuristic value for the board
Returns the heuristic value for the board
def heuristic_value(self) -> float:
        score = 0
        maximizing_player, minimizing_player = self.__players
        if self.__check_for_draw():
            return 0
        def count_symbol(player: Player, *cells: Player) -> int:
            return sum(cell == player for cell in cells if cell is not None)
        def score_group(*group: Player) -> int:
            POINTS_BY_COUNT = {
                (self.SEQ_LENGTH, 0): 2 * self.VICTORY_VALUE,
                (self.SEQ_LENGTH - 1, 0): 300,
                (self.SEQ_LENGTH - 2, 0): 50,
                (self.SEQ_LENGTH - 3, 0): 10,
            }
            count_max = count_symbol(maximizing_player, *group)
            count_min = count_symbol(minimizing_player, *group)
            max_advantage = POINTS_BY_COUNT.get((count_max, count_min), 0)
            min_advantage = POINTS_BY_COUNT.get((count_min, count_max), 0)
            return max_advantage - min_advantage
        for row in range(self.__rows - self.SEQ_LENGTH + 1):
            for col in range(self.__columns):
                group = [self.__grid[row + i][col] for i in range(self.SEQ_LENGTH)]
                score += score_group(*group)
        for row in range(self.__rows):
            for col in range(self.__columns - self.SEQ_LENGTH + 1):
                group = [self.__grid[row][col + i] for i in range(self.SEQ_LENGTH)]
                score += score_group(*group)
        for row in range(self.__rows - self.SEQ_LENGTH + 1):
            for col in range(self.__columns - self.SEQ_LENGTH + 1):
                group = [self.__grid[row + i][col + i] for i in range(self.SEQ_LENGTH)]
                score += score_group(*group)
        for row in range(self.__rows - self.SEQ_LENGTH + 1):
            for col in range(self.SEQ_LENGTH - 1, self.__columns):
                group = [self.__grid[row + i][col - i] for i in range(self.SEQ_LENGTH)]
                score += score_group(*group)
        return score
def heuristic_value ( self ) -> float : score = 0 maximizing_player , minimizing_player = self . __players if self . __check_for_draw ( ) : return 0 def count_symbol ( player : Player , * cells : Player ) -> int : """Returns number of cells matching player""" return sum ( cell == player for cell in cells if cell is not None ) def score_group ( * group : Player ) -> int : """Return score to add for a group of cells""" POINTS_BY_COUNT = { ( self . SEQ_LENGTH , 0 ) : 2 * self . VICTORY_VALUE , ( self . SEQ_LENGTH - 1 , 0 ) : 300 , ( self . SEQ_LENGTH - 2 , 0 ) : 50 , ( self . SEQ_LENGTH - 3 , 0 ) : 10 , } count_max = count_symbol ( maximizing_player , * group ) count_min = count_symbol ( minimizing_player , * group ) max_advantage = POINTS_BY_COUNT . get ( ( count_max , count_min ) , 0 ) min_advantage = POINTS_BY_COUNT . get ( ( count_min , count_max ) , 0 ) return max_advantage - min_advantage for row in range ( self . __rows - self . SEQ_LENGTH + 1 ) : for col in range ( self . __columns ) : group = [ self . __grid [ row + i ] [ col ] for i in range ( self . SEQ_LENGTH ) ] score += score_group ( * group ) for row in range ( self . __rows ) : for col in range ( self . __columns - self . SEQ_LENGTH + 1 ) : group = [ self . __grid [ row ] [ col + i ] for i in range ( self . SEQ_LENGTH ) ] score += score_group ( * group ) for row in range ( self . __rows - self . SEQ_LENGTH + 1 ) : for col in range ( self . __columns - self . SEQ_LENGTH + 1 ) : group = [ self . __grid [ row + i ] [ col + i ] for i in range ( self . SEQ_LENGTH ) ] score += score_group ( * group ) for row in range ( self . __rows - self . SEQ_LENGTH + 1 ) : for col in range ( self . SEQ_LENGTH - 1 , self . __columns ) : group = [ self . __grid [ row + i ] [ col - i ] for i in range ( self . SEQ_LENGTH ) ] score += score_group ( * group ) return score
Returns the heuristic value for the board
Returns the heuristic value for the board
"""Returns the heuristic value for the board""" # score the board position
 # set players for maximizing and minimizing
 # score based on how many pieces are connected or close
 # the more connected pieces you have, the closer you are to winning
 """Returns number of cells matching player""" """Return score to add for a group of cells""" # the more connected pieces you have, the closer you are to winning
 # each pair represents the number of your symbol and number of opponent's symbol
 # player has four in a row
 # player has three in a row
 # player has two in a row
 # player has one in a row
 # get number of max's pieces in the group
 # get number of min's pieces in the group
 # compute how much of an advantage each player has in the group
 # vertical groups
 # horizontal groups
 # diagonal down to the right
 # diagonal down to the left

self