C463/B551/I400 Artificial Intelligence
Dana Vrajitoru

C463/B551/I400 Homework 5

Due date: Wednesday, October 1, 2025.

Ex. 1. Search Algorithms

Download the following Python program implementing a grid application with a target and a player:
Grid.py

The module contains an implementation of a class called Grid that stores a 2D table and displays it. A function for the breadth-first search is implemented as model. 

a. Complete the implementation of the functions key_press and neighbors with the other 3 directions. For both of them, use self.rows() and self.cols() to make sure that the player doesn't go outside the table. Add 3 more functions similar to move_up for the other 3 directions. In the function open_window, add the remaining needed keys in the tuple in the for loop containing the function call to the function bind

b. Implement two functions  depth_first(self) and astar(self). Add corresponding buttons to the interface in the function open_window.

The depth-first search has the following outline:

depth_first_search(origin):
    flag(origin)
    if origin is target: return success
    for child in neighbors(origin):
        if child is not flagged:
            parent(child) = origin
            flag(child)
            if depth_first_search(child): return success
    return failure

You will need the function depth_first(self) to be a wrapper function around a recursive function with this outline, because functions connected to buttons cannot have extra parameters. Use self.player_pos as the parameter origin in the recursive function. 

The A* algorithm is based on the breadth-first search with the following modifications:

Turn in:

Submit the modified file Grid.py to Canvas, Homework 5.