Gather commands before UpdateState

This commit is contained in:
Luke Meyers 2020-02-08 20:27:39 -08:00
parent 879260f719
commit 0cb604f102
4 changed files with 22 additions and 21 deletions

View File

@ -1,10 +1,10 @@
package game package game
func smartChooseCommand(s State, teamID int) command { func ChooseCommand(s State, teamID int) Command {
return smartChooseHelper(s, teamID, aiDepth) 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 bestCmd, bestN := speedUp, 0
for _, cmd := range validCommands { for _, cmd := range validCommands {
@ -17,7 +17,7 @@ func smartChooseHelper(s State, teamID int, depth int) command {
return bestCmd 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) { if !legalMove(s, teamID, cmd) {
return -1 return -1
} }
@ -28,11 +28,11 @@ func score(cmd command, s State, teamID int, depth int) int {
if b == nil { if b == nil {
return 0 return 0
} }
return b.Position.Pos return b.Position.Pos*100 + b.Battery.Charge
} }
depth-- depth--
cmd2 := smartChooseHelper(s, teamID, depth) cmd2 := chooseCommandHelper(s, teamID, depth)
return score(cmd2, s, teamID, depth) return score(cmd2, s, teamID, depth)
} }

View File

@ -1,18 +1,18 @@
package game package game
type command int type Command int
const ( const (
speedUp command = iota speedUp Command = iota
slowDown slowDown
left left
right right
clearObstacle 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 := 1
//da += rand.Intn(3) - 1 //da += rand.Intn(3) - 1
@ -53,7 +53,7 @@ func doCommand(cmd command, s State, teamID int) State {
return s return s
} }
func (c command) String() string { func (c Command) String() string {
switch c { switch c {
case speedUp: case speedUp:
return "speed up" return "speed up"

View File

@ -4,14 +4,7 @@ import (
"log" "log"
) )
func UpdateState(s State, sOld State) State { func UpdateState(s State, sOld State, cmds []Command) 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
}
for i, cmd := range cmds { for i, cmd := range cmds {
s = doCommand(cmd, s, i) s = doCommand(cmd, s, i)
} }
@ -114,7 +107,7 @@ func gameOver(s State) bool {
return false 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]) r := ActiveRacer(s.Teams[teamID])
if r == nil { if r == nil {
return false return false

10
main.go
View File

@ -57,7 +57,15 @@ func run() error {
log.Printf("TURN %d", turn) log.Printf("TURN %d", turn)
rs.Animating = true rs.Animating = true
rs.Frame = 0 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++ turn++
if s.GameOver { if s.GameOver {
s = game.NewState() s = game.NewState()