don't place obstacles on occupied spaces
This commit is contained in:
parent
f7a2b25a63
commit
7bb4272e61
39
game/game.go
39
game/game.go
|
@ -209,26 +209,51 @@ func NewState() State {
|
||||||
|
|
||||||
return State{
|
return State{
|
||||||
Teams: teams,
|
Teams: teams,
|
||||||
Obstacles: randomObstacles(),
|
Obstacles: randomObstacles(teams),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func randomObstacles() []Obstacle {
|
func randomObstacles(teams []Team) []Obstacle {
|
||||||
var os []Obstacle
|
var os []Obstacle
|
||||||
|
|
||||||
const numObstacles = 4 * NumTeams
|
const numObstacles = 12 * NumTeams
|
||||||
for i := 0; i < numObstacles; i++ {
|
for i := 0; i < numObstacles; i++ {
|
||||||
os = append(os, Obstacle{
|
os = append(os, Obstacle{
|
||||||
Position: Position{
|
Position: randomOpenPosition(teams, os),
|
||||||
Pos: rand.Intn(Steps-8) + 4,
|
|
||||||
Lane: rand.Intn(NumLanes),
|
|
||||||
},
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
return 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 (
|
var (
|
||||||
staticObstacles = []Obstacle{
|
staticObstacles = []Obstacle{
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in New Issue