|
- package main
-
- import (
- "botg/models"
- "botg/strategies"
- "bufio"
- "fmt"
- "math/rand"
- "os"
- "strconv"
- "strings"
- "time"
- )
-
- var stars []*models.Star
- var flights []models.Flight
- var links []models.Link
-
- func main() {
- // Write to logfile
- s1 := rand.NewSource(time.Now().UnixNano())
- r1 := rand.New(s1)
- rand.Seed(10000)
- filename := fmt.Sprintf("logs/%d.txt", r1.Intn(10000))
- f, err := os.Create(filename)
- if err != nil {
- panic(err)
- }
- defer f.Close()
-
- // Open stdin for reading
- reader := bufio.NewReader(os.Stdin)
-
- var gameComplete bool = false
- var roundComplete bool = false
-
- // Run strategy
- strategy := strategies.LinkerStrategy{}
- //strategy := strategies.NoneStrategy {}
-
- for gameComplete == false {
- roundComplete = false
-
- // Empty flights
- flights = []models.Flight{}
- links = []models.Link{}
- for roundComplete == false {
- text, err := reader.ReadString('\n')
- if err != nil {
- gameComplete = true
- break
- }
-
- //Write to log
- w := bufio.NewWriter(f)
- w.WriteString(text)
- w.Flush()
-
- roundComplete = parseLine(text)
- }
-
- strategy.Execute(stars, flights, links)
- fmt.Println("done")
- }
- }
-
- func parseLine(text string) bool {
- parts := strings.Split(strings.Trim(text, "\n"), " ")
- command := parts[0]
-
- switch command {
- case "stars":
- // Process stars
- idx := 0
- for i := 1; i < len(parts); i+=2 {
- var x, y int
- x, _ = strconv.Atoi(parts[i])
- y, _ = strconv.Atoi(parts[i+1])
- star := &models.Star{
- Idx: idx,
- X: x,
- Y: y,
- Richness: 0,
- Owner: -2,
- Ships: -2,
- Capturing: false,
- CapturedBy: -2,
- Turns: -2,
- }
- stars = append(stars, star)
- idx++
- }
- break
- case "star":
- var idx, _ = strconv.Atoi(parts[1])
- var richness, _ = strconv.Atoi(parts[2])
- var owner, _ = strconv.Atoi(parts[3])
- var ships, _ = strconv.Atoi(parts[4])
- var turns, _ = strconv.Atoi(parts[5])
- star := stars[idx]
- star.Richness = richness
- star.Owner = owner
- star.Ships = ships
- star.Turns = turns
- star.FlightsAllowed = 3
- if star.Capturing && !star.Unowned() {
- star.Capturing = false
- if !star.OwnedByMe() {
- star.CapturedBy = -2
- }
- }
- break
- case "link":
- var source_idx, _ = strconv.Atoi(parts[1])
- var target_idx, _ = strconv.Atoi(parts[2])
-
- link := models.Link{
- SourceStar: source_idx,
- TargetStar: target_idx,
- }
-
- links = append(links, link)
- break
- case "flight":
- var source_idx, _ = strconv.Atoi(parts[1])
- var target_idx, _ = strconv.Atoi(parts[2])
- var ship_count, _ = strconv.Atoi(parts[3])
- var owner, _ = strconv.Atoi(parts[4])
- var turns, _ = strconv.Atoi(parts[5])
-
- flight := models.Flight{
- SourceStar: source_idx,
- TargetStar: target_idx,
- Ships: ship_count,
- Owner: owner,
- Turns: turns,
- }
- flights = append(flights, flight)
- break
- case "done":
- return true
- }
-
- // Update star flight data
- for _, s := range stars {
- for _, f := range flights {
- if f.SourceStar == s.Idx {
- s.FlightsAllowed--
- }
- }
- }
-
- return false
- }
|