AI don't attempt illegal moves

This commit is contained in:
Luke Meyers 2020-02-07 20:28:49 -08:00
parent 5b45dad1bb
commit 2c660f07d5
2 changed files with 19 additions and 0 deletions

View File

@ -36,6 +36,9 @@ func smartChooseCommand(s State, teamID int) command {
log.Printf("team %d base score: %d", teamID, score(s, teamID)) log.Printf("team %d base score: %d", teamID, score(s, teamID))
for _, cmd := range []command{speedUp, slowDown, left, right} { for _, cmd := range []command{speedUp, slowDown, left, right} {
if !legalMove(s, teamID, cmd) {
continue
}
ss := doCommand(cmd, s, teamID) ss := doCommand(cmd, s, teamID)
n := score(ss, teamID) n := score(ss, teamID)
log.Printf("team %d score %s: %d", teamID, cmd, n) log.Printf("team %d score %s: %d", teamID, cmd, n)

View File

@ -107,6 +107,22 @@ func gameOver(s State) bool {
return false 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 { func abs(n int) int {
if n < 0 { if n < 0 {
return -n return -n