Example #1
0
from Graph import Graph
from AStarSearch import AStarSearch
from util import *

# Width and height of graph grid, must be redefined for various graphs
WIDTH = 10
HEIGHT = 8

if __name__ == "__main__":
    # Command line parser
    parser = argparse.ArgumentParser()
    parser.add_argument(
        "input_file", help="The name of the file to treat as the search space")
    parser.add_argument("heuristic",
                        help="Name of search heuristic to use in A* search",
                        choices=("manhattan", "diagonal"),
                        default="manhattan")
    args = parser.parse_args()

    # open file
    f = open(args.input_file, 'r')
    g = Graph(WIDTH, HEIGHT)

    # Create our graph structure to traverse
    create_graph_from_file(f, g)

    # Create and perform A* search
    search = AStarSearch(g.get(0, 0), g.get(WIDTH - 1, HEIGHT - 1), g,
                         args.heuristic)
    search.search()
Example #2
0
    x = ReadFile("test.txt", "map-out.txt")
    t = x.read_file()

    food_fen = FoodGenerator()

    for m in range(0,11):
        init_state = GameMap(t, None, [0, 2], [[0, 2], [0, 1], [0, 0]], True)
        init_state_diagonal = GameMap(t, None, [0, 2], [[0, 2], [0, 1], [0, 0]], False)
        init_state.set_food_position(food_fen.generate_food(init_state.game_map))
        init_state_diagonal.set_food_position(food_fen.generate_food(init_state.game_map))

        movement_list_manhattan = []
        movement_list_diagonal = []
        for i in range(0, 30):
            # print "Score=", i
            mySearch = AStarSearch(init_state)
            diagonalSearch = AStarSearch(init_state_diagonal)
            diagonalSearch.start_search(False)
            mySearch.start_search(True)
            answer = mySearch.get_answer()
            answer_diagonal = diagonalSearch.get_answer()
            movement_list_manhattan.append(len(answer) - 1)
            movement_list_diagonal.append(len(answer_diagonal) - 1)
            pygame.init()
            map_length = 300
            screen = draw_table(len(t), map_length)
            draw_obstacles(len(t), screen, answer[0].get_obstacle_pos(), map_length)
            draw_snake(len(t), screen, answer[0].snake_position, map_length)
            draw_food(len(t), screen, answer[0].food_position, map_length)

            index = 0