Jul-26-2023, 09:59 PM
Hello people I am kind of new to coding and I am making a robot that moves through a maze that is 5 by 5. I am writing dijkstras algorithm to help me, or at least trying to, I think the problem with my code is the part where it detects a new space. However there is probably more to change.
This is my code so far...
But I want it to check the spaces around it, update what it prints with a 1, then on the next print update it with the number 2 until it reaches the X.
Any help appreciated , thanks in advance :)
This is my code so far...
import time import numpy as np Maze = np.array([ "X", "_", "_", "_", "_", "_", "Z", "Z", "Z", "_", "_", "Z", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "Z", "_", "Y", ]) # Reshape the 1D array into a 2D 7x7 grid Maze = Maze.reshape((5, 5)) def X(): CurrentValue = str(input("CurrentValue")) if CurrentValue: print ("currentCoords" + str(CurrentValue)) else: print("currentCoords") return CurrentValue = "Y" i = 0 # Check for obstacles represented as -1 def check_next_coordinates(X, Y): if 0 <= X < 5 and 0 <= Y < 5: return Maze[X, Y] != -1 return False # Stop when it reaches the Y while CurrentValue != X: currentCoords = np.argwhere(Maze == CurrentValue) # Check if currentCoords array is empty if not currentCoords.any(): break print("Current Coordinates:", currentCoords[-1]) time.sleep(1) # Check for empty space if Maze[tuple(currentCoords[-1])] == -2: Maze[tuple(currentCoords[-1])] = int(CurrentValue) + 1 elif Maze[tuple(currentCoords[-1])] == 0: print("Maze:", Maze) CurrentValue = 0 X = currentCoords[-1][0] Y = currentCoords[-1][1] if currentCoords[-1].size > 1 else currentCoords[-1][0] # Check the left space left_x, left_y = X, Y - 1 if check_next_coordinates(left_x, left_y): if Maze[left_x, left_y] == -2: Maze[left_x, left_y] = CurrentValue + 1 elif Maze[left_x, left_y] == 0: print("Left space is available") CurrentValue = -2 # Check the above space above_X, above_Y = X - 1, Y if check_next_coordinates(above_X, above_Y): if Maze[above_X, above_Y] == -2: Maze[above_X, above_Y] = CurrentValue + 1 elif Maze[above_X, above_Y] == 0: print("Space above is available") CurrentValue = -2 # Check the right space right_X, right_Y = X, Y +1 if check_next_coordinates(right_X, right_Y): if Maze[right_X, right_Y] == -2: Maze[right_X, right_Y] = CurrentValue + 1 elif Maze[right_X, right_Y] == 0: print("Right space is available") CurrentValue = -2 # Adds 1 to the next space i = i + 1 # Print the updated Maze after each move print(Maze) #print("Maze:", Maze)So now the code prints the maze, the coordinates and then waits a second then prints the maze again so on and so on.
But I want it to check the spaces around it, update what it prints with a 1, then on the next print update it with the number 2 until it reaches the X.
Any help appreciated , thanks in advance :)