Example #1
0
    def test_dijkstra_2(self):
        g = DirectedGraph()

        for i in range(100):
            g.add_vertex(i)

        for _ in range(250):
            a = random.randint(1, 100)
            b = random.randint(1, 100)
            if a != b:
                g.add_edge(a, b, random.randint(1, 100))

        distance, path = dijkstra(g, 1, 100)

        if path:
            cur_dist = 0
            start = 1
            for i in path:
                for edge in g.adjacency_list[start]:
                    if edge.b == i:
                        cur_dist += edge.weight
                        break
                start = i

            if cur_dist == 0:
                cur_dist = sys.maxsize

            assert cur_dist == distance
Example #2
0
 def check_win(self, color):
     """Check if a player has won using dijsktra shortest path.
     """
     if dijkstra(self, color) == 0:
         return True
     else:
         return False
Example #3
0
    def test_dijkstra_undirected(self):

        distance, path = dijkstra(self.undirected_graph, 1, 5)

        assert distance == 8

        assert path == [2, 4, 3, 5]
Example #4
0
def test_win():
    board = HexBoard(size=5)

    for i in range(5):
        board.set_piece((0,i), board.RED)
    
    board.print()

    print(board.is_game_over())

    #############################################

    board = HexBoard(size=5)

    for i in range(5):
        board.set_piece((0,i), board.BLUE)
    
    board.print()

    print(board.is_game_over())

    #############################################

    board = HexBoard(size=5)

    for i in range(5):
        board.set_piece((i,0), board.BLUE)
    
    board.print()

    print(board.is_game_over())

    #############################################

    board = HexBoard(size=5)

    for i in range(5):
        board.set_piece((i,0), board.RED)
    
    board.print()

    print(board.is_game_over())

    #############################################

    board = HexBoard(size=3)

    board.set_piece((2,0), board.BLUE)
    board.set_piece((1,1), board.BLUE)
    board.set_piece((1,2), board.BLUE)
    
    board.print()

    print(dijkstra(board, board.BLUE))

    print(board.is_game_over())
Example #5
0
def test_dijkstra():
    board = HexBoard(size=6)

    board.set_piece((2, 0), board.BLUE)
    board.set_piece((2, 1), board.BLUE)
    board.set_piece((2, 2), board.BLUE)
    board.set_piece((3, 2), board.BLUE)

    board.set_piece((3, 0), board.RED)
    board.set_piece((4, 0), board.RED)
    board.set_piece((3, 1), board.RED)
    board.set_piece((1, 2), board.RED)

    dist_blue = dijkstra(board, board.BLUE)
    dist_red  = dijkstra(board, board.RED)
    print(80*'-')
    print(dist_blue)
    print(dist_red)


    board.print()