title: Avoid visited patch netlogo
body: <p>Sir i am working on a project and time to time i use to post questions here related to that problem. I have created a simulation scenario i which turtles (robots) move around in the space and also each robot keep track of its visited patches. On the movement if <code>patch-ahead 1</code>  is visited patch then i need to turn the robot to 45 degree left and check again for <code>patch-ahead</code> until it checks all 8 neighbors if any one of the 8 is UN-visited then it should move to that patch and continue its exploration. But if all 8 are visited then it should move to patch which is in front of current heading after checking the all 8 neighbors no matter it is visited. 
  Here is the piece of code i am using. </p>
  
  <pre><code>breed [robots robot ]
  robots-own[ state memory ]
  patches-own [ is-obstacle? ]
  
  to setup
  
  __clear-all-and-reset-ticks
  create-robots num [ 
  set memory (list patch-here)
  ]
  draw-obstacles
    ask patches [if pxcor = 0 and pycor = 0  [ set pcolor black ]]
  end   
  
  to draw-obstacles
     ask patches with [ pxcor mod 6 = 0 and pycor mod 6 = 0 ] [set pcolor red set is-obstacle? true]
    ; set pcolor red
    ask patches [ ifelse pcolor = red [ set is-obstacle? true ][ set is-obstacle? false ] ]
    ask patches with [ count neighbors != 8 ] [ set pcolor red set is-obstacle? true ]
  end
  
  
  to make-obstacle
   if mouse-down?
   [ ask-concurrent patches
     [ if ((abs (pxcor - mouse-xcor)) &lt; 1) and ((abs (pycor - mouse-ycor)) &lt; 1)
       [set pcolor red]]
   ]
  end
  to remove-obs
   if mouse-down?
   [ ask-concurrent patches
     [ if ((abs (pxcor - mouse-xcor)) &lt; 1) and ((abs (pycor - mouse-ycor)) &lt; 1)
       [set pcolor black]]
   ]
  end
  
  to go
    ask patches [if ( pcolor = red )[set is-obstacle? true]]
    ask patches [if ( pcolor = black )[set is-obstacle? false]]
    ask-concurrent robots ; wanderers instructions
    [
     rt random-float rate-of-random-turn 
     lt (rate-of-random-turn  / 2)
     set-state
     move-robots
    ]
    tick 
  end
  
  to move-robots ;;turtle proc
   if (not member? state ["disperse" "explore"]) [
      error "Unknown state"
    ]
    if (state = "disperse") [
      disperse
    ]
    if (state = "explore") [
      explore
    ]
  end
  
  to set-state ;;turtle proc
    ifelse (any? other turtles in-radius 1) [
      set state "disperse"
    ] [
      set state "explore"
    ]
  end
  to disperse ;;turtle proc
   avoid-obstacle
   move1
  
   ; move-to one-of patch-set [neighbors] of neighbors
  end
  
  to explore
    move
    ;search-open-room
    avoid-obstacle
    ;move-to one-of neighbors
  end
  
  to move
   fd speed
   set memory lput patch-here memory
   if ( (member? patch-ahead 1 memory) or ([is-obstacle?] of patch-ahead 1 ) )
    [ lt random 45
  
       ]
  
  end
  
  to move1
  fd speed
  end
  
  to avoid-obstacle
    set memory lput patch-here memory
        if ([is-obstacle?] of patch-ahead 1 )
      [
          ifelse [is-obstacle?] of patch-at-heading-and-distance (heading - 5) 1
          [
             ifelse [is-obstacle?] of patch-at-heading-and-distance (heading + 5) 1
              [ 
                  ifelse random 1 = 0
                  [ rt 40 ]
                  [ lt 40 ]
              ]
              [ rt 60 ]
          ]
          [lt 60]
      ]
  
  end
  to search-open-room
    ask robots[
      ifelse ([is-obstacle?] of patches in-cone 2 150 )
        [  rt 45 ] [ move ]
  
      ]
  
  end
  </code></pre>
  
  <p>But in the <code>move</code> procedure i am just able to <code>lt random 45</code>. How to change it according to above mentioned scenario. I tried many with <code>while</code> loop and <code>repeat</code>  statement but code does not seems to be working for me.  </p>