From 2ae124f59e829c8227e5f53366348f4a0fb3cce2 Mon Sep 17 00:00:00 2001 From: Luke Meyers Date: Wed, 5 Feb 2020 21:47:15 -0800 Subject: [PATCH] lane change right --- game/ai.go | 5 ++++- game/commands.go | 30 ++++++++++++++++++++++++++++++ game/game.go | 27 ++------------------------- 3 files changed, 36 insertions(+), 26 deletions(-) create mode 100644 game/commands.go diff --git a/game/ai.go b/game/ai.go index 7d80fb0..cd1acfa 100644 --- a/game/ai.go +++ b/game/ai.go @@ -5,7 +5,10 @@ import "log" func chooseCommand(t Team, s State) command { h := t.Baton.Holder if collide(h.Pos+1, h.Lane, s) { - return left + if h.Lane <= t.Lane { + return left + } + return right } var nextBot *Bot diff --git a/game/commands.go b/game/commands.go new file mode 100644 index 0000000..2556e00 --- /dev/null +++ b/game/commands.go @@ -0,0 +1,30 @@ +package game + +import "math/rand" + +func doCommand(cmd command, b *Bot) { + da := 1 + da += rand.Intn(3) - 1 + + switch cmd { + case speedUp: + b.a += da + accelerate(b) + case slowDown: + b.a -= da + accelerate(b) + case left: + b.Lane++ + case right: + b.Lane-- + } +} + +type command int + +const ( + speedUp command = iota + slowDown + left + right +) diff --git a/game/game.go b/game/game.go index 91ee056..dcf49db 100644 --- a/game/game.go +++ b/game/game.go @@ -2,7 +2,6 @@ package game import ( "log" - "math/rand" ) type State struct { @@ -26,6 +25,7 @@ func NewState() State { teams = append(teams, Team{ Bots: bots, Baton: Baton{Holder: &bots[0]}, + Lane: i, }) } @@ -48,6 +48,7 @@ type Team struct { Bots []Bot Baton Baton won bool + Lane int } type Bot struct { @@ -85,30 +86,6 @@ func UpdateState(sOld State) State { return s } -func doCommand(cmd command, b *Bot) { - da := 1 - da += rand.Intn(3) - 1 - - switch cmd { - case speedUp: - b.a += da - accelerate(b) - case slowDown: - b.a -= da - accelerate(b) - case left: - b.Lane++ - } -} - -type command int - -const ( - speedUp command = iota - slowDown - left -) - func maybePassBaton(t *Team) { for i, b := range t.Bots { h := t.Baton.Holder