From eac373c4442b8eb4d9cc83b53c4a4dda585c3eab Mon Sep 17 00:00:00 2001 From: Luke Meyers Date: Fri, 7 Feb 2020 22:05:22 -0800 Subject: [PATCH] bots clear obstacles --- game/ai.go | 2 +- game/commands.go | 15 +++++++++++++-- game/game.go | 10 ++++++++++ 3 files changed, 24 insertions(+), 3 deletions(-) diff --git a/game/ai.go b/game/ai.go index 50e53f4..002831a 100644 --- a/game/ai.go +++ b/game/ai.go @@ -35,7 +35,7 @@ func smartChooseCommand(s State, teamID int) command { bestCmd, bestN := speedUp, 0 log.Printf("team %d base score: %d", teamID, score(s, teamID)) - for _, cmd := range []command{speedUp, slowDown, left, right} { + for _, cmd := range validCommands { if !legalMove(s, teamID, cmd) { continue } diff --git a/game/commands.go b/game/commands.go index f9bae50..64b8b55 100644 --- a/game/commands.go +++ b/game/commands.go @@ -7,8 +7,11 @@ const ( slowDown left right + clearObstacle ) +var validCommands = []command{speedUp, slowDown, left, right, clearObstacle} + func doCommand(cmd command, s State, teamID int) State { da := 1 //da += rand.Intn(3) - 1 @@ -22,17 +25,23 @@ func doCommand(cmd command, s State, teamID int) State { case speedUp: b.a += da *b = accelerate(*b) + s = updateBot(s, *b) case slowDown: b.a -= da *b = accelerate(*b) + s = updateBot(s, *b) case left: b.Position.Lane++ + s = updateBot(s, *b) case right: b.Position.Lane-- + s = updateBot(s, *b) + case clearObstacle: + pos := b.Position + pos.Pos++ + s = removeObstacle(s, pos) } - s = updateBot(s, *b) - if b := ActiveBot(s.Teams[teamID]); b != nil { s = moveBot(s, *b) } @@ -51,6 +60,8 @@ func (c command) String() string { return "go left" case right: return "go right" + case clearObstacle: + return "clear obstacle" } return "(unknown)" } diff --git a/game/game.go b/game/game.go index b6d9309..0dd03fe 100644 --- a/game/game.go +++ b/game/game.go @@ -94,6 +94,16 @@ func destroyBot(s State, b Bot) State { return updateBot(s, b) } +func removeObstacle(s State, pos Position) State { + for i, o := range s.Obstacles { + if o.Position == pos { + s.Obstacles = append(s.Obstacles[:i], s.Obstacles[i+1:]...) + break + } + } + return s +} + func won(b Bot, s State) bool { return b.Position.Pos >= Steps }