don't place obstacles on occupied spaces

This commit is contained in:
Luke Meyers 2020-02-08 00:05:43 -08:00
parent f7a2b25a63
commit 7bb4272e61
1 changed files with 32 additions and 7 deletions

View File

@ -209,26 +209,51 @@ func NewState() State {
return State{
Teams: teams,
Obstacles: randomObstacles(),
Obstacles: randomObstacles(teams),
}
}
func randomObstacles() []Obstacle {
func randomObstacles(teams []Team) []Obstacle {
var os []Obstacle
const numObstacles = 4 * NumTeams
const numObstacles = 12 * NumTeams
for i := 0; i < numObstacles; i++ {
os = append(os, Obstacle{
Position: Position{
Pos: rand.Intn(Steps-8) + 4,
Lane: rand.Intn(NumLanes),
},
Position: randomOpenPosition(teams, os),
})
}
return os
}
func randomOpenPosition(ts []Team, os []Obstacle) Position {
for {
p := Position{
Pos: rand.Intn(Steps-8) + 4,
Lane: rand.Intn(NumLanes),
}
if positionOpen(p, ts, os) {
return p
}
}
}
func positionOpen(pos Position, ts []Team, os []Obstacle) bool {
for _, t := range ts {
for _, b := range t.Bots {
if b.Position == pos {
return false
}
}
}
for _, o := range os {
if o.Position == pos {
return false
}
}
return true
}
var (
staticObstacles = []Obstacle{
{