Skip to main content
Fixed starting with a room with only one door.
Source Link

I'm not too clear on your requirements, but if the following is allowed, it may be a simple to follow algorithm. Probably a bug in there, especially since it uses a recursive function. Also, it checks if a door leads to the room you came from, but I don't know how a three room circular path would handle, it may just keep looping forever in those three rooms. You may have to add a history to ensure no room is checked twice. But by reading your description that may not be allowed.

solved = FALSE

SearchRoom(rooms[0],NULL rooms[0])    // Start at room 1 (or any room)
IF solved THEN
  // solvable
ELSE
  // unsolvable
ENDIF
END

// Recursive function, should run until it searches every room or finds the exit
FUNCTION SearchRoom: curRoom, prevRoom
  // Is this room the 'exit' room
  IF curRoom.IsExit() THEN
    solved = TRUE
    RETURN
  ENDIF

  // Deadend?  (skip starting room)
  IF (curRoom.id <> prevRoom.id) AND (curRoom.doors <= 1) THEN RETURN

  // Search each room the current room leads to
  FOREACH door IN curRoom
    // Skip the room we just came from
    IF door.id <> prevRoom.id THEN
      SearchRoom(door, curRoom)
    ENDIF
    IF solved THEN EXIT LOOP
  NEXT

  RETURN
ENDFUNCTION

[Edit] Added 'id' to previous check, and updated to make more object oriented.

I'm not too clear on your requirements, but if the following is allowed, it may be a simple to follow algorithm. Probably a bug in there, especially since it uses a recursive function. Also, it checks if a door leads to the room you came from, but I don't know how a three room circular path would handle, it may just keep looping forever in those three rooms. You may have to add a history to ensure no room is checked twice. But by reading your description that may not be allowed.

solved = FALSE

SearchRoom(rooms[0],NULL)    // Start at room 1 (or any room)
IF solved THEN
  // solvable
ELSE
  // unsolvable
ENDIF
END

// Recursive function, should run until it searches every room or finds the exit
FUNCTION SearchRoom: curRoom, prevRoom
  // Is this room the 'exit' room
  IF curRoom.IsExit() THEN
    solved = TRUE
    RETURN
  ENDIF

  // Deadend?
  IF curRoom.doors <= 1 THEN RETURN

  // Search each room the current room leads to
  FOREACH door IN curRoom
    // Skip the room we just came from
    IF door.id <> prevRoom.id THEN
      SearchRoom(door, curRoom)
    ENDIF
    IF solved THEN EXIT LOOP
  NEXT

  RETURN
ENDFUNCTION

[Edit] Added 'id' to previous check, and updated to make more object oriented.

I'm not too clear on your requirements, but if the following is allowed, it may be a simple to follow algorithm. Probably a bug in there, especially since it uses a recursive function. Also, it checks if a door leads to the room you came from, but I don't know how a three room circular path would handle, it may just keep looping forever in those three rooms. You may have to add a history to ensure no room is checked twice. But by reading your description that may not be allowed.

solved = FALSE

SearchRoom(rooms[0], rooms[0])    // Start at room 1 (or any room)
IF solved THEN
  // solvable
ELSE
  // unsolvable
ENDIF
END

// Recursive function, should run until it searches every room or finds the exit
FUNCTION SearchRoom: curRoom, prevRoom
  // Is this room the 'exit' room
  IF curRoom.IsExit() THEN
    solved = TRUE
    RETURN
  ENDIF

  // Deadend?  (skip starting room)
  IF (curRoom.id <> prevRoom.id) AND (curRoom.doors <= 1) THEN RETURN

  // Search each room the current room leads to
  FOREACH door IN curRoom
    // Skip the room we just came from
    IF door.id <> prevRoom.id THEN
      SearchRoom(door, curRoom)
    ENDIF
    IF solved THEN EXIT LOOP
  NEXT

  RETURN
ENDFUNCTION

