viewing paste bfsRoom | Smalltalk

Posted on the
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
!MUDMobile methodsFor:'brain'!
 
bfsRoom: aRoom
        " Breadth First Search for a Room - resulting in an MUDExit object or nil "
        | visited queued where togo |
        where := self location.
        where = aRoom ifTrue: [ ^ nil ].
        visited := Set new.
        queued := SharedQueue new.
        visited add: where.
        where exits do: [ :e | e direction = aRoom ifTrue: [ ^ e ].
                (visited includes: e direction) ifFalse: [ queued nextPut: e. queued nextPut: e ] ].
        [ queued isEmpty not ] whileTrue: [
                visited add: where.
                where := queued next direction.
                togo := queued next.
                where exits do: [ :e | e direction = aRoom ifTrue: [ ^ togo ].
                        (visited includes: e direction) ifFalse: [ queued nextPut: e. queued nextPut: togo ] ] ].
        ^ nil
 
! !
Viewed 235 times, submitted by unknown.