Created
November 11, 2011 05:29
-
-
Save border/1357273 to your computer and use it in GitHub Desktop.
A ants bot for AIChallenge By Golang
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"os" | |
"rand" | |
) | |
type MyBot struct { | |
orders map[Location]Location | |
} | |
//NewBot creates a new instance of your bot | |
func NewBot(s *State) Bot { | |
mb := &MyBot{ | |
orders: make(map[Location]Location), | |
} | |
return mb | |
} | |
//DoTurn is where you should do your bot's actual work. | |
func (mb *MyBot) DoTurn(s *State) os.Error { | |
dirs := []Direction{North, East, South, West} | |
for loc, ant := range s.Map.Ants { | |
if ant != MY_ANT { | |
continue | |
} | |
//try each direction in a random order | |
p := rand.Perm(4) | |
for _, i := range p { | |
d := dirs[i] | |
loc2 := s.Map.Move(loc, d) | |
//item := s.Map.itemGrid[loc] | |
if s.Map.SafeDestination(loc2) { | |
if _, is := mb.orders[loc2]; !is { | |
s.IssueOrderLoc(loc, d) | |
mb.orders[loc2] = loc | |
} | |
//there's also an s.IssueOrderRowCol if you don't have a Location handy | |
break | |
} | |
} | |
} | |
//returning an error will halt the whole program! | |
return nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment