Python Forum
Dijkstra algorithm helping find new space
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Dijkstra algorithm helping find new space
#1
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...
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 :)
Reply


Messages In This Thread
Dijkstra algorithm helping find new space - by 1bumcheek - Jul-26-2023, 09:59 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  from global space to local space Skaperen 4 2,463 Sep-08-2020, 04:59 PM
Last Post: Skaperen
  helping PyInstaller To Find files Harshil 0 1,568 Aug-30-2020, 10:16 AM
Last Post: Harshil
  FAST algorithm of checkSamecircularList(x, y) without L CircularList ref but find ref lsepolis123 0 1,624 Aug-14-2019, 07:51 AM
Last Post: lsepolis123
  Dijkstra code - trying to simplify it grandpapa10 1 2,273 Jan-23-2019, 12:43 PM
Last Post: Larz60+
Question Helping understand classes Miraclefruit 10 6,611 Nov-27-2017, 01:58 PM
Last Post: Windspar

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020