From 791fffdebb343ae352b5e3ee6d0e10a8b9ceebfd Mon Sep 17 00:00:00 2001 From: Luke Meyers Date: Fri, 7 Feb 2020 20:17:09 -0800 Subject: [PATCH] primitive predictive AI --- game/ai.go | 27 +++++++++++++++++++++++++++ game/commands.go | 20 ++++++++++++++++++++ game/game.go | 8 +++----- game/physics.go | 3 --- main.go | 5 +++++ 5 files changed, 55 insertions(+), 8 deletions(-) diff --git a/game/ai.go b/game/ai.go index f2b2096..cb9ea52 100644 --- a/game/ai.go +++ b/game/ai.go @@ -1,5 +1,7 @@ package game +import "log" + func chooseCommand(s State, teamID int) command { t := s.Teams[teamID] h := ActiveBot(t) @@ -28,3 +30,28 @@ func chooseCommand(s State, teamID int) command { return speedUp } + +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} { + ss := doCommand(cmd, s, teamID) + n := score(ss, teamID) + log.Printf("team %d score %s: %d", teamID, cmd, n) + if n > bestN { + bestCmd, bestN = cmd, n + } + } + + return bestCmd +} + +func score(s State, teamID int) int { + t := s.Teams[teamID] + b := ActiveBot(t) + if b == nil { + return 0 + } + return b.Position.Pos +} diff --git a/game/commands.go b/game/commands.go index 652bd8b..f9bae50 100644 --- a/game/commands.go +++ b/game/commands.go @@ -32,5 +32,25 @@ func doCommand(cmd command, s State, teamID int) State { } s = updateBot(s, *b) + + if b := ActiveBot(s.Teams[teamID]); b != nil { + s = moveBot(s, *b) + } + s = maybePassBaton(s, teamID) + return s } + +func (c command) String() string { + switch c { + case speedUp: + return "speed up" + case slowDown: + return "slow down" + case left: + return "go left" + case right: + return "go right" + } + return "(unknown)" +} diff --git a/game/game.go b/game/game.go index aa91b87..ad07357 100644 --- a/game/game.go +++ b/game/game.go @@ -4,11 +4,9 @@ import "log" func UpdateState(s State, sOld State) State { for i := range s.Teams { - s = doCommand(chooseCommand(s, i), s, i) - if b := ActiveBot(s.Teams[i]); b != nil { - s = moveBot(s, *b) - } - s = maybePassBaton(s, i) + cmd := smartChooseCommand(s, i) + log.Printf("team %d chose to %v", i, cmd) + s = doCommand(cmd, s, i) } for _, t := range s.Teams { diff --git a/game/physics.go b/game/physics.go index d0991dc..0213bfe 100644 --- a/game/physics.go +++ b/game/physics.go @@ -1,7 +1,5 @@ package game -import "log" - func accelerate(b Bot) Bot { if b.a < -maxA { b.a = -maxA @@ -24,7 +22,6 @@ func accelerate(b Bot) Bot { func moveBot(s State, b Bot) State { for i := 0; i < b.v; i++ { if o := collide(b.Position.Pos+1, b.Position.Lane, s); o != nil { - log.Printf("bot %d crashed into %#v!", b.ID, o) return destroyBot(s, b) } else { b.Position.Pos++ diff --git a/main.go b/main.go index ee77986..66a61b7 100644 --- a/main.go +++ b/main.go @@ -1,6 +1,7 @@ package main import ( + "log" "math/rand" "relay/game" "relay/gfx" @@ -34,6 +35,7 @@ func run() { Frames: 20, } sOld := s + turn := 1 for !w.Closed() && !s.GameOver { switch { @@ -45,12 +47,15 @@ func run() { sOld = s } case w.Pressed(pixelgl.KeySpace): + log.Printf("TURN %d", turn) rs.Animating = true rs.Frame = 0 s = game.UpdateState(s, sOld) + turn++ if s.GameOver { s = game.NewState() sOld = s + turn = 1 } }