lane change right

This commit is contained in:
Luke Meyers 2020-02-05 21:47:15 -08:00
parent b113d52f66
commit 2ae124f59e
3 changed files with 36 additions and 26 deletions

View File

@ -5,8 +5,11 @@ import "log"
func chooseCommand(t Team, s State) command { func chooseCommand(t Team, s State) command {
h := t.Baton.Holder h := t.Baton.Holder
if collide(h.Pos+1, h.Lane, s) { if collide(h.Pos+1, h.Lane, s) {
if h.Lane <= t.Lane {
return left return left
} }
return right
}
var nextBot *Bot var nextBot *Bot
for i, b := range t.Bots { for i, b := range t.Bots {

30
game/commands.go Normal file
View File

@ -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
)

View File

@ -2,7 +2,6 @@ package game
import ( import (
"log" "log"
"math/rand"
) )
type State struct { type State struct {
@ -26,6 +25,7 @@ func NewState() State {
teams = append(teams, Team{ teams = append(teams, Team{
Bots: bots, Bots: bots,
Baton: Baton{Holder: &bots[0]}, Baton: Baton{Holder: &bots[0]},
Lane: i,
}) })
} }
@ -48,6 +48,7 @@ type Team struct {
Bots []Bot Bots []Bot
Baton Baton Baton Baton
won bool won bool
Lane int
} }
type Bot struct { type Bot struct {
@ -85,30 +86,6 @@ func UpdateState(sOld State) State {
return s 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) { func maybePassBaton(t *Team) {
for i, b := range t.Bots { for i, b := range t.Bots {
h := t.Baton.Holder h := t.Baton.Holder