Skip to content

Commit 97ad73c

Browse files
committed
(trip)(vehicle)(simulator) compute score, display failed trips
1 parent 5776541 commit 97ad73c

File tree

4 files changed

+34
-8
lines changed

4 files changed

+34
-8
lines changed

ghashcode/trip.go

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package ghashcode
22

33
import (
44
"image/color"
5+
"math"
56

67
"golang.org/x/image/colornames"
78

@@ -18,13 +19,17 @@ type Trip struct {
1819
Start Coordinates
1920
End Coordinates
2021

22+
Distance int
23+
Bonus int
24+
2125
EarliestStart int32
2226
LatestFinish int32
2327

2428
Color color.RGBA
2529

2630
Taken bool
2731
InProgress bool
32+
Failed bool
2833

2934
// precomputed values
3035
GraphicLine *imdraw.IMDraw
@@ -85,12 +90,27 @@ func (t *Trip) SomeoneIsOnIt() {
8590
t.Taken = true
8691
}
8792

88-
func (t *Trip) StartTrip() {
93+
func (t *Trip) StartTrip(step int) {
94+
if step == int(t.EarliestStart) {
95+
t.Bonus += 2
96+
}
8997
t.Color = colornames.Cyan
9098
}
9199

92-
func (t *Trip) Finish() {
93-
t.Color = colornames.Green
100+
func (t *Trip) Finish(step int32) int {
101+
failed := false
102+
if step > t.LatestFinish {
103+
failed = true
104+
}
105+
t.Failed = failed
106+
107+
if !failed {
108+
t.Color = colornames.Green
109+
return t.Distance + t.Bonus
110+
} else {
111+
t.Color = colornames.Red
112+
return 0
113+
}
94114
}
95115

96116
func (t *Trip) WarnEarly() {
@@ -110,9 +130,12 @@ func NewTrip(id int, a, b, x, y, s, f int32) *Trip {
110130
// default values
111131
trip.InProgress = false
112132
trip.Taken = false
133+
trip.Failed = false
113134
trip.Color = config.Config.UI.TripDefaultColor
114135

115136
// precomputed values
137+
trip.Distance = int(math.Abs(float64(a-x)) + math.Abs(float64(b-y)))
138+
trip.Bonus = 0
116139
imd := imdraw.New(nil)
117140

118141
imd.Color = trip.Color

ghashcode/vehicle.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func (v *Vehicle) SetPosition(x, y int32) {
3939
v.CurrentPosition.Y = y
4040
}
4141

42-
func (v *Vehicle) Drive(allTrips []*Trip, step int) {
42+
func (v *Vehicle) Drive(allTrips []*Trip, step int) (score int) {
4343
// it has done every of his trips
4444
if v.CurrentRide >= len(v.Trips) {
4545
return
@@ -48,22 +48,24 @@ func (v *Vehicle) Drive(allTrips []*Trip, step int) {
4848
tripToGoTo := allTrips[v.Trips[v.CurrentRide]]
4949

5050
if v.OnRide {
51-
tripToGoTo.StartTrip()
51+
tripToGoTo.StartTrip(step)
5252
if int32(step) < tripToGoTo.EarliestStart {
5353
tripToGoTo.WarnEarly()
5454
return
5555
}
5656
v.DriveOnTrip(tripToGoTo.End.X, tripToGoTo.End.Y)
5757
cx, cy := v.GetPosition()
5858
if cx == tripToGoTo.End.X && cy == tripToGoTo.End.Y {
59-
tripToGoTo.Finish()
59+
score += tripToGoTo.Finish(int32(step))
6060
v.NextTrip()
61+
return
6162
}
6263
return
6364
}
6465

6566
tripToGoTo.SomeoneIsOnIt()
6667
v.DriveTo(tripToGoTo.Start.X, tripToGoTo.Start.Y)
68+
return
6769
}
6870

6971
func (v *Vehicle) GetPosition() (int32, int32) {

main.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ func run() {
7777

7878
tick := time.Tick(*frameRate)
7979

80+
score := 0
8081
step := 0
8182
lastStep := 0
8283
imd := imdraw.New(nil)
@@ -121,7 +122,7 @@ func run() {
121122
continue
122123
}
123124
if lastStep != step {
124-
vehicle.Drive(trips, step)
125+
score += vehicle.Drive(trips, step)
125126
}
126127
vehicle.AddToImd(imd)
127128
}
@@ -137,7 +138,7 @@ func run() {
137138

138139
win.SetMatrix(pixel.IM)
139140
ui.DrawStepNumber(win, step)
140-
ui.DrawScore(win, 0)
141+
ui.DrawScore(win, score)
141142
ui.DrawNumberOfVehicles(win, len(vehicles))
142143
ui.DrawNumberOfTrips(win, len(trips))
143144
win.Update()

screenshots/v0.10.png

20 KB
Loading

0 commit comments

Comments
 (0)