-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
66 lines (56 loc) · 1.63 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import commandLineArgs from './utils/argsReader.js'
import fileReader from './utils/fileReader.js'
import algorithm from './algorithm/search.js'
const args = commandLineArgs.readCommandLineArgs()
const map = fileReader.readJson(args.map)
const startId = args.start_room
const toCollect = args.objects_to_collect
let seen = {}
solveMaze()
/**
*
* @param {*} currentRoom
* Continue the search in the maze
*/
function walk ( currentRoom ) {
const neighbours = algorithm.getNeighbours(currentRoom)
if (!seen[currentRoom.id])
seen[currentRoom.id] = []
neighbours.forEach( room => {
if ( !algorithm.alreadyVisited(seen, room, currentRoom.id) )
{
seen[currentRoom.id].push(room)
collect(room)
}
});
}
/**
* @param {Number} roomId
* Retrieve the objects in that room
*/
function collect ( roomId ) {
const currentRoom = map.rooms.filter( room => room.id === roomId )[0]
let collected = []
if ( currentRoom.objects.length > 0 ){
currentRoom.objects.forEach(obj => {
collected.push(obj.name)
const index = toCollect.indexOf(obj.name)
if (index > -1)
toCollect.splice(index, 1)
});
}
else
collected = 'None'
console.log( roomId + "\t" + currentRoom.name + "\t\t" + collected)
if ( toCollect.length > 0)
walk(currentRoom)
else{
console.log("All objects Collected")
process.exit(0)
}
}
function solveMaze () {
console.log("\nID\tRoom\t\tObject Collected")
console.log("----------------------------------------")
collect(startId)
}