Skip to content

Commit 7ab1e46

Browse files
committed
(all) doc & better code quality
closes #1
1 parent 97ad73c commit 7ab1e46

6 files changed

Lines changed: 158 additions & 122 deletions

File tree

config/config.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,13 @@ type uiConfig struct {
2020
VehicleDefaultColor color.RGBA
2121
}
2222

23+
type simulationConfig struct {
24+
Bonus int
25+
}
26+
2327
type config struct {
24-
UI uiConfig
28+
UI uiConfig
29+
Simulation simulationConfig
2530
}
2631

2732
var Config config
@@ -34,4 +39,6 @@ func init() {
3439
Config.UI.VehicleDefaultColor = colornames.Red
3540
Config.UI.GridColor = colornames.Gray
3641
Config.UI.WindowTitle = "Google Hashcode 2018 - Simulator!"
42+
43+
Config.Simulation.Bonus = 2
3744
}

ghashcode/coordinates.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package ghashcode
22

3+
// Coordinates export a type that look like
4+
// coordinates on a map
35
type Coordinates struct {
46
X, Y int32
57
}

ghashcode/trip.go

Lines changed: 48 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -10,62 +10,72 @@ import (
1010

1111
"github.com/faiface/pixel"
1212
"github.com/faiface/pixel/imdraw"
13-
"github.com/faiface/pixel/pixelgl"
1413
)
1514

15+
// Trip a structure which represents a trip in the simulation
16+
// Quite self-explanatory
1617
type Trip struct {
18+
// unique identifier for each trip, used for debugging
1719
ID int
1820

21+
// start & end coordinates
1922
Start Coordinates
2023
End Coordinates
2124

25+
// computed distance from Start to End
26+
// helps for score computation
2227
Distance int
23-
Bonus int
28+
// we set there the bonus defined in Config if the
29+
// driver starts its trip on time
30+
Bonus int
2431

32+
// A driver cannot start its trip before EarlierStart step
33+
// And if he finish his drive after LatestFinish the trip will
34+
// be marked as failed
2535
EarliestStart int32
2636
LatestFinish int32
2737

38+
// current color of the trip
2839
Color color.RGBA
2940

30-
Taken bool
41+
// when a driver is going towards the trip
42+
Taken bool
43+
// the vehicle is actually doing it
3144
InProgress bool
32-
Failed bool
33-
34-
// precomputed values
35-
GraphicLine *imdraw.IMDraw
36-
}
37-
38-
func (t *Trip) DrawToWindow(win *pixelgl.Window) {
39-
if !t.Taken {
40-
return
41-
}
42-
43-
t.GraphicLine.Color = t.Color
44-
t.GraphicLine.Draw(win)
45+
// the vehicle end the trip too late
46+
Failed bool
4547
}
4648

49+
// AddToImd adds to the imd batch the graphic line of the trip
50+
// We only call .draw once for all the trips
4751
func (t *Trip) AddToImd(imd *imdraw.IMDraw) {
52+
// we only show taken trips for performance
53+
// TODO a way to desactivate this
4854
if !t.Taken {
4955
return
5056
}
5157

58+
// depending of the status of the trip we assign it a color
5259
imd.Color = t.Color
5360

54-
/* start point */
61+
// start point
5562
startX := t.Start.X*config.Config.UI.SquareSize + config.Config.UI.SquareSize
5663
startY := t.Start.Y*config.Config.UI.SquareSize + config.Config.UI.SquareSize
5764
imd.Push(pixel.V(float64(startX), float64(startY)))
58-
/* second point */
65+
// second point
5966
intermediateX := (t.End.X)*config.Config.UI.SquareSize + config.Config.UI.SquareSize
6067
intermediateY := (t.Start.Y)*config.Config.UI.SquareSize + config.Config.UI.SquareSize
6168
imd.Push(pixel.V(float64(intermediateX), float64(intermediateY)))
62-
/* final point */
69+
// third final point
6370
endX := t.End.X*config.Config.UI.SquareSize + config.Config.UI.SquareSize
6471
endY := t.End.Y*config.Config.UI.SquareSize + config.Config.UI.SquareSize
6572
imd.Push(pixel.V(float64(endX), float64(endY)))
6673

74+
// registering the line
6775
imd.Line(2)
6876

77+
// we create the tip of the trip to see where its start is
78+
// looks like this: O---------
6979
imd.Color = t.Color
7080
imd.EndShape = imdraw.RoundEndShape
7181

@@ -75,28 +85,40 @@ func (t *Trip) AddToImd(imd *imdraw.IMDraw) {
7585
imd.Line(10)
7686
}
7787

88+
// SetStart setter for StartCoordinates
7889
func (t *Trip) SetStart(x, y int32) {
7990
t.Start.X = x
8091
t.Start.Y = y
8192
}
8293

94+
// SetEnd setter for StartCoordinates
8395
func (t *Trip) SetEnd(x, y int32) {
8496
t.End.X = x
8597
t.End.Y = y
8698
}
8799

100+
// SomeoneIsOnIt we could store the vehicle id for debugging purposes there
101+
// but atm we only change the trip color and its status
88102
func (t *Trip) SomeoneIsOnIt() {
89103
t.Color = colornames.Beige
90104
t.Taken = true
91105
}
92106

107+
// StartTrip takes the current step as parameter
108+
// if we start it on time we earn the bonus points
109+
// a different color is assigned if we start it on time or not
93110
func (t *Trip) StartTrip(step int) {
94111
if step == int(t.EarliestStart) {
95-
t.Bonus += 2
112+
t.Bonus += config.Config.Simulation.Bonus
113+
t.Color = colornames.Gold
114+
return
96115
}
97116
t.Color = colornames.Cyan
98117
}
99118

119+
// Finish the vehicle finished the trip at step.
120+
// we determine if he failed or not to arrive on time
121+
// a different is assigned if we end it on time or not
100122
func (t *Trip) Finish(step int32) int {
101123
failed := false
102124
if step > t.LatestFinish {
@@ -107,16 +129,19 @@ func (t *Trip) Finish(step int32) int {
107129
if !failed {
108130
t.Color = colornames.Green
109131
return t.Distance + t.Bonus
110-
} else {
111-
t.Color = colornames.Red
112-
return 0
113132
}
133+
t.Color = colornames.Red
134+
return 0
114135
}
115136

137+
// WarnEarly it changes only the trip color to warn graphically
138+
// that a vehicle is too early on a trip
139+
// We can see that way that some drivers are waiting way too long
116140
func (t *Trip) WarnEarly() {
117141
t.Color = colornames.Yellow
118142
}
119143

144+
// NewTrip a constructor for Trip
120145
func NewTrip(id int, a, b, x, y, s, f int32) *Trip {
121146
trip := new(Trip)
122147

@@ -141,7 +166,5 @@ func NewTrip(id int, a, b, x, y, s, f int32) *Trip {
141166
imd.Color = trip.Color
142167
imd.EndShape = imdraw.RoundEndShape
143168

144-
trip.GraphicLine = imd
145-
146169
return trip
147170
}

ghashcode/vehicle.go

Lines changed: 88 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,24 @@ import (
88
"github.com/faiface/pixel/imdraw"
99
)
1010

11+
// Vehicle a structure which represents a vehicle in the simulation
12+
// Quite self-explanatory
1113
type Vehicle struct {
14+
// current position of the vehicle
1215
CurrentPosition Coordinates
1316

14-
CurrentRide int
15-
OnRide bool
16-
Trips []int32
17-
Enabled bool
17+
// Trips an array of trips' indexes
18+
// (and not their ids)
19+
Trips []int32
20+
// index of the current trip in Trips
21+
CurrentTrip int
22+
// if he is currently assigned on a trip
23+
OnRide bool
24+
Enabled bool
1825
}
1926

27+
// AddToImd adds to the imd batch the graphic point of the vehicle
28+
// We only call .draw once for all the vehicles
2029
func (v *Vehicle) AddToImd(imd *imdraw.IMDraw) {
2130
imd.Color = config.Config.UI.VehicleDefaultColor
2231
imd.EndShape = imdraw.RoundEndShape
@@ -34,18 +43,13 @@ func (v *Vehicle) AddToImd(imd *imdraw.IMDraw) {
3443
imd.Line(config.Config.UI.VehicleSize)
3544
}
3645

37-
func (v *Vehicle) SetPosition(x, y int32) {
38-
v.CurrentPosition.X = x
39-
v.CurrentPosition.Y = y
40-
}
41-
4246
func (v *Vehicle) Drive(allTrips []*Trip, step int) (score int) {
4347
// it has done every of his trips
44-
if v.CurrentRide >= len(v.Trips) {
48+
if v.CurrentTrip >= len(v.Trips) {
4549
return
4650
}
4751

48-
tripToGoTo := allTrips[v.Trips[v.CurrentRide]]
52+
tripToGoTo := allTrips[v.Trips[v.CurrentTrip]]
4953

5054
if v.OnRide {
5155
tripToGoTo.StartTrip(step)
@@ -54,8 +58,8 @@ func (v *Vehicle) Drive(allTrips []*Trip, step int) (score int) {
5458
return
5559
}
5660
v.DriveOnTrip(tripToGoTo.End.X, tripToGoTo.End.Y)
57-
cx, cy := v.GetPosition()
58-
if cx == tripToGoTo.End.X && cy == tripToGoTo.End.Y {
61+
currentX, currentY := v.GetPosition()
62+
if currentX == tripToGoTo.End.X && currentY == tripToGoTo.End.Y {
5963
score += tripToGoTo.Finish(int32(step))
6064
v.NextTrip()
6165
return
@@ -68,71 +72,103 @@ func (v *Vehicle) Drive(allTrips []*Trip, step int) (score int) {
6872
return
6973
}
7074

71-
func (v *Vehicle) GetPosition() (int32, int32) {
72-
return v.CurrentPosition.X, v.CurrentPosition.Y
73-
}
74-
75+
// NextTrip move forward next trip or disable the vehicle
76+
// if there isnt any trips left
7577
func (v *Vehicle) NextTrip() {
76-
// move forward next trip
77-
v.CurrentRide++
78+
v.CurrentTrip++
7879
v.OnRide = false
79-
if v.CurrentRide >= len(v.Trips) {
80-
// no trip ? hide the car
80+
81+
// no trip ? hide the car
82+
if v.CurrentTrip >= len(v.Trips) {
8183
v.Enabled = false
8284
}
8385
}
8486

85-
func (v *Vehicle) DriveOnTrip(x, y int32) {
86-
cx, cy := v.GetPosition()
87-
if cx > x {
88-
cx--
89-
} else if cx < x {
90-
cx++
91-
} else if cy > y {
92-
cy--
93-
} else if cy < y {
94-
cy++
87+
// DriveOnTrip it will make the vehicle
88+
// follow the line drawn on the screen by the trip
89+
func (v *Vehicle) DriveOnTrip(destinationX, destinationY int32) {
90+
currentX, currentY := v.GetPosition()
91+
if currentX > destinationX {
92+
currentX--
93+
} else if currentX < destinationX {
94+
currentX++
95+
} else if currentY > destinationY {
96+
currentY--
97+
} else if currentY < destinationY {
98+
currentY++
9599
}
96-
v.SetPosition(cx, cy)
100+
101+
// update vehicle position
102+
v.SetPosition(currentX, currentY)
97103
}
98104

99-
func (v *Vehicle) DriveTo(x, y int32) {
100-
cx, cy := v.GetPosition()
101-
dx := math.Abs(float64(cx - x))
102-
dy := math.Abs(float64(cy - y))
105+
// DriveTo destinationX, destinationY coordinates
106+
// It will make the vehicle move in a straight line to a point
107+
func (v *Vehicle) DriveTo(destinationX, destinationY int32) {
108+
// retrieve current position
109+
currentX, currentY := v.GetPosition()
103110

104-
// fmt.Printf("goto %d %d\n", x, y)
105-
nx := cx
106-
ny := cy
111+
// calculate the absolute difference between the positions
112+
dx := math.Abs(float64(currentX - destinationX))
113+
dy := math.Abs(float64(currentY - destinationY))
114+
115+
// newX and newY
116+
nx := currentX
117+
ny := currentY
107118
if dx < dy {
108-
if cy < y {
119+
if currentY < destinationY {
109120
ny++
110-
} else if cy > y {
121+
} else if currentY > destinationY {
111122
ny--
112123
}
113124
} else if dx > dy {
114-
if cx < x {
125+
if currentX < destinationX {
115126
nx++
116-
} else if cx > x {
127+
} else if currentX > destinationX {
117128
nx--
118129
}
119130
} else if dx == dy {
120-
if cx < x {
131+
if currentX < destinationX {
121132
nx++
122-
} else if cx > x {
133+
} else if currentX > destinationX {
123134
nx--
124-
} else if cy < y {
135+
} else if currentY < destinationY {
125136
ny++
126-
} else if cy > y {
137+
} else if currentY > destinationX {
127138
ny--
128139
}
129140
}
130-
// fmt.Printf("driving to %d, %d\n", x, y)
131-
// fmt.Printf("current pos [%d, %d]", v.CurrentPosition.X, v.CurrentPosition.Y)
132-
// fmt.Printf("-\n")
133-
// fmt.Printf("moving from (%d, %d) to (%d, %d)\n", cx, cy, nx, ny)
141+
142+
// update the vehicle position
134143
v.SetPosition(nx, ny)
135-
if x == nx && y == ny {
144+
145+
// it reached his destination so we can consider him ready to start his ride
146+
if destinationX == nx && destinationY == ny {
136147
v.OnRide = true
137148
}
138149
}
150+
151+
// NewVehicle a constructor for Trip
152+
// takes its trips as parameters
153+
func NewVehicle(trips []int32) *Vehicle {
154+
v := new(Vehicle)
155+
156+
// default values
157+
v.SetPosition(0, 0)
158+
v.Enabled = true
159+
v.CurrentTrip = 0
160+
v.Trips = trips
161+
162+
return v
163+
}
164+
165+
// SetPosition setter for CurrentPosition
166+
func (v *Vehicle) SetPosition(x, y int32) {
167+
v.CurrentPosition.X = x
168+
v.CurrentPosition.Y = y
169+
}
170+
171+
// GetPosition getter for CurrentPosition
172+
func (v *Vehicle) GetPosition() (int32, int32) {
173+
return v.CurrentPosition.X, v.CurrentPosition.Y
174+
}

0 commit comments

Comments
 (0)