From 7bb4272e61705dfd80d14324cc29b46da5bbfd5b Mon Sep 17 00:00:00 2001 From: Luke Meyers Date: Sat, 8 Feb 2020 00:05:43 -0800 Subject: [PATCH] don't place obstacles on occupied spaces --- game/game.go | 39 ++++++++++++++++++++++++++++++++------- 1 file changed, 32 insertions(+), 7 deletions(-) diff --git a/game/game.go b/game/game.go index 4ebfd83..53a6c4d 100644 --- a/game/game.go +++ b/game/game.go @@ -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{ {