Skip to content

Commit 0bd36f3

Browse files
committed
(vehicle) +DriveOnTrip
1 parent 31b7a38 commit 0bd36f3

1 file changed

Lines changed: 28 additions & 0 deletions

File tree

ghashcode/vehicle.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,17 @@ func (v *Vehicle) Drive(allTrips []*Trip) {
5050
}
5151

5252
tripToGoTo := allTrips[v.Trips[v.CurrentRide]]
53+
54+
if v.OnRide {
55+
tripToGoTo.StartTrip()
56+
v.DriveOnTrip(tripToGoTo.End.X, tripToGoTo.End.Y)
57+
cx, cy := v.GetPosition()
58+
if cx == tripToGoTo.End.X && cy == tripToGoTo.End.Y {
59+
tripToGoTo.Finish()
60+
}
61+
return
62+
}
63+
5364
tripToGoTo.SomeoneIsOnIt()
5465
v.DriveTo(tripToGoTo.Start.X, tripToGoTo.Start.Y)
5566
}
@@ -58,6 +69,20 @@ func (v *Vehicle) GetPosition() (int32, int32) {
5869
return v.CurrentPosition.X, v.CurrentPosition.Y
5970
}
6071

72+
func (v *Vehicle) DriveOnTrip(x, y int32) {
73+
cx, cy := v.GetPosition()
74+
if cx > x {
75+
cx--
76+
} else if cx < x {
77+
cx++
78+
} else if cy > y {
79+
cy--
80+
} else if cy < y {
81+
cy++
82+
}
83+
v.SetPosition(cx, cy)
84+
}
85+
6186
func (v *Vehicle) DriveTo(x, y int32) {
6287
cx, cy := v.GetPosition()
6388
dx := math.Abs(float64(cx - x))
@@ -94,4 +119,7 @@ func (v *Vehicle) DriveTo(x, y int32) {
94119
// fmt.Printf("-\n")
95120
// fmt.Printf("moving from (%d, %d) to (%d, %d)\n", cx, cy, nx, ny)
96121
v.SetPosition(nx, ny)
122+
if x == nx && y == ny {
123+
v.OnRide = true
124+
}
97125
}

0 commit comments

Comments
 (0)