import copy
import sys
from collections import defaultdict
from data_structures import Tra_model, Stack, Queue
from state_rep import extractParams
from depth_first import single_dfs
from breadth_first import single_bfs
from runAStar import *




# Take a .txt file, parses out the maze, and returns a 
# 2D representation of the array

def parser(file):
    file_open = open (file, "r")
    big_list =  []
        
    for line in file_open:
        line = line.strip("\n")
        line = list(line)
        big_list.append(line)

    return big_list       	#Returns the 2d array


#--------------------------------------------------------------------

# You can't run both BFS and DFS at same time. Because both of the 
# functions make changes to the biglist. So ones result will affect the 
# others. 

def main():

	check = False

	while check is False:
		print("\n")
		print("Welcome to Maze Search!")
		print("Please use one of the following input files: ")
		print("1prize-large.txt")
		print("1prize-medium.txt")
		print("1prize-open.txt")
		print("1prize-tiny.txt")

		in_file = input("Maze File: ")
		print("\n")
		print("please select the search algorithm")
		print("bfs, dfs, astar")
		algorithm = input("Search Algorithm: ")

		if algorithm == "bfs" or algorithm == "dfs" or algorithm == "astar":
			check = True


	big_list = parser(in_file)
	params = extractParams(big_list)
	start = params['start']


	if algorithm == "astar":
		a_star_driver(in_file)

    
	if algorithm == 'bfs' or algorithm == "dfs":
		if algorithm == "bfs":
			exp_bfs, graph, steps, expanded = single_bfs(big_list, start) #bfs search
		else:
			exp_dfs, graph, steps, expanded = single_dfs(big_list, start) #dfs search

		for i in graph:
			i = ",".join(i)
			str_i = str(i).replace(",", " ")
			print(str_i)

		print("\n")
		print("Path Cost: ", steps, "steps")
		print("Expanded Nodes: ", expanded)


    

if __name__ == "__main__":
    main()
