diff --git a/game/ai.go b/game/ai.go index 002831a..3b50b60 100644 --- a/game/ai.go +++ b/game/ai.go @@ -1,7 +1,5 @@ package game -import "log" - func chooseCommand(s State, teamID int) command { t := s.Teams[teamID] h := ActiveBot(t) @@ -32,16 +30,18 @@ func chooseCommand(s State, teamID int) command { } func smartChooseCommand(s State, teamID int) command { + return smartChooseHelper(s, teamID, 2) +} + +func smartChooseHelper(s State, teamID int, depth int) command { bestCmd, bestN := speedUp, 0 - log.Printf("team %d base score: %d", teamID, score(s, teamID)) for _, cmd := range validCommands { if !legalMove(s, teamID, cmd) { continue } ss := doCommand(cmd, s, teamID) - n := score(ss, teamID) - log.Printf("team %d score %s: %d", teamID, cmd, n) + n := score(ss, teamID, depth) if n > bestN { bestCmd, bestN = cmd, n } @@ -50,11 +50,17 @@ func smartChooseCommand(s State, teamID int) command { return bestCmd } -func score(s State, teamID int) int { - t := s.Teams[teamID] - b := ActiveBot(t) - if b == nil { - return 0 +func score(s State, teamID int, depth int) int { + if depth == 0 { + t := s.Teams[teamID] + b := ActiveBot(t) + if b == nil { + return 0 + } + return b.Position.Pos } - return b.Position.Pos + + cmd := smartChooseHelper(s, teamID, depth-1) + ss := doCommand(cmd, s, teamID) + return score(ss, teamID, depth-1) } diff --git a/game/game.go b/game/game.go index dcbd2fc..4ebfd83 100644 --- a/game/game.go +++ b/game/game.go @@ -85,11 +85,9 @@ func updateTeam(s State, t Team) State { func destroyBot(s State, b Bot) State { // insert obstacle where bot was s.Obstacles = append(s.Obstacles, Obstacle{Position: b.Position}) - log.Printf("spawn obstacle at %v", b.Position) // spawn bot back at starting position b.Position = b.StartPos - log.Printf("respawn bot %d at %v", b.ID, b.Position) return updateBot(s, b) } @@ -97,7 +95,11 @@ func destroyBot(s State, b Bot) State { func removeObstacle(s State, pos Position) State { for i, o := range s.Obstacles { if o.Position == pos { - s.Obstacles = append(s.Obstacles[:i], s.Obstacles[i+1:]...) + var os []Obstacle + os = append(os, s.Obstacles[:i]...) + os = append(os, s.Obstacles[i+1:]...) + s.Obstacles = os + //s.Obstacles = append([]Obstacle{}, append(s.Obstacles[:i], s.Obstacles[i+1:]...)...) break } } @@ -257,8 +259,8 @@ var ( ) const ( - Steps = 40 - numBots = 4 - NumTeams = 2 + Steps = 50 + numBots = 5 + NumTeams = 6 NumLanes = NumTeams ) diff --git a/gfx/gfx.go b/gfx/gfx.go index 83b6d89..928b04b 100644 --- a/gfx/gfx.go +++ b/gfx/gfx.go @@ -33,32 +33,16 @@ func Render(rs RenderState, sOld, sNew game.State, w *pixelgl.Window) RenderStat } func renderBots(sOld, sNew game.State, tween float64, w *pixelgl.Window, colors map[*game.Team]pixel.RGBA) { - bounds := w.Bounds() - im := imdraw.New(nil) - for i, t := range sNew.Teams { + c := colors[&sNew.Teams[i]] for j, bot := range t.Bots { - c := colors[&sNew.Teams[i]] - im.Color = c - oldBot := sOld.Teams[i].Bots[j] - oldPos := lanePos(oldBot.Position.Pos, oldBot.Position.Lane, botWidth, bounds) - newPos := lanePos(bot.Position.Pos, bot.Position.Lane, botWidth, bounds) - - pos := pixel.Vec{ - X: oldPos.X + tween*(newPos.X-oldPos.X), - Y: oldPos.Y + tween*(newPos.Y-oldPos.Y), - } - - im.Push(pos) - im.Clear() - im.Circle(botWidth, 0) - im.Draw(w) + renderBot(oldBot, bot, sOld, sNew, w, c, tween) } oldHolder, newHolder := game.ActiveBot(sOld.Teams[i]), game.ActiveBot(sNew.Teams[i]) - oldPos := lanePos(oldHolder.Position.Pos, oldHolder.Position.Lane, botWidth, bounds) - newPos := lanePos(newHolder.Position.Pos, newHolder.Position.Lane, botWidth, bounds) + oldPos := lanePos(oldHolder.Position.Pos, oldHolder.Position.Lane, botWidth, w.Bounds()) + newPos := lanePos(newHolder.Position.Pos, newHolder.Position.Lane, botWidth, w.Bounds()) pos := pixel.Vec{ X: oldPos.X + tween*(newPos.X-oldPos.X), @@ -68,6 +52,24 @@ func renderBots(sOld, sNew game.State, tween float64, w *pixelgl.Window, colors } } +func renderBot(oldBot, bot game.Bot, sOld, sNew game.State, w *pixelgl.Window, c pixel.RGBA, tween float64) { + im := imdraw.New(nil) + im.Color = c + + oldPos := lanePos(oldBot.Position.Pos, oldBot.Position.Lane, botWidth, w.Bounds()) + newPos := lanePos(bot.Position.Pos, bot.Position.Lane, botWidth, w.Bounds()) + + pos := pixel.Vec{ + X: oldPos.X + tween*(newPos.X-oldPos.X), + Y: oldPos.Y + tween*(newPos.Y-oldPos.Y), + } + + im.Push(pos) + im.Clear() + im.Circle(botWidth, 0) + im.Draw(w) +} + func renderBaton(pos pixel.Vec, w *pixelgl.Window) { im := imdraw.New(nil) im.Color = pixel.RGB(0, 0, 0)