-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalgo.go
65 lines (52 loc) · 1.47 KB
/
algo.go
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
package main
import "fmt"
type AlgorithmName int
const (
Greedy AlgorithmName = iota
)
func (trip *Trip) simulateWithAlgorithm(algoName AlgorithmName) {
destination := trip.destination
hero := &trip.hero
trip.day = 1
for {
same, nextCoordinate := getNextCoordinate(algoName, destination, hero.coordinate)
if same {
return
}
hero.coordinate = nextCoordinate
newTile := &trip.tileMap[nextCoordinate.height][nextCoordinate.width]
hero.hpLoss(newTile.threatLevel)
newTile.setHeroTrace()
trip.day += newTile.passability
if hero.hp == 0 {
newTile.setHeroDeath()
return
}
}
}
func getNextCoordinate(algoName AlgorithmName, destination TileCoordinate, heroC TileCoordinate) (same bool, nextCoordinate TileCoordinate) {
switch algoName {
case Greedy:
return getNextCoordinateGreedy(destination, heroC)
default:
panic(fmt.Sprintln("Invalid algorithm name:", algoName))
}
}
func getNextCoordinateGreedy(destination TileCoordinate, heroC TileCoordinate) (same bool, nextCoordinate TileCoordinate) {
nextCoordinate = heroC
if destination.height == heroC.height {
if destination.width == heroC.width {
return true, nextCoordinate
}
nextCoordinate.width = heroC.width + 1
if destination.width < heroC.width {
nextCoordinate.width = heroC.width - 1
}
return false, nextCoordinate
}
nextCoordinate.height = heroC.height + 1
if destination.height < heroC.height {
nextCoordinate.height = heroC.height - 1
}
return false, nextCoordinate
}