From 2c660f07d5264782b01049ccdfa4846b3578e3dd Mon Sep 17 00:00:00 2001 From: Luke Meyers Date: Fri, 7 Feb 2020 20:28:49 -0800 Subject: [PATCH] AI don't attempt illegal moves --- game/ai.go | 3 +++ game/game.go | 16 ++++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/game/ai.go b/game/ai.go index cb9ea52..50e53f4 100644 --- a/game/ai.go +++ b/game/ai.go @@ -36,6 +36,9 @@ func smartChooseCommand(s State, teamID int) command { log.Printf("team %d base score: %d", teamID, score(s, teamID)) for _, cmd := range []command{speedUp, slowDown, left, right} { + if !legalMove(s, teamID, cmd) { + continue + } ss := doCommand(cmd, s, teamID) n := score(ss, teamID) log.Printf("team %d score %s: %d", teamID, cmd, n) diff --git a/game/game.go b/game/game.go index ca68e01..b6d9309 100644 --- a/game/game.go +++ b/game/game.go @@ -107,6 +107,22 @@ func gameOver(s State) bool { return false } +func legalMove(s State, teamID int, cmd command) bool { + b := ActiveBot(s.Teams[teamID]) + if b == nil { + return false + } + + switch cmd { + case left: + return b.Position.Lane < NumLanes-1 + case right: + return b.Position.Lane > 0 + + } + return true +} + func abs(n int) int { if n < 0 { return -n