Loading cmd/wator/main.go +10 −10 Original line number Diff line number Diff line Loading @@ -30,11 +30,11 @@ func main() { Debug: false, //true, Width: 72, //32, Height: 36, //14, InitialFishes: 400, //200, InitialSharks: 40, //20, BreedFish: 3, BreedShark: 30, StarveShark: 2, InitialFood: 400, //200, InitialHunters: 40, //20, BreedFood: 3, BreedHunter: 30, StarveHunter: 2, SleepMilliseconds: 50 * time.Millisecond, } Loading @@ -48,11 +48,11 @@ func main() { flag.BoolVar(&w.Debug, "debug", false, "enable debug mode") flag.IntVar(&w.Width, "width", w.Width, "wator width") flag.IntVar(&w.Height, "height", w.Height, "wator height") flag.IntVar(&w.InitialFishes, "fish-count", 400, "initial number of fish") flag.IntVar(&w.InitialSharks, "shark-count", 40, "initial number of sharks") flag.IntVar(&w.BreedFish, "fish-breed", 3, "chronos for fish to breed") flag.IntVar(&w.BreedShark, "shark-breed", 30, "chronos for sharks to breed") flag.IntVar(&w.StarveShark, "shark-starve", 2, "chronos for sharks to starve") flag.IntVar(&w.InitialFood, "fish-count", 400, "initial number of fish") flag.IntVar(&w.InitialHunters, "shark-count", 40, "initial number of sharks") flag.IntVar(&w.BreedFood, "fish-breed", 3, "chronos for fish to breed") flag.IntVar(&w.BreedHunter, "shark-breed", 30, "chronos for sharks to breed") flag.IntVar(&w.StarveHunter, "shark-starve", 2, "chronos for sharks to starve") //flag.IntVar(&w.MaxAgeFishes, "fish-max-age", ) flag.Parse() Loading ocean.go +14 −14 Original line number Diff line number Diff line Loading @@ -4,22 +4,22 @@ import ( "math/rand" ) // Ocean is the container for fish and sharks. type Ocean struct { // Playfield is the container for fish and sharks. type Playfield struct { Cells map[position]Celler Food map[position]Food Hunters map[position]Hunter } // CreateOcean initializes a new ocean. func createOcean() (*Ocean, error) { o := &Ocean{Cells: make(map[position]Celler)} func createPlayfield() (*Playfield, error) { o := &Playfield{Cells: make(map[position]Celler)} o.Food = make(map[position]Food) o.Hunters = make(map[position]Hunter) return o, nil } func (o *Ocean) populateWater(w *Wator) error { func (o *Playfield) populateCells(w *Wator) error { for x := 0; x < w.Width; x++ { for y := 0; y < w.Height; y++ { p := position{x: x, y: y} Loading @@ -29,8 +29,8 @@ func (o *Ocean) populateWater(w *Wator) error { return nil } func (o *Ocean) populateFishes(w *Wator) error { for i := 0; i < w.InitialFishes; i++ { func (o *Playfield) populateFood(w *Wator) error { for i := 0; i < w.InitialFood; i++ { done := false for !done { x := rand.Intn(w.Width) Loading @@ -38,7 +38,7 @@ func (o *Ocean) populateFishes(w *Wator) error { p := position{x: x, y: y} switch o.Cells[p].(type) { case Celler: a := rand.Intn(w.BreedFish) a := rand.Intn(w.BreedFood) f := w.FoodCreator(a) o.Cells[p] = f o.Food[p] = f Loading @@ -49,8 +49,8 @@ func (o *Ocean) populateFishes(w *Wator) error { return nil } func (o *Ocean) populateSharks(w *Wator) error { for i := 0; i < w.InitialSharks; i++ { func (o *Playfield) populateHunters(w *Wator) error { for i := 0; i < w.InitialHunters; i++ { done := false for !done { x := rand.Intn(w.Width) Loading @@ -58,7 +58,7 @@ func (o *Ocean) populateSharks(w *Wator) error { p := position{x: x, y: y} switch o.Cells[p].(type) { case Celler: a := rand.Intn(w.BreedShark) a := rand.Intn(w.BreedHunter) s := w.HunterCreator(a, 0) o.Cells[p] = s o.Hunters[p] = s Loading @@ -69,7 +69,7 @@ func (o *Ocean) populateSharks(w *Wator) error { return nil } func (o *Ocean) isWater(p position) bool { func (o *Playfield) isCeller(p position) bool { r := false switch o.Cells[p].(type) { case Celler: Loading @@ -78,7 +78,7 @@ func (o *Ocean) isWater(p position) bool { return r } func (o *Ocean) isFish(p position) bool { func (o *Playfield) isFood(p position) bool { r := false switch o.Cells[p].(type) { case Food: Loading @@ -87,7 +87,7 @@ func (o *Ocean) isFish(p position) bool { return r } func (o *Ocean) isShark(p position) bool { func (o *Playfield) isHunter(p position) bool { r := false switch o.Cells[p].(type) { case Hunter: Loading wator.go +26 −26 Original line number Diff line number Diff line Loading @@ -13,18 +13,18 @@ type Wator struct { Debug bool Width int Height int InitialFishes int InitialSharks int BreedFish int BreedShark int StarveShark int MaxAgeFishes int MaxAgeSharks int InitialFood int InitialHunters int BreedFood int BreedHunter int StarveHunter int MaxAgeFood int MaxAgeHunter int SleepMilliseconds time.Duration chronon int last *Ocean current *Ocean next *Ocean last *Playfield current *Playfield next *Playfield CellCreator func() Celler FoodCreator func(int) Food HunterCreator func(int, int) Hunter Loading @@ -50,23 +50,23 @@ func (w *Wator) Populate() error { panic("no hunter creator function given") } o, err := createOcean() o, err := createPlayfield() if err != nil { panic(err) } // fill with water if err := o.populateWater(w); err != nil { if err := o.populateCells(w); err != nil { panic(err) } // place fishes if err := o.populateFishes(w); err != nil { if err := o.populateFood(w); err != nil { panic(err) } // place sharks if err := o.populateSharks(w); err != nil { if err := o.populateHunters(w); err != nil { panic(err) } Loading Loading @@ -112,13 +112,13 @@ func (w *Wator) String() string { // Next caclculates the next generation of wator. func (w *Wator) Next() error { // create new ocean n, err := createOcean() n, err := createPlayfield() if err != nil { panic(err) } // add water if err := n.populateWater(w); err != nil { if err := n.populateCells(w); err != nil { panic(err) } Loading Loading @@ -148,7 +148,7 @@ func (w *Wator) position(x int, y int) position { return position{nx, ny} } func fishNeighours(w *Wator, o *Ocean, p position) []position { func fishNeighours(w *Wator, o *Playfield, p position) []position { var eps []position for _, ap := range []position{ w.position(p.x+1, p.y), Loading @@ -156,14 +156,14 @@ func fishNeighours(w *Wator, o *Ocean, p position) []position { w.position(p.x, p.y+1), w.position(p.x, p.y-1), } { if o.isWater(ap) { if o.isCeller(ap) { eps = append(eps, ap) } } return eps } func sharkNeighbours(w *Wator, o *Ocean, p position) ([]position, []position) { func sharkNeighbours(w *Wator, o *Playfield, p position) ([]position, []position) { var fps []position // fish positions var eps []position // empty positions for _, ap := range []position{ Loading @@ -172,10 +172,10 @@ func sharkNeighbours(w *Wator, o *Ocean, p position) ([]position, []position) { w.position(p.x, p.y+1), w.position(p.x, p.y-1), } { if o.isWater(ap) { if o.isCeller(ap) { eps = append(eps, ap) } if o.isFish(ap) { if o.isFood(ap) { fps = append(fps, ap) } } Loading @@ -190,7 +190,7 @@ func randomNeighbour(ps []position) (position, bool) { return ps[rand.Intn(l)], true } func processFishes(w *Wator, c *Ocean, n *Ocean) error { func processFishes(w *Wator, c *Playfield, n *Playfield) error { for p, f := range c.Food { f.SetAge(f.Age() + 1) var np position Loading @@ -203,7 +203,7 @@ func processFishes(w *Wator, c *Ocean, n *Ocean) error { } // fish can only give birth when moved if (np != p) && (f.Age() >= w.BreedFish) { if (np != p) && (f.Age() >= w.BreedFood) { f.SetAge(0) fc := w.FoodCreator(0) n.Cells[p] = fc Loading @@ -217,7 +217,7 @@ func processFishes(w *Wator, c *Ocean, n *Ocean) error { return nil } func processSharks(w *Wator, c *Ocean, n *Ocean) error { func processSharks(w *Wator, c *Playfield, n *Playfield) error { for p, s := range c.Hunters { s.SetAge(s.Age() + 1) s.SetAte(s.Ate() + 1) Loading @@ -244,7 +244,7 @@ func processSharks(w *Wator, c *Ocean, n *Ocean) error { } // sharks can only give birth when moved if (np != p) && (s.Age() > w.BreedShark) { if (np != p) && (s.Age() > w.BreedHunter) { s.SetAte(0) sc := w.HunterCreator(0, 0) n.Cells[p] = sc Loading @@ -252,7 +252,7 @@ func processSharks(w *Wator, c *Ocean, n *Ocean) error { } // only move shark when not starved if s.Ate() < w.StarveShark { if s.Ate() < w.StarveHunter { n.Cells[np] = s n.Hunters[np] = s } Loading Loading
cmd/wator/main.go +10 −10 Original line number Diff line number Diff line Loading @@ -30,11 +30,11 @@ func main() { Debug: false, //true, Width: 72, //32, Height: 36, //14, InitialFishes: 400, //200, InitialSharks: 40, //20, BreedFish: 3, BreedShark: 30, StarveShark: 2, InitialFood: 400, //200, InitialHunters: 40, //20, BreedFood: 3, BreedHunter: 30, StarveHunter: 2, SleepMilliseconds: 50 * time.Millisecond, } Loading @@ -48,11 +48,11 @@ func main() { flag.BoolVar(&w.Debug, "debug", false, "enable debug mode") flag.IntVar(&w.Width, "width", w.Width, "wator width") flag.IntVar(&w.Height, "height", w.Height, "wator height") flag.IntVar(&w.InitialFishes, "fish-count", 400, "initial number of fish") flag.IntVar(&w.InitialSharks, "shark-count", 40, "initial number of sharks") flag.IntVar(&w.BreedFish, "fish-breed", 3, "chronos for fish to breed") flag.IntVar(&w.BreedShark, "shark-breed", 30, "chronos for sharks to breed") flag.IntVar(&w.StarveShark, "shark-starve", 2, "chronos for sharks to starve") flag.IntVar(&w.InitialFood, "fish-count", 400, "initial number of fish") flag.IntVar(&w.InitialHunters, "shark-count", 40, "initial number of sharks") flag.IntVar(&w.BreedFood, "fish-breed", 3, "chronos for fish to breed") flag.IntVar(&w.BreedHunter, "shark-breed", 30, "chronos for sharks to breed") flag.IntVar(&w.StarveHunter, "shark-starve", 2, "chronos for sharks to starve") //flag.IntVar(&w.MaxAgeFishes, "fish-max-age", ) flag.Parse() Loading
ocean.go +14 −14 Original line number Diff line number Diff line Loading @@ -4,22 +4,22 @@ import ( "math/rand" ) // Ocean is the container for fish and sharks. type Ocean struct { // Playfield is the container for fish and sharks. type Playfield struct { Cells map[position]Celler Food map[position]Food Hunters map[position]Hunter } // CreateOcean initializes a new ocean. func createOcean() (*Ocean, error) { o := &Ocean{Cells: make(map[position]Celler)} func createPlayfield() (*Playfield, error) { o := &Playfield{Cells: make(map[position]Celler)} o.Food = make(map[position]Food) o.Hunters = make(map[position]Hunter) return o, nil } func (o *Ocean) populateWater(w *Wator) error { func (o *Playfield) populateCells(w *Wator) error { for x := 0; x < w.Width; x++ { for y := 0; y < w.Height; y++ { p := position{x: x, y: y} Loading @@ -29,8 +29,8 @@ func (o *Ocean) populateWater(w *Wator) error { return nil } func (o *Ocean) populateFishes(w *Wator) error { for i := 0; i < w.InitialFishes; i++ { func (o *Playfield) populateFood(w *Wator) error { for i := 0; i < w.InitialFood; i++ { done := false for !done { x := rand.Intn(w.Width) Loading @@ -38,7 +38,7 @@ func (o *Ocean) populateFishes(w *Wator) error { p := position{x: x, y: y} switch o.Cells[p].(type) { case Celler: a := rand.Intn(w.BreedFish) a := rand.Intn(w.BreedFood) f := w.FoodCreator(a) o.Cells[p] = f o.Food[p] = f Loading @@ -49,8 +49,8 @@ func (o *Ocean) populateFishes(w *Wator) error { return nil } func (o *Ocean) populateSharks(w *Wator) error { for i := 0; i < w.InitialSharks; i++ { func (o *Playfield) populateHunters(w *Wator) error { for i := 0; i < w.InitialHunters; i++ { done := false for !done { x := rand.Intn(w.Width) Loading @@ -58,7 +58,7 @@ func (o *Ocean) populateSharks(w *Wator) error { p := position{x: x, y: y} switch o.Cells[p].(type) { case Celler: a := rand.Intn(w.BreedShark) a := rand.Intn(w.BreedHunter) s := w.HunterCreator(a, 0) o.Cells[p] = s o.Hunters[p] = s Loading @@ -69,7 +69,7 @@ func (o *Ocean) populateSharks(w *Wator) error { return nil } func (o *Ocean) isWater(p position) bool { func (o *Playfield) isCeller(p position) bool { r := false switch o.Cells[p].(type) { case Celler: Loading @@ -78,7 +78,7 @@ func (o *Ocean) isWater(p position) bool { return r } func (o *Ocean) isFish(p position) bool { func (o *Playfield) isFood(p position) bool { r := false switch o.Cells[p].(type) { case Food: Loading @@ -87,7 +87,7 @@ func (o *Ocean) isFish(p position) bool { return r } func (o *Ocean) isShark(p position) bool { func (o *Playfield) isHunter(p position) bool { r := false switch o.Cells[p].(type) { case Hunter: Loading
wator.go +26 −26 Original line number Diff line number Diff line Loading @@ -13,18 +13,18 @@ type Wator struct { Debug bool Width int Height int InitialFishes int InitialSharks int BreedFish int BreedShark int StarveShark int MaxAgeFishes int MaxAgeSharks int InitialFood int InitialHunters int BreedFood int BreedHunter int StarveHunter int MaxAgeFood int MaxAgeHunter int SleepMilliseconds time.Duration chronon int last *Ocean current *Ocean next *Ocean last *Playfield current *Playfield next *Playfield CellCreator func() Celler FoodCreator func(int) Food HunterCreator func(int, int) Hunter Loading @@ -50,23 +50,23 @@ func (w *Wator) Populate() error { panic("no hunter creator function given") } o, err := createOcean() o, err := createPlayfield() if err != nil { panic(err) } // fill with water if err := o.populateWater(w); err != nil { if err := o.populateCells(w); err != nil { panic(err) } // place fishes if err := o.populateFishes(w); err != nil { if err := o.populateFood(w); err != nil { panic(err) } // place sharks if err := o.populateSharks(w); err != nil { if err := o.populateHunters(w); err != nil { panic(err) } Loading Loading @@ -112,13 +112,13 @@ func (w *Wator) String() string { // Next caclculates the next generation of wator. func (w *Wator) Next() error { // create new ocean n, err := createOcean() n, err := createPlayfield() if err != nil { panic(err) } // add water if err := n.populateWater(w); err != nil { if err := n.populateCells(w); err != nil { panic(err) } Loading Loading @@ -148,7 +148,7 @@ func (w *Wator) position(x int, y int) position { return position{nx, ny} } func fishNeighours(w *Wator, o *Ocean, p position) []position { func fishNeighours(w *Wator, o *Playfield, p position) []position { var eps []position for _, ap := range []position{ w.position(p.x+1, p.y), Loading @@ -156,14 +156,14 @@ func fishNeighours(w *Wator, o *Ocean, p position) []position { w.position(p.x, p.y+1), w.position(p.x, p.y-1), } { if o.isWater(ap) { if o.isCeller(ap) { eps = append(eps, ap) } } return eps } func sharkNeighbours(w *Wator, o *Ocean, p position) ([]position, []position) { func sharkNeighbours(w *Wator, o *Playfield, p position) ([]position, []position) { var fps []position // fish positions var eps []position // empty positions for _, ap := range []position{ Loading @@ -172,10 +172,10 @@ func sharkNeighbours(w *Wator, o *Ocean, p position) ([]position, []position) { w.position(p.x, p.y+1), w.position(p.x, p.y-1), } { if o.isWater(ap) { if o.isCeller(ap) { eps = append(eps, ap) } if o.isFish(ap) { if o.isFood(ap) { fps = append(fps, ap) } } Loading @@ -190,7 +190,7 @@ func randomNeighbour(ps []position) (position, bool) { return ps[rand.Intn(l)], true } func processFishes(w *Wator, c *Ocean, n *Ocean) error { func processFishes(w *Wator, c *Playfield, n *Playfield) error { for p, f := range c.Food { f.SetAge(f.Age() + 1) var np position Loading @@ -203,7 +203,7 @@ func processFishes(w *Wator, c *Ocean, n *Ocean) error { } // fish can only give birth when moved if (np != p) && (f.Age() >= w.BreedFish) { if (np != p) && (f.Age() >= w.BreedFood) { f.SetAge(0) fc := w.FoodCreator(0) n.Cells[p] = fc Loading @@ -217,7 +217,7 @@ func processFishes(w *Wator, c *Ocean, n *Ocean) error { return nil } func processSharks(w *Wator, c *Ocean, n *Ocean) error { func processSharks(w *Wator, c *Playfield, n *Playfield) error { for p, s := range c.Hunters { s.SetAge(s.Age() + 1) s.SetAte(s.Ate() + 1) Loading @@ -244,7 +244,7 @@ func processSharks(w *Wator, c *Ocean, n *Ocean) error { } // sharks can only give birth when moved if (np != p) && (s.Age() > w.BreedShark) { if (np != p) && (s.Age() > w.BreedHunter) { s.SetAte(0) sc := w.HunterCreator(0, 0) n.Cells[p] = sc Loading @@ -252,7 +252,7 @@ func processSharks(w *Wator, c *Ocean, n *Ocean) error { } // only move shark when not starved if s.Ate() < w.StarveShark { if s.Ate() < w.StarveHunter { n.Cells[np] = s n.Hunters[np] = s } Loading