Gather commands before UpdateState
This commit is contained in:
parent
879260f719
commit
0cb604f102
12
game/ai.go
12
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)
|
||||
}
|
||||
|
||||
|
|
|
@ -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"
|
||||
|
|
11
game/game.go
11
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
|
||||
|
|
10
main.go
10
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()
|
||||
|
|
Loading…
Reference in New Issue