Example #1
0
    def erase_board(self, territory_board):
        """
        Erases the territory.
        """
        
        board_size = len(territory_board)

        vertexes_with_value = self.get_vertexes_with_value(territory_board)
        vertexes_to_replace = []
        
        for vertex in vertexes_with_value:
            near_vertexes = get_vertex_around_position(board_size, vertex)
            value_to_replace = territory_board[vertex.x][vertex.y]
            to_substract = 0
            
            for near_vertex in near_vertexes:
                value = territory_board[near_vertex.x][near_vertex.y]
                
                if (value > 0 and value_to_replace < 0) or value == 0:#Fix!
                    to_substract += 1
            
            if value_to_replace > 0:
                if value_to_replace < to_substract:
                    vertexes_to_replace.append((vertex, 0))
                else:
                    vertexes_to_replace.append((vertex, value_to_replace - to_substract))
            else:
                if abs(value_to_replace) < to_substract:
                    vertexes_to_replace.append((vertex, 0))
                else:
                    vertexes_to_replace.append((vertex, value_to_replace + to_substract))
        
        self.replace_values(territory_board, vertexes_to_replace)
Example #2
0
 def test_get_squares_around_position(self):
     
     board = Board(9)
     
     squares = get_vertex_around_position(board.size, Vertex(0,0))
     
     self.assertEqual(len(squares), 2)
     
     squares = get_vertex_around_position(board.size, Vertex(8,0))
     
     self.assertEqual(len(squares), 2)
     
     squares = get_vertex_around_position(board.size, Vertex(7,3))
     
     self.assertEqual(len(squares), 4)
     
     squares = get_vertex_around_position(board.size, Vertex(8,4))
     
     self.assertEqual(len(squares), 3)
Example #3
0
    def add_liberties(self, board, stone):
        """
        Pre: the move is valid
        Updates the liberties after a stone is added to this group
        """
        if stone in self.liberties:
            self.liberties.remove(stone)

        possible_liberties = get_vertex_around_position(board.size, stone)
        
        for possible_libertie in possible_liberties:
            if possible_libertie not in self.liberties and board.content[possible_libertie.x][possible_libertie.y] == EMPTY:
                self.liberties.append(possible_libertie)
Example #4
0
 def count_stones_around(self, territory_board, vertex):
     """
     Returns the number of white and black stones of stones
     around a vertex. 
     """
     
     vertexes_to_check = get_vertex_around_position(len(territory_board), vertex)
             
     black_stones_around = 0
     white_stones_around = 0
                
     for vertex_to_check in vertexes_to_check:
         if territory_board[vertex_to_check.x][vertex_to_check.y] > 0:
             white_stones_around += 1
         elif territory_board[vertex_to_check.x][vertex_to_check.y] < 0:
             black_stones_around += 1
             
     return white_stones_around, black_stones_around
Example #5
0
    def __add_to_group(self, move):
        """
        Adds the stone to the corresponding group.
        If no group is near, it creates a new group. 
        If the stone can be added to more than one group, the groups have to be fusioned 
        """
        vertex_list = get_vertex_around_position(self.size, move.vertex)
        color = move.color
        new_stone = move.vertex
        near_groups = []
        
        for vertex in vertex_list:
            x, y = vertex.x, vertex.y
            if self.content[x][y] == color:
                near_groups.append(self.get_group(vertex))

        if not near_groups:
            # Creates new group
            self.groups.append(Group(self, color, new_stone))
        elif len(near_groups) == 1:
            # Adds stone to the group
            near_groups[0].add_stone(self, new_stone)
        else:
            # Fusion groups
            new_group = Group(self, color, new_stone)
            stone_list = []

            for group in near_groups:
                if group in self.groups:
                    stone_list += group.stones
                    self.groups.remove(group)

            for stone in set(stone_list):
                new_group.add_stone(self, stone)

            self.groups.append(new_group)
Example #6
0
    def dilatate_board(self, territory_board):
        """
        Dilatates the territory.
        """
        
        board_size = len(territory_board)

        vertexes_with_value = self.get_vertexes_with_value(territory_board)
        vertexes_to_replace = []

        for vertex in vertexes_with_value:
            near_vertexes = get_vertex_around_position(board_size, vertex)
            
            for near_vertex in near_vertexes:
                
                white_stones_around, black_stones_around = self.count_stones_around(territory_board, near_vertex)
                
                vertex_value = territory_board[near_vertex.x][near_vertex.y]
                
                if vertex_value == 0:                   
                    if white_stones_around != 0 and black_stones_around == 0:
                        vertexes_to_replace.append((near_vertex, white_stones_around))
                    elif black_stones_around != 0 and white_stones_around == 0:
                        vertexes_to_replace.append((near_vertex, -black_stones_around))
                elif vertex_value > 0:
                    if black_stones_around != 0:
                        continue
                    
                    vertexes_to_replace.append((near_vertex, vertex_value + white_stones_around))
                else:
                    if white_stones_around != 0:
                        continue
                    
                    vertexes_to_replace.append((near_vertex, vertex_value - black_stones_around))
                
        self.replace_values(territory_board, vertexes_to_replace)