split out env.go
This commit is contained in:
parent
6e7ac9c1ba
commit
24eb2c6143
|
@ -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,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
)
|
89
game/game.go
89
game/game.go
|
@ -2,7 +2,6 @@ package game
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"log"
|
"log"
|
||||||
"math/rand"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func UpdateState(s State, sOld State) State {
|
func UpdateState(s State, sOld State) State {
|
||||||
|
@ -92,20 +91,6 @@ func destroyRacer(s State, r Racer) State {
|
||||||
return updateRacer(s, r)
|
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 {
|
func won(r Racer, s State) bool {
|
||||||
return r.Position.Pos >= Steps
|
return r.Position.Pos >= Steps
|
||||||
}
|
}
|
||||||
|
@ -179,10 +164,6 @@ type Baton struct {
|
||||||
HolderID int
|
HolderID int
|
||||||
}
|
}
|
||||||
|
|
||||||
type Obstacle struct {
|
|
||||||
Position Position
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewState() State {
|
func NewState() State {
|
||||||
var teams []Team
|
var teams []Team
|
||||||
for i := 0; i < NumTeams; i++ {
|
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 (
|
const (
|
||||||
Steps = 50
|
Steps = 50
|
||||||
numRacers = 5
|
numRacers = 5
|
||||||
|
|
Loading…
Reference in New Issue