From 0cb604f102774eafb2b97deeb635dd84c172e36f Mon Sep 17 00:00:00 2001 From: Luke Meyers Date: Sat, 8 Feb 2020 20:27:39 -0800 Subject: [PATCH] Gather commands before UpdateState --- game/ai.go | 12 ++++++------ game/commands.go | 10 +++++----- game/game.go | 11 ++--------- main.go | 10 +++++++++- 4 files changed, 22 insertions(+), 21 deletions(-) diff --git a/game/ai.go b/game/ai.go index e4172e6..e5012eb 100644 --- a/game/ai.go +++ b/game/ai.go @@ -1,10 +1,10 @@ package game -func smartChooseCommand(s State, teamID int) command { - return smartChooseHelper(s, teamID, aiDepth) +func ChooseCommand(s State, teamID int) Command { + return chooseCommandHelper(s, teamID, aiDepth) } -func smartChooseHelper(s State, teamID int, depth int) command { +func chooseCommandHelper(s State, teamID int, depth int) Command { bestCmd, bestN := speedUp, 0 for _, cmd := range validCommands { @@ -17,7 +17,7 @@ func smartChooseHelper(s State, teamID int, depth int) command { return bestCmd } -func score(cmd command, s State, teamID int, depth int) int { +func score(cmd Command, s State, teamID int, depth int) int { if !legalMove(s, teamID, cmd) { return -1 } @@ -28,11 +28,11 @@ func score(cmd command, s State, teamID int, depth int) int { if b == nil { return 0 } - return b.Position.Pos + return b.Position.Pos*100 + b.Battery.Charge } depth-- - cmd2 := smartChooseHelper(s, teamID, depth) + cmd2 := chooseCommandHelper(s, teamID, depth) return score(cmd2, s, teamID, depth) } diff --git a/game/commands.go b/game/commands.go index 722b279..cfdc675 100644 --- a/game/commands.go +++ b/game/commands.go @@ -1,18 +1,18 @@ package game -type command int +type Command int const ( - speedUp command = iota + speedUp Command = iota slowDown left right clearObstacle ) -var validCommands = []command{speedUp, slowDown, left, right, clearObstacle} +var validCommands = []Command{speedUp, slowDown, left, right, clearObstacle} -func doCommand(cmd command, s State, teamID int) State { +func doCommand(cmd Command, s State, teamID int) State { da := 1 //da += rand.Intn(3) - 1 @@ -53,7 +53,7 @@ func doCommand(cmd command, s State, teamID int) State { return s } -func (c command) String() string { +func (c Command) String() string { switch c { case speedUp: return "speed up" diff --git a/game/game.go b/game/game.go index d072e60..fbd5a4f 100644 --- a/game/game.go +++ b/game/game.go @@ -4,14 +4,7 @@ import ( "log" ) -func UpdateState(s State, sOld State) State { - cmds := make([]command, len(s.Teams)) - for i := range s.Teams { - cmd := smartChooseCommand(s, i) - log.Printf("team %d chose to %v", i, cmd) - cmds[i] = cmd - } - +func UpdateState(s State, sOld State, cmds []Command) State { for i, cmd := range cmds { s = doCommand(cmd, s, i) } @@ -114,7 +107,7 @@ func gameOver(s State) bool { return false } -func legalMove(s State, teamID int, cmd command) bool { +func legalMove(s State, teamID int, cmd Command) bool { r := ActiveRacer(s.Teams[teamID]) if r == nil { return false diff --git a/main.go b/main.go index ead2724..ba8b85f 100644 --- a/main.go +++ b/main.go @@ -57,7 +57,15 @@ func run() error { log.Printf("TURN %d", turn) rs.Animating = true rs.Frame = 0 - s = game.UpdateState(s, sOld) + + cmds := make([]game.Command, len(s.Teams)) + for i := range s.Teams { + cmd := game.ChooseCommand(s, i) + log.Printf("team %d chose to %v", i, cmd) + cmds[i] = cmd + } + + s = game.UpdateState(s, sOld, cmds) turn++ if s.GameOver { s = game.NewState()