diff --git a/game/env.go b/game/env.go new file mode 100644 index 0000000..4488887 --- /dev/null +++ b/game/env.go @@ -0,0 +1,90 @@ +package game + +import "math/rand" + +type Obstacle struct { + Position Position +} + +func removeObstacle(s State, pos Position) State { + for i, o := range s.Obstacles { + if o.Position == pos { + var os []Obstacle + os = append(os, s.Obstacles[:i]...) + os = append(os, s.Obstacles[i+1:]...) + s.Obstacles = os + break + } + } + return s +} + +func positionOpen(pos Position, ts []Team, os []Obstacle) bool { + for _, t := range ts { + for _, r := range t.Racers { + if r.Position == pos { + return false + } + } + } + for _, o := range os { + if o.Position == pos { + return false + } + } + return true +} + +func randomOpenPosition(ts []Team, os []Obstacle) Position { + for { + p := Position{ + Pos: rand.Intn(Steps-8) + 4, + Lane: rand.Intn(NumLanes), + } + if positionOpen(p, ts, os) { + return p + } + } +} + +func randomObstacles(teams []Team) []Obstacle { + var os []Obstacle + + const numObstacles = 5 * NumTeams + for i := 0; i < numObstacles; i++ { + os = append(os, Obstacle{ + Position: randomOpenPosition(teams, os), + }) + } + + return os +} + +var ( + staticObstacles = []Obstacle{ + { + Position: Position{ + Lane: 0, + Pos: Steps / 3, + }, + }, + { + Position: Position{ + Lane: 1, + Pos: Steps * 2 / 3, + }, + }, + { + Position: Position{ + Lane: 2, + Pos: Steps / 2, + }, + }, + { + Position: Position{ + Lane: 3, + Pos: Steps * 3 / 4, + }, + }, + } +) diff --git a/game/game.go b/game/game.go index 5386725..3a0ffb9 100644 --- a/game/game.go +++ b/game/game.go @@ -2,7 +2,6 @@ package game import ( "log" - "math/rand" ) func UpdateState(s State, sOld State) State { @@ -92,20 +91,6 @@ func destroyRacer(s State, r Racer) State { return updateRacer(s, r) } -func removeObstacle(s State, pos Position) State { - for i, o := range s.Obstacles { - if o.Position == pos { - 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 - } - } - return s -} - func won(r Racer, s State) bool { return r.Position.Pos >= Steps } @@ -179,10 +164,6 @@ type Baton struct { HolderID int } -type Obstacle struct { - Position Position -} - func NewState() State { var teams []Team for i := 0; i < NumTeams; i++ { @@ -213,76 +194,6 @@ func NewState() State { } } -func randomObstacles(teams []Team) []Obstacle { - var os []Obstacle - - const numObstacles = 5 * NumTeams - for i := 0; i < numObstacles; i++ { - os = append(os, Obstacle{ - Position: randomOpenPosition(teams, os), - }) - } - - return os -} - -func randomOpenPosition(ts []Team, os []Obstacle) Position { - for { - p := Position{ - Pos: rand.Intn(Steps-8) + 4, - Lane: rand.Intn(NumLanes), - } - if positionOpen(p, ts, os) { - return p - } - } -} - -func positionOpen(pos Position, ts []Team, os []Obstacle) bool { - for _, t := range ts { - for _, r := range t.Racers { - if r.Position == pos { - return false - } - } - } - for _, o := range os { - if o.Position == pos { - return false - } - } - return true -} - -var ( - staticObstacles = []Obstacle{ - { - Position: Position{ - Lane: 0, - Pos: Steps / 3, - }, - }, - { - Position: Position{ - Lane: 1, - Pos: Steps * 2 / 3, - }, - }, - { - Position: Position{ - Lane: 2, - Pos: Steps / 2, - }, - }, - { - Position: Position{ - Lane: 3, - Pos: Steps * 3 / 4, - }, - }, - } -) - const ( Steps = 50 numRacers = 5