def print_introduction():
     Printer.print_border_top()
     Printer.print_content('IT3105 :: Module 3 :: GAC + A* + Nonograms')
     Printer.print_border_middle()
    def parse_files(self):
        # Set to None to avoid "referenced before assigned" complaint
        input_choice_graph = None

        # Get all boards from directory
        graphs = glob.glob('module3/grams/*.txt')

        # Present different graphs to the user
        while True:
            Printer.print_content('Available nonograms:')
            Printer.print_border_middle()

            # Print list of boards
            idx = 0
            for b in graphs:
                Printer.print_content('[' + str(idx) + ']: ' + b, align='left')
                idx += 1
            Printer.print_border_bottom()

            # Get the user input
            input_choice_graph = raw_input('[0-' + str(len(graphs) - 1) + ']: ')
            Printer.print_newline()

            # Validate input
            try:
                input_choice_graph = int(input_choice_graph)

                if input_choice_graph < 0 or input_choice_graph >= len(graphs):
                    raise AssertionError('')
                break
            except (AssertionError, ValueError):
                Printer.print_border_top()
                Printer.print_content('Invalid input, try again')
                Printer.print_border_middle()

        # Parse the file the user chose
        self.parse_file(str(graphs[input_choice_graph]))
    def run(self):
        # Create new instance of GUI
        gui = Gui()

        # Set reference to AStarCSP here
        gui.astar_gac = self.astar_gac

        # Draw the initial drawing
        gui.draw_initial()

        # If OS X, swap to TKInter window
        if platform.system() == 'Darwin':
            os.system('''/usr/bin/osascript -e 'tell app "Finder" to set frontmost of process "Python" to true' ''')

        # Start the GUI
        gui.after(0, gui.task)

        # Start the event mainloop here
        gui.mainloop()

        # Set the terminal to the frontmost process (expects iTerm to be the chosen terminal)
        if platform.system() == 'Darwin':
            os.system('''/usr/bin/osascript -e 'tell app "Finder" to set frontmost of process "iTerm" to true' ''')

        # Pretty print
        Printer.print_border_top()
        Printer.print_content('Counts')
        Printer.print_border_middle()

        # Print the stats
        Printer.print_content('Search nodes generated (A* generated states): ' + str(len(gui.astar_gac.astar.states)), align='left')
        Printer.print_content('Search nodes expanded (A* open closed list): ' + str(len(gui.astar_gac.astar.closed)), align='left')
        Printer.print_content('Solution path length: ' + str(len(gui.astar_gac.astar.goal_path())), align='left')

        # Print closing border
        Printer.print_border_bottom()
    def parse_files(self):
        # Set to None to avoid "referenced before assigned" complaint
        input_choice_graph = None
        input_choice_k = None

        # Get all boards from directory
        graphs = glob.glob('module2/graphs/*.txt')

        # Present different graphs to the user
        while True:
            Printer.print_content('Available graphs: ')
            Printer.print_border_middle()

            # Print list of boards
            idx = 0
            for b in graphs:
                Printer.print_content('[' + str(idx) + ']: ' + b, align='left')
                idx += 1
            Printer.print_border_bottom()

            # Get the user input
            input_choice_graph = raw_input('[0-' + str(len(graphs) - 1) + ']: ')
            Printer.print_newline()

            # Validate input
            try:
                input_choice_graph = int(input_choice_graph)

                if input_choice_graph < 0 or input_choice_graph >= len(graphs):
                    raise AssertionError('')
                break
            except (AssertionError, ValueError):
                Printer.print_border_top()
                Printer.print_content('Invalid input, try again')
                Printer.print_border_middle()

        # Get suggested K value for this graph
        graph_suggested_k = None
        graph_suggested_k_temp = graphs[input_choice_graph].split('.')[0].split('-')[-1]
        if 'k' in graph_suggested_k_temp:
            graph_suggested_k = int(graph_suggested_k_temp.replace('k', ''))

        # Get the K value
        Printer.print_border_top()
        while True:
            if graph_suggested_k is not None:
                Printer.print_content('Set K value for this graph, suggested value is ' + str(graph_suggested_k))
            else:
                Printer.print_content('Set K value for this graph')

            Printer.print_border_bottom()

            # Get the user input
            input_choice_k = raw_input('[4-9]: ')
            Printer.print_newline()

            # Validate input
            try:
                input_choice_k = int(input_choice_k)

                if input_choice_k < 4 or input_choice_k > 9:
                    raise AssertionError('')
                break
            except (AssertionError, ValueError):
                Printer.print_border_top()
                Printer.print_content('Invalid input, try again')
                Printer.print_border_middle()

        # Parse the file the user chose
        self.parse_file(str(graphs[input_choice_graph]), input_choice_k)
 def print_introduction():
     Printer.print_border_top()
     Printer.print_content('IT3105 :: Module 2 :: GAC + A*')
     Printer.print_border_middle()
    def run(self):
        # Set reference to GAC
        Gui.GAC = GAC

        # Create new instance of GUI
        gui = Gui()

        # Set reference to AStarCSP here
        gui.astar_gac = self.astar_gac

        # Draw the initial drawing
        gui.draw_once()

        # If OS X, swap to TKInter window
        if platform.system() == 'Darwin':
            os.system('''/usr/bin/osascript -e 'tell app "Finder" to set frontmost of process "Python" to true' ''')

        # Start the GUI
        gui.after(0, gui.task)

        # Start the event mainloop here
        gui.mainloop()

        # Set the terminal to the frontmost process (expects iTerm to be the chosen terminal)
        if platform.system() == 'Darwin':
            os.system('''/usr/bin/osascript -e 'tell app "Finder" to set frontmost of process "iTerm" to true' ''')

        # Pretty print
        Printer.print_border_top()
        Printer.print_content('Counts')
        Printer.print_border_middle()

        # Get verticies without color
        uncolored = 0
        for var in gui.astar_gac.gac_state.gac.variables:
            if len(var.domain) != 1:
                uncolored += 1

        # Print the stats
        Printer.print_content('Unsatisfied constraints: ' + str(0), align='left') # If we get here it is impossible to have unsatisfied constraints
        Printer.print_content('Vertices without color: ' + str(uncolored), align='left')
        Printer.print_content('Nodes in search tree (A* generated states): ' + str(len(gui.astar_gac.astar.states)), align='left')
        Printer.print_content('Nodes popped and expanded (A* open closed list): ' + str(len(gui.astar_gac.astar.closed)), align='left')
        Printer.print_content('Solution path length: ' + str(len(gui.astar_gac.astar.goal_path())), align='left')

        # Print closing border
        Printer.print_border_bottom()
    def start_gui(self, width, height):
        # Create new instance of GUI
        gui = Gui()

        # Set width and height
        gui.width = width
        gui.height = height

        # Apply the grid as the data source
        gui.set_data_source(self.a_star)

        # Set TKInter to the frontmost process
        if platform.system() == 'Darwin':
            os.system('''/usr/bin/osascript -e 'tell app "Finder" to set frontmost of process "Python" to true' ''')

        # Draw the initial GUI
        gui.draw_initial()

        # Start the GUI
        gui.after(0, gui.task)

        # Start the event mainloop here
        gui.mainloop()

        # Set the terminal to the frontmost process (expects iTerm to be the chosen terminal)
        if platform.system() == 'Darwin':
            os.system('''/usr/bin/osascript -e 'tell app "Finder" to set frontmost of process "iTerm" to true' ''')

        # Check if solution for all boards were found
        if gui.finished:
            # Pretty print
            Printer.print_border_top()
            Printer.print_content('Comparison')

            # Loop all the data
            for data in gui.data:
                # Pretty print
                Printer.print_border_middle()

                # Print the name of the behavior
                Printer.print_content(data.behavior.NAME, align='left')
                Printer.print_empty()

                # Print the stats
                Printer.print_content('States generated: ' + str(len(data.closed) + len(data.open)), align='left')
                Printer.print_content('Solution path length: ' + str(len(data.goal_path())),
                                      align='left')

            # Print closing border
            Printer.print_border_bottom()