[Edit] Added 'id' to previous check, and updated to make more object oriented.

Post Merged (destination) from gamedev.stackexchange.com/questions/15531/…
edited body
Source Link

I'm not too clear on your requirements, but if the following is allowed, it may be a simple to follow algorithm. Probably a bug in there, especially since it uses a recursive function. Also, it checks if a door leads to the room you came from, but I don't know how a three room circular path would handle, it may just keep looping forever in those three rooms. You may have to add a history to ensure no room is checked twice. But by reading your description that may not be allowed.

solved = FALSE

SearchRoom(rooms[0],NULL)    // Start at room 1 (or any room)
IF solved THEN
  // solvable
ELSE
  // unsolvable
ENDIF
END

// Recursive function, should run until it searches every room or finds the exit
FUNCTION SearchRoom: curRoom, prevRoom
  // Is this room the 'exit' room
  IF curRoom.IsExit() THEN
    solved = TRUE
    RETURN
  ENDIF

  // Deadend?
  IF curRoom.doors <= 1 THEN RETURN

  // Search each room the current room leads to
  FOREACH roomdoor IN curRoom
    // Skip the room we just came from
    IF roomdoor.id <> prevRoom.id THEN
      SearchRoom(roomdoor, curRoom)
    ENDIF
    IF solved THEN EXIT LOOP
  NEXT

  RETURN
ENDFUNCTION

[Edit] Added 'id' to previous check, and updated to make more object oriented.

I'm not too clear on your requirements, but if the following is allowed, it may be a simple to follow algorithm. Probably a bug in there, especially since it uses a recursive function. Also, it checks if a door leads to the room you came from, but I don't know how a three room circular path would handle, it may just keep looping forever in those three rooms. You may have to add a history to ensure no room is checked twice. But by reading your description that may not be allowed.

solved = FALSE

SearchRoom(rooms[0],NULL)    // Start at room 1 (or any room)
IF solved THEN
  // solvable
ELSE
  // unsolvable
ENDIF
END

// Recursive function, should run until it searches every room or finds the exit
FUNCTION SearchRoom: curRoom, prevRoom
  // Is this room the 'exit' room
  IF curRoom.IsExit() THEN
    solved = TRUE
    RETURN
  ENDIF

  // Deadend?
  IF curRoom.doors <= 1 THEN RETURN

  // Search each room the current room leads to
  FOREACH room IN curRoom
    // Skip the room we just came from
    IF room.id <> prevRoom.id THEN
      SearchRoom(room, curRoom)
    ENDIF
    IF solved THEN EXIT LOOP
  NEXT

  RETURN
ENDFUNCTION

[Edit] Added 'id' to previous check, and updated to make more object oriented.

I'm not too clear on your requirements, but if the following is allowed, it may be a simple to follow algorithm. Probably a bug in there, especially since it uses a recursive function. Also, it checks if a door leads to the room you came from, but I don't know how a three room circular path would handle, it may just keep looping forever in those three rooms. You may have to add a history to ensure no room is checked twice. But by reading your description that may not be allowed.

solved = FALSE

SearchRoom(rooms[0],NULL)    // Start at room 1 (or any room)
IF solved THEN
  // solvable
ELSE
  // unsolvable
ENDIF
END

// Recursive function, should run until it searches every room or finds the exit
FUNCTION SearchRoom: curRoom, prevRoom
  // Is this room the 'exit' room
  IF curRoom.IsExit() THEN
    solved = TRUE
    RETURN
  ENDIF

  // Deadend?
  IF curRoom.doors <= 1 THEN RETURN

  // Search each room the current room leads to
  FOREACH door IN curRoom
    // Skip the room we just came from
    IF door.id <> prevRoom.id THEN
      SearchRoom(door, curRoom)
    ENDIF
    IF solved THEN EXIT LOOP
  NEXT

  RETURN
ENDFUNCTION

[Edit] Added 'id' to previous check, and updated to make more object oriented.

Added 'sovled' check in FOREACH loop
Source Link

I'm not too clear on your requirements, but if the following is allowed, it may be a simple to follow algorithm. Probably a bug in there, especially since it uses a recursive function. Also, it checks if a door leads to the room you came from, but I don't know how a three room circular path would handle, it may just keep looping forever in those three rooms. You may have to add a history to ensure no room is checked twice. But by reading your description that may not be allowed.

solved = FALSE

SearchRoom(rooms[0],NULL)    // Start at room 1 (or any room)
IF solved THEN
  // solvable
ELSE
  // unsolvable
ENDIF
END

// Recursive function, should run until it searches every room or finds the exit
FUNCTION SearchRoom: curRoom, prevRoom
  // Is this room the 'exit' room
  IF curRoom.IsExit() THEN
    solved = TRUE
    RETURN
  ENDIF

  // Deadend?
  IF curRoom.doors <= 1 THEN RETURN

  // Search each room the current room leads to
  FOREACH room IN curRoom
    // Skip the room we just came from
    IF room.id <> prevRoom.id THEN
      SearchRoom(room, curRoom)
    ENDIF
    IF solved THEN EXIT LOOP
  NEXT

  RETURN
ENDFUNCTION

[Edit] Added 'id' to previous check, and updated to make more object oriented.

I'm not too clear on your requirements, but if the following is allowed, it may be a simple to follow algorithm. Probably a bug in there, especially since it uses a recursive function. Also, it checks if a door leads to the room you came from, but I don't know how a three room circular path would handle, it may just keep looping forever in those three rooms. You may have to add a history to ensure no room is checked twice. But by reading your description that may not be allowed.

solved = FALSE

SearchRoom(rooms[0],NULL)    // Start at room 1 (or any room)
IF solved THEN
  // solvable
ELSE
  // unsolvable
ENDIF
END

// Recursive function, should run until it searches every room or finds the exit
FUNCTION SearchRoom: curRoom, prevRoom
  // Is this room the 'exit' room
  IF curRoom.IsExit() THEN
    solved = TRUE
    RETURN
  ENDIF

  // Deadend?
  IF curRoom.doors <= 1 THEN RETURN

  // Search each room the current room leads to
  FOREACH room IN curRoom
    // Skip the room we just came from
    IF room.id <> prevRoom.id THEN
      SearchRoom(room, curRoom)
    ENDIF
  NEXT

  RETURN
ENDFUNCTION

[Edit] Added 'id' to previous check, and updated to make more object oriented.

I'm not too clear on your requirements, but if the following is allowed, it may be a simple to follow algorithm. Probably a bug in there, especially since it uses a recursive function. Also, it checks if a door leads to the room you came from, but I don't know how a three room circular path would handle, it may just keep looping forever in those three rooms. You may have to add a history to ensure no room is checked twice. But by reading your description that may not be allowed.

solved = FALSE

SearchRoom(rooms[0],NULL)    // Start at room 1 (or any room)
IF solved THEN
  // solvable
ELSE
  // unsolvable
ENDIF
END

// Recursive function, should run until it searches every room or finds the exit
FUNCTION SearchRoom: curRoom, prevRoom
  // Is this room the 'exit' room
  IF curRoom.IsExit() THEN
    solved = TRUE
    RETURN
  ENDIF

  // Deadend?
  IF curRoom.doors <= 1 THEN RETURN

  // Search each room the current room leads to
  FOREACH room IN curRoom
    // Skip the room we just came from
    IF room.id <> prevRoom.id THEN
      SearchRoom(room, curRoom)
    ENDIF
    IF solved THEN EXIT LOOP
  NEXT

  RETURN
ENDFUNCTION

[Edit] Added 'id' to previous check, and updated to make more object oriented.

added 97 characters in body
Source Link
Loading
Source Link
Loading