I am programming a Dungeon generator for litte game.
Dungeons are made up of rooms. A roomhas connectionsfor other rooms.
room.connections = [room_a, room_b]
and
room.number = 1 # unique id
Now I need to select a number by this number.
I did this first with a method recursive_scanthat didnβt work, because the rooms can circle, which throws a StackOverflowError. Therefore, I put an array with the name of the already_scannedroom numbers that were already selected in the method arguments. Then he did not scan all the rooms - by the way, I have no idea why, in my logical unreliability, this should have worked.
Then I tried to put all the rooms in an array, and then iterate over the array in the desired room - but here I get the problem, each room is basically connected to every other room, at least to some other rooms between them; therefore, the array becomes as large as dungeon_size * array_of_rooms.length.
Now I need an explicit pointer - I know that almost every var in ruby ββis a pointer, except Fixnums and Float (and maybe some others). Despite this, the array is getting large, so I need a real pointer.
(I also tried setting up an array of object_ids and loading them via ObectSpace, but unfortunately, because I often have to load rooms, rooms with the required object_id have already been reworked, as explained by the error message.)
This is the recursive scan method:
def room(number)
recursive_scan(@map, number, [])
end
private
def recursive_scan(room, number, scanned)
scanned << room.room_number
if room.room_number == number
room
else
r = nil
room.connections.each do |next_room|
if !scanned.include?(next_room.room_number)
r = recursive_scan(next_room, number, scanned)
end
end
r
end
end