declare all paths
This commit is contained in:
parent
11ce220347
commit
596a77db59
|
@ -4,7 +4,12 @@
|
|||
##end
|
||||
3 5 2
|
||||
2 5 6
|
||||
1-3
|
||||
4 7 7
|
||||
5 8 9
|
||||
1-2
|
||||
2-3
|
||||
3-2
|
||||
1-3
|
||||
1-4
|
||||
4-5
|
||||
5-3
|
||||
5-2
|
||||
|
|
65
maze/maze.go
65
maze/maze.go
|
@ -23,7 +23,6 @@ type Room struct {
|
|||
|
||||
var collection [][]string
|
||||
|
||||
|
||||
// Load method to load the maze from input
|
||||
func Load(inputStream *bufio.Reader) *Maze {
|
||||
maze := &Maze{}
|
||||
|
@ -77,7 +76,6 @@ func Load(inputStream *bufio.Reader) *Maze {
|
|||
if len(parts) > 0 {
|
||||
maze.endRoom = parts[0]
|
||||
log.Println("End room:", maze.endRoom)
|
||||
// Ensure the end room is loaded as a regular room
|
||||
if !maze.loadRooms(nextLine) {
|
||||
return nil
|
||||
}
|
||||
|
@ -106,7 +104,6 @@ func Load(inputStream *bufio.Reader) *Maze {
|
|||
return maze
|
||||
}
|
||||
|
||||
|
||||
func (maze *Maze) loadRooms(line string) bool {
|
||||
// Parse room data
|
||||
parts := strings.Fields(line)
|
||||
|
@ -168,32 +165,58 @@ func (maze *Maze) mapFarm(line string) bool {
|
|||
}
|
||||
|
||||
// Establish connection between rooms
|
||||
// room1. = append(room1.connections, *room2)
|
||||
// room2.connections = append(room2.connections, *room1)
|
||||
collection = append(collection, []string{room1Name, room2Name})
|
||||
room1.paths = append(room1.paths, room2Name)
|
||||
room2.paths = append(room2.paths, room1Name)
|
||||
return true
|
||||
}
|
||||
|
||||
func (maze *Maze) Solve() {
|
||||
log.Println("Solving the maze...")
|
||||
//l want to declare all paths
|
||||
var path string;
|
||||
var currentcol = collection[0][0]
|
||||
var flage bool
|
||||
|
||||
for !strings.HasPrefix(currentcol, maze.endRoom){
|
||||
|
||||
if strings.HasPrefix(currentcol, maze.startRoom) {
|
||||
path = currentcol
|
||||
flage = true
|
||||
break
|
||||
}
|
||||
|
||||
if flage {
|
||||
var paths []string
|
||||
|
||||
for _, room := range maze.rooms { // Iterate through rooms to find paths
|
||||
if room.name == maze.startRoom {
|
||||
maze.explorePaths(room, maze.endRoom, []string{}, &paths)
|
||||
}
|
||||
}
|
||||
|
||||
log.Println(collection)
|
||||
|
||||
if len(paths) == 0 {
|
||||
log.Println("No paths found from start room to end room.")
|
||||
} else {
|
||||
log.Println("Paths found:")
|
||||
for i, path := range paths {
|
||||
log.Printf("Path %d: %s", i+1, path)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (maze *Maze) explorePaths(currentRoom Room, endRoom string, currentPath []string, paths *[]string) {
|
||||
currentPath = append(currentPath, currentRoom.name) // is the end room
|
||||
|
||||
if currentRoom.name == endRoom { // is the end room
|
||||
*paths = append(*paths, strings.Join(currentPath, " -> "))
|
||||
return
|
||||
}
|
||||
|
||||
// Explore all possible paths from current room
|
||||
for _, nextRoomName := range currentRoom.paths {
|
||||
for _, room := range maze.rooms {
|
||||
if room.name == nextRoomName {
|
||||
// Explore next room if it's not already visited
|
||||
if !contains(currentPath, room.name) {
|
||||
maze.explorePaths(room, endRoom, currentPath, paths)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func contains(path []string, room string) bool {
|
||||
for _, r := range path {
|
||||
if r == room {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue