├── .gitignore
├── README.md
├── maze-solver.py
└── maze.png
/.gitignore:
--------------------------------------------------------------------------------
1 | .DS_Store
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # gpt3-python-maze-solver
2 | I asked GPT-3 to write a program to solve a maze. It did.
3 |
4 | See it in [the playground](https://beta.openai.com/playground/p/wNiL2BaK2bUVTwMx36HH5lPV).
5 |
6 | # How to use
7 |
8 | 1. Clone the repo.
9 | 2. `cd gpt3-python-maze-solver`
10 | 3. `python maze-solver.py && open solution.png`
11 |
12 | # Instructions provided to GPT-3
13 | ```
14 | Image of maze:
15 | The maze walls will be black
16 | The maze path will be white
17 | The maze start will be a green pixel
18 | The maze end will be a red pixel
19 |
20 | Write a python program that does the following:
21 | Load an image of a maze from the file "maze.png". It will meet the description of the maze above.
22 | Analyze the image to find a solution to the maze. Use the a* pathfinding algorithm.
23 | Produce an image which has a line drawing of the solution to the maze.
24 |
25 | ### Python code:
26 | ```
27 |
28 | # Results
29 | Input
30 |
31 |
32 | Output
33 |
34 |
35 |
36 |
37 | # Bugs
38 | This solution it created had one bug, at the very last line where it attempted to save the image of the solution. I fixed it, and that was the only change.
39 |
40 | It took several tries to get a version that worked, but this one did. It does not employ the A* algorithm, but it does find a solutiuon.
41 |
--------------------------------------------------------------------------------
/maze-solver.py:
--------------------------------------------------------------------------------
1 | import numpy as np
2 | import matplotlib.pyplot as plt
3 | from PIL import Image
4 |
5 | # Define some constants
6 | BLACK = (0, 0, 0)
7 | WHITE = (255, 255, 255)
8 | GREEN = (0, 255, 0)
9 | RED = (255, 0, 0)
10 |
11 | # Load the image
12 | img = Image.open("maze.png")
13 |
14 | # Get the size of the image
15 | size = img.size
16 |
17 | # Get the pixel data from the image
18 | pixels = np.array(img)
19 |
20 | # Find the start and end of the maze
21 | start = None
22 | end = None
23 | for row in range(size[0]):
24 | for col in range(size[1]):
25 | if np.all(pixels[row, col] == GREEN):
26 | start = (row, col)
27 | elif np.all(pixels[row, col] == RED):
28 | end = (row, col)
29 |
30 | # Create a list of all the possible moves
31 | moves = [(0, 1), (0, -1), (1, 0), (-1, 0)]
32 |
33 | # Create a list of all the visited nodes
34 | visited = []
35 |
36 | # Create a list of all the nodes to visit
37 | to_visit = []
38 |
39 | # Add the starting node to the list of nodes to visit
40 | to_visit.append(start)
41 |
42 | # Create a dictionary of the node's parents
43 | parents = {}
44 |
45 | # Loop until we find the end
46 | while len(to_visit) > 0:
47 | # Get the node with the lowest cost
48 | current = to_visit[0]
49 | current_cost = float("inf")
50 | for node in to_visit:
51 | if node[0] + node[1] < current_cost:
52 | current = node
53 | current_cost = node[0] + node[1]
54 |
55 | # If the current node is the end, we're done
56 | if current == end:
57 | break
58 |
59 | # Remove the current node from the list of nodes to visit
60 | to_visit.remove(current)
61 |
62 | # Add the current node to the list of visited nodes
63 | visited.append(current)
64 |
65 | # Loop through the moves
66 | for move in moves:
67 | # Get the new row and column
68 | row = current[0] + move[0]
69 | col = current[1] + move[1]
70 |
71 | # Make sure the move is valid
72 | if row < 0 or row >= size[0] or col < 0 or col >= size[1]:
73 | continue
74 |
75 | # Make sure we haven't visited the node already
76 | if (row, col) in visited:
77 | continue
78 |
79 | # Make sure the node isn't a wall
80 | if np.all(pixels[row, col] == BLACK):
81 | continue
82 |
83 | # Add the node to the list of nodes to visit
84 | to_visit.append((row, col))
85 |
86 | # Add the node to the dictionary of parents
87 | parents[(row, col)] = current
88 |
89 | # Create an empty image
90 | img_solution = Image.new("RGB", size)
91 | pixels_solution = np.array(img_solution)
92 |
93 | # Loop through the parents dictionary
94 | current = end
95 | while current in parents:
96 | # Get the parent
97 | parent = parents[current]
98 |
99 | # Draw a white line on the solution image
100 | pixels_solution[current[0], current[1]] = WHITE
101 | pixels_solution[parent[0], parent[1]] = WHITE
102 |
103 | # Set the current node to the parent
104 | current = parent
105 |
106 | # Save the solution image
107 |
108 | # NOTE: THIS LINE WAS CREATED BY GPT BUT DOES NOT WORK
109 | # img_solution.save("solution.png")
110 |
111 | # The following line does work
112 | Image.fromarray(pixels_solution).save('solution.png')
--------------------------------------------------------------------------------
/maze.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/theseamusjames/gpt3-python-maze-solver/9bb02e55c1c0c752f24d390f4bef8d8a2c7255ae/maze.png
--------------------------------------------------------------------------------