4444 ["#" , "#" , "#" , "#" , "#" , "#" , "#" , "#" , "#" , "#" , "#" , "#" , "#" , "#" , "#" , "#" , "#" , "#" , "#" , "#" ]
4545]
4646
47- def print_maze (maze : list [list [str ]], stdscr : curses .window , visited : set [tuple [int , int ]], path : list [tuple [int , int ]] = []):
47+ def print_maze (maze : list [list [str ]], stdscr : curses .window , visited : set [tuple [int , int ]], path : list [tuple [int , int ]] = []) -> None :
4848 blue = curses .color_pair (1 )
4949 red = curses .color_pair (2 )
5050 green = curses .color_pair (3 )
@@ -59,31 +59,69 @@ def print_maze(maze: list[list[str]], stdscr: curses.window, visited: set[tuple[
5959 stdscr .addstr (row , column * 2 , j , blue )
6060
6161
62- def find (maze : list [list [str ]], start : str ) -> tuple [int , int ] | None : #to check and return starting position in maze
62+ def find (maze : list [list [str ]], start : str ) -> tuple [int , int ] | None :
63+ """
64+ Find the first occurrence of a given element (like 'O' for start or 'X' for target) in the maze.
65+
66+ Examples:
67+ >>> from project import find, maze
68+ >>> find(maze, "O")
69+ (0, 1)
70+ >>> find(maze, "X")
71+ (18, 18)
72+ >>> find(maze, "Z") is None
73+ True
74+ """
6375 for row , i in enumerate (maze ):
6476 for column , j in enumerate (i ):
6577 if j == start :
66- return (
67- row ,
68- column ,
69- ) # return tuple of row, column location of element in the maze
78+ return (row , column )
7079 return None
7180
81+
7282def find_neighbours (maze : list [list [str ]], row : int , col : int ) -> list [tuple [int , int ]]:
83+ """
84+ Find all valid (up, down, left, right) neighbour positions for a given cell.
85+
86+ Examples:
87+ >>> from project import find_neighbours, maze
88+ >>> set(find_neighbours(maze, 4, 4)) == {(3, 4), (5, 4), (4, 3), (4, 5)}
89+ True
90+ >>> set(find_neighbours(maze, 0, 0)) == {(0, 1), (1, 0)}
91+ True
92+ >>> set(find_neighbours(maze, 0, 8)) == {(0, 7), (1, 8)}
93+ True
94+ """
7395 neighbours = []
74-
75- if row > 0 : # for UP
96+ if row > 0 :
7697 neighbours .append ((row - 1 , col ))
77- if row + 1 < len (maze ): # for DOWN
98+ if row + 1 < len (maze ):
7899 neighbours .append ((row + 1 , col ))
79- if col + 1 < len (maze [0 ]): # for RIGHT
100+ if col + 1 < len (maze [0 ]):
80101 neighbours .append ((row , col + 1 ))
81- if col > 0 : # for LEFT
102+ if col > 0 :
82103 neighbours .append ((row , col - 1 ))
83104 return neighbours
84105
85106
86- def traverse (maze : list [list [str ]], stdscr : curses .window ) -> list [tuple [int , int ]] | None : #implementing bfs traversal
107+ def traverse (maze : list [list [str ]], stdscr ) -> list [tuple [int , int ]] | None :
108+ """
109+ Run a breadth-first search on the maze and return the path from start 'O' to target 'X'.
110+
111+ Examples:
112+ >>> from project import traverse, maze
113+ >>> from unittest import mock
114+ >>> import curses
115+ >>> stdscr = mock.Mock()
116+ >>> curses.color_pair = mock.Mock(side_effect=lambda x: x)
117+ >>> path = traverse(maze, stdscr)
118+ >>> expected_path = [
119+ ... (0, 1), (1, 1), (2, 1), (3, 1), (4, 1), (5, 1), (6, 1), (7, 1),
120+ ... (7, 2), (7, 3), (7, 4), (7, 5), (7, 6), (7, 7), (8, 7)
121+ ... ]
122+ >>> path == expected_path
123+ True
124+ """
87125 start = "O"
88126 target = "X"
89127 start_pos = find (maze , start )
0 commit comments