Maze Solver

Maze Solver preview image

1 collaborator

Default-person Athmane Hassouna (Author)

Tags

(This model has yet to be categorized with any tags)
Visible to everyone | Changeable by the author
Model was written in NetLogo 3D 6.0 • Viewed 186 times • Downloaded 13 times • Run 0 times
Download the 'Maze Solver' modelDownload this modelEmbed this model

Do you have questions or comments about this model? Ask them here! (You'll first need to log in.)


## WHAT IS IT?

(a general understanding of what the model is trying to show or explain)

## HOW IT WORKS

(what rules the agents use to create the overall behavior of the model)

## HOW TO USE IT

(how to use the model, including a description of each of the items in the Interface tab)

## THINGS TO NOTICE

(suggested things for the user to notice while running the model)

## THINGS TO TRY

(suggested things for the user to try to do (move sliders, switches, etc.) with the model)

## EXTENDING THE MODEL

(suggested things to add or change in the Code tab to make the model more complicated, detailed, accurate, etc.)

## NETLOGO FEATURES

(interesting or unusual features of NetLogo that the model uses, particularly in the Code tab; or where workarounds were needed for missing features)

## RELATED MODELS

(models in the NetLogo Models Library and elsewhere which are of related interest)

## CREDITS AND REFERENCES

(a reference to the model's URL on the web if it has one, as well as any other necessary credits, citations, and links)

Comments and Questions

Please start the discussion about this model! (You'll first need to log in.)

Click to Run Model

patches-own [visited visited2 pos dis]

globals [stack stack2 x c k t]

breed [robotLHSs robotLHS]
breed [robotASs robotAS]
breed [robotRMs robotRM]

to setup
  ca
  reset-ticks
  set stack []
  set stack2 []
end 

to make-maze

ask patches with [pzcor = 0] [
    set pcolor 0
    set visited false
    set pos ""
    ]
ask patches with [pzcor = -1] [
    set pcolor 92
    ]
  set x min-pxcor
  while [x <= max-pxcor][ask patches with [pxcor = x and pzcor = 0][set pcolor grey
      if (pxcor != min-pxcor and pxcor != max-pxcor and pycor != min-pycor and pycor != max-pycor)[set pos "wall"]]
  ask patches with [pycor = x and pzcor = 0][set pcolor grey
    if (pxcor != min-pxcor and pxcor != max-pxcor and pycor != min-pycor and pycor != max-pycor)[set pos "wall"]]
  set x (x + 2)]
  set x 0
  set c 0
  set stack (lput x stack)

  ask patches with [pxcor = max-pxcor - 1 and pycor = max-pycor - 1 and pzcor = 0] [
    set pos ""
    set pcolor green
    set visited false
    set dis (abs (max-pxcor - 1) - pxcor) + (abs (max-pycor - 1) - pycor)
    ]
  ask patches with [pxcor = min-pxcor + 1 and pycor = min-pycor + 1 and pzcor = 0] [
    set pos 0
    set pcolor 17
    set visited false
    set dis ""
    ]

  while[not(empty? stack)][
  ask patches with [pos = x and pzcor = 0][
    set k (one-of (neighbors6 with [visited = false and pos = "wall" and pzcor = 0] ))
    ifelse(k != nobody)[
    ask k[
      set t (one-of (neighbors6 with [(pcolor = 0  or pcolor = green) and visited = false]))
      ifelse (t != nobody)[
      set pcolor 0
      set visited true
      set pos ""
      set c (c + 1)
      set stack (lput c stack)
      ask t[
        set visited true
        set pos c
        ]
      set x (last stack)
      ][
        set visited true
        set x (last stack)
        set stack (but-last stack)]
      ]
    ][
      set visited true
      set x (last stack)
      set stack (but-last stack)
      ]
    ]

  ]
  ask patches with [pcolor = 0 or pcolor = green][
    set visited false
    set dis sqrt (((max-pxcor - 1) - pxcor) * ((max-pxcor - 1) - pxcor) + ((max-pycor - 1) - pycor) * ((max-pycor - 1) - pycor))
  ]
end 

to create-robot
  if(Algorithms = "Random mouse algorithm")[
    create-robotRMs 1 [
    set xcor min-pxcor + 1
    set ycor min-pycor + 1
    set zcor 0
    set heading 0
    set color sky
    set shape "circle"
    pd]
  ]
  if(Algorithms = "Left Hand Side algorithm")[
    create-robotLHSs 1 [
    set xcor min-pxcor + 1
    set ycor min-pycor + 1
    set zcor 0
    set heading 0
    set color yellow
    set shape "circle"
    pd]
  ]
  if(Algorithms = "A* algorithm")[
    create-robotASs 1 [
    set xcor min-pxcor + 1
    set ycor min-pycor + 1
    set zcor 0
    set heading 0
    set color blue
    set shape "circle"
    pd]
  ]
end 

to solve-maze
  if (any? robotLHSs)[
    LHS
  ]
  if (any? robotASs)[
    AStar
  ]
  if (any? robotRMs)[
    RM-alg
  ]
end 

to LHS
  reset-ticks

  let mes ""
  while[mes != "maze solved !"][
    tick
  ask robotLHSs [
    set c ([pcolor] of patch-here)
    ifelse (c = green)[
      set mes "maze solved !"
      die
      ][
      set k ([pcolor] of patch-ahead 1)
      set t ([pcolor] of patch-left-and-ahead 90 1)
      ifelse(t != grey)[
        set heading (heading - 90)
        fd 1
        ][
        ifelse(k != grey)[
          fd 1
          ][
          set heading (heading + 90)
          ]
        ]
      ]
  ]
  ]
  show "Left Hand Side Robot :"
  show ticks
  show mes
end 

to AStar
  reset-ticks

  while[t != "maze solved !"][
    tick
    ask robotASs[
    set c ([pcolor] of patch-here)
    ifelse (c = green)[
      set t "maze solved !"
      die
    ][
      set stack ([dis] of neighbors6 with[(pcolor = 0 or pcolor = green) and visited = false and pzcor = 0])
      ifelse (not empty? stack)[
        set x (min stack)
        ask patch-here [set stack2 (lput dis stack2)]
        face one-of (neighbors6 with [(pcolor = 0 or pcolor = green) and dis = x and visited = false and pzcor = 0])
        fd 1
        ask patch-here[
          set visited true
        ]
      ]
      [
        set k last stack2
        ask patch-here [set visited2 true]
        face one-of (neighbors6 with [dis = k and pcolor = 0 and visited2 = 0 and pzcor = 0])
        fd 1
        set stack2 (but-last stack2)
      ]
    ]
  ]
  ]

  show "A* Robot :"
  show ticks
  show t
  set t ""
end 

to RM-alg
  reset-ticks

  while[t != "maze solved !"][
    tick
    ask robotRMs[
    set c ([pcolor] of patch-here)
    ifelse (c = green)[
      set t "maze solved !"
      die
    ][
      set k one-of neighbors6 with[(pcolor = 0 or pcolor = green) and visited = false and pzcor = 0]
      ifelse(k != nobody)[
        face k
        fd 1
        ask k [set visited true]
      ][
        ask patch-here [set visited2 1]
        face one-of neighbors6 with[(pcolor = 0 or pcolor = green) and visited2 = 0 and pzcor = 0]
        fd 1
      ]
    ]
  ]
  ]

  show "RM Robot :"
  show ticks
  show t
  set t ""
end 

to reset
  ask patches with [pcolor = 0 or pcolor = green][
    set visited false
    set visited2 0
  ]
end 

There is only one version of this model, created almost 2 years ago by Athmane Hassouna.

Attached files

File Type Description Last updated
Maze Solver.png preview Preview for 'Maze Solver' almost 2 years ago, by Athmane Hassouna Download

This model does not have any ancestors.

This model does not have any descendants.