Loading ocean.go +16 −16 Original line number Diff line number Diff line Loading @@ -10,16 +10,16 @@ import ( // Ocean is the container for fish and sharks. type Ocean struct { cells map[position]Celler fishes map[position]*fish.Fish sharks map[position]*shark.Shark Cells map[position]Celler Food map[position]*fish.Fish Hunters map[position]*shark.Shark } // CreateOcean initializes a new ocean. func createOcean() (*Ocean, error) { o := &Ocean{cells: make(map[position]Celler)} o.fishes = make(map[position]*fish.Fish) o.sharks = make(map[position]*shark.Shark) o := &Ocean{Cells: make(map[position]Celler)} o.Food = make(map[position]*fish.Fish) o.Hunters = make(map[position]*shark.Shark) return o, nil } Loading @@ -27,7 +27,7 @@ func (o *Ocean) populateWater(w *Wator) error { for x := 0; x < w.Width; x++ { for y := 0; y < w.Height; y++ { p := position{x: x, y: y} o.cells[p] = &water.Water{} o.Cells[p] = &water.Water{} } } return nil Loading @@ -40,12 +40,12 @@ func (o *Ocean) populateFishes(w *Wator) error { x := rand.Intn(w.Width) y := rand.Intn(w.Height) p := position{x: x, y: y} switch o.cells[p].(type) { switch o.Cells[p].(type) { case *water.Water: a := rand.Intn(w.BreedFish) f := fish.New(a) o.cells[p] = f o.fishes[p] = f o.Cells[p] = f o.Food[p] = f done = true } } Loading @@ -60,12 +60,12 @@ func (o *Ocean) populateSharks(w *Wator) error { x := rand.Intn(w.Width) y := rand.Intn(w.Height) p := position{x: x, y: y} switch o.cells[p].(type) { switch o.Cells[p].(type) { case *water.Water: a := rand.Intn(w.BreedShark) s := shark.New(a) o.cells[p] = s o.sharks[p] = s o.Cells[p] = s o.Hunters[p] = s done = true } } Loading @@ -75,7 +75,7 @@ func (o *Ocean) populateSharks(w *Wator) error { func (o *Ocean) isWater(p position) bool { r := false switch o.cells[p].(type) { switch o.Cells[p].(type) { case *water.Water: r = true } Loading @@ -84,7 +84,7 @@ func (o *Ocean) isWater(p position) bool { func (o *Ocean) isFish(p position) bool { r := false switch o.cells[p].(type) { switch o.Cells[p].(type) { case *fish.Fish: r = true } Loading @@ -93,7 +93,7 @@ func (o *Ocean) isFish(p position) bool { func (o *Ocean) isShark(p position) bool { r := false switch o.cells[p].(type) { switch o.Cells[p].(type) { case *shark.Shark: r = true } Loading wator.go +17 −17 Original line number Diff line number Diff line Loading @@ -80,7 +80,7 @@ func (w *Wator) Display() error { for y := 0; y < w.Height; y++ { for x := 0; x < w.Width; x++ { p := position{x: x, y: y} s := fmt.Sprintf("%s", w.current.cells[p].String()) s := fmt.Sprintf("%s", w.current.Cells[p].String()) sb.WriteString(s) } sb.WriteString("\n") Loading @@ -91,8 +91,8 @@ func (w *Wator) Display() error { // String returns some information about current state. func (w *Wator) String() string { cf := len(w.current.fishes) cs := len(w.current.sharks) cf := len(w.current.Food) cs := len(w.current.Hunters) s := fmt.Sprintf("Chronon: %d - Fishes: %d - Sharks: %d", w.chronon, cf, cs) return s } Loading Loading @@ -179,7 +179,7 @@ func randomNeighbour(ps []position) (position, bool) { } func processFishes(w *Wator, c *Ocean, n *Ocean) error { for p, f := range c.fishes { for p, f := range c.Food { f.SetAge(f.Age() + 1) var np position eps := fishNeighours(w, c, p) // find empty neighbours Loading @@ -194,19 +194,19 @@ func processFishes(w *Wator, c *Ocean, n *Ocean) error { if (np != p) && (f.Age() >= w.BreedFish) { f.SetAge(0) fc := &fish.Fish{} n.cells[p] = fc n.fishes[p] = fc n.Cells[p] = fc n.Food[p] = fc } n.cells[np] = f n.fishes[np] = f n.Cells[np] = f n.Food[np] = f } return nil } func processSharks(w *Wator, c *Ocean, n *Ocean) error { for p, s := range c.sharks { for p, s := range c.Hunters { s.SetAge(s.Age() + 1) s.SetAte(s.Ate() + 1) var np position Loading @@ -215,10 +215,10 @@ func processSharks(w *Wator, c *Ocean, n *Ocean) error { if fMoved { // shark ate fish s.SetAte(0) c.cells[fp] = &water.Water{} n.cells[fp] = &water.Water{} delete(c.fishes, fp) delete(n.fishes, fp) c.Cells[fp] = &water.Water{} n.Cells[fp] = &water.Water{} delete(c.Food, fp) delete(n.Food, fp) np = fp } else { ep, eMoved := randomNeighbour(eps) Loading @@ -235,14 +235,14 @@ func processSharks(w *Wator, c *Ocean, n *Ocean) error { if (np != p) && (s.Age() > w.BreedShark) { s.SetAte(0) sc := &shark.Shark{} n.cells[p] = sc n.sharks[p] = sc n.Cells[p] = sc n.Hunters[p] = sc } // only move shark when not starved if s.Ate() < w.StarveShark { n.cells[np] = s n.sharks[np] = s n.Cells[np] = s n.Hunters[np] = s } } Loading Loading
ocean.go +16 −16 Original line number Diff line number Diff line Loading @@ -10,16 +10,16 @@ import ( // Ocean is the container for fish and sharks. type Ocean struct { cells map[position]Celler fishes map[position]*fish.Fish sharks map[position]*shark.Shark Cells map[position]Celler Food map[position]*fish.Fish Hunters map[position]*shark.Shark } // CreateOcean initializes a new ocean. func createOcean() (*Ocean, error) { o := &Ocean{cells: make(map[position]Celler)} o.fishes = make(map[position]*fish.Fish) o.sharks = make(map[position]*shark.Shark) o := &Ocean{Cells: make(map[position]Celler)} o.Food = make(map[position]*fish.Fish) o.Hunters = make(map[position]*shark.Shark) return o, nil } Loading @@ -27,7 +27,7 @@ func (o *Ocean) populateWater(w *Wator) error { for x := 0; x < w.Width; x++ { for y := 0; y < w.Height; y++ { p := position{x: x, y: y} o.cells[p] = &water.Water{} o.Cells[p] = &water.Water{} } } return nil Loading @@ -40,12 +40,12 @@ func (o *Ocean) populateFishes(w *Wator) error { x := rand.Intn(w.Width) y := rand.Intn(w.Height) p := position{x: x, y: y} switch o.cells[p].(type) { switch o.Cells[p].(type) { case *water.Water: a := rand.Intn(w.BreedFish) f := fish.New(a) o.cells[p] = f o.fishes[p] = f o.Cells[p] = f o.Food[p] = f done = true } } Loading @@ -60,12 +60,12 @@ func (o *Ocean) populateSharks(w *Wator) error { x := rand.Intn(w.Width) y := rand.Intn(w.Height) p := position{x: x, y: y} switch o.cells[p].(type) { switch o.Cells[p].(type) { case *water.Water: a := rand.Intn(w.BreedShark) s := shark.New(a) o.cells[p] = s o.sharks[p] = s o.Cells[p] = s o.Hunters[p] = s done = true } } Loading @@ -75,7 +75,7 @@ func (o *Ocean) populateSharks(w *Wator) error { func (o *Ocean) isWater(p position) bool { r := false switch o.cells[p].(type) { switch o.Cells[p].(type) { case *water.Water: r = true } Loading @@ -84,7 +84,7 @@ func (o *Ocean) isWater(p position) bool { func (o *Ocean) isFish(p position) bool { r := false switch o.cells[p].(type) { switch o.Cells[p].(type) { case *fish.Fish: r = true } Loading @@ -93,7 +93,7 @@ func (o *Ocean) isFish(p position) bool { func (o *Ocean) isShark(p position) bool { r := false switch o.cells[p].(type) { switch o.Cells[p].(type) { case *shark.Shark: r = true } Loading
wator.go +17 −17 Original line number Diff line number Diff line Loading @@ -80,7 +80,7 @@ func (w *Wator) Display() error { for y := 0; y < w.Height; y++ { for x := 0; x < w.Width; x++ { p := position{x: x, y: y} s := fmt.Sprintf("%s", w.current.cells[p].String()) s := fmt.Sprintf("%s", w.current.Cells[p].String()) sb.WriteString(s) } sb.WriteString("\n") Loading @@ -91,8 +91,8 @@ func (w *Wator) Display() error { // String returns some information about current state. func (w *Wator) String() string { cf := len(w.current.fishes) cs := len(w.current.sharks) cf := len(w.current.Food) cs := len(w.current.Hunters) s := fmt.Sprintf("Chronon: %d - Fishes: %d - Sharks: %d", w.chronon, cf, cs) return s } Loading Loading @@ -179,7 +179,7 @@ func randomNeighbour(ps []position) (position, bool) { } func processFishes(w *Wator, c *Ocean, n *Ocean) error { for p, f := range c.fishes { for p, f := range c.Food { f.SetAge(f.Age() + 1) var np position eps := fishNeighours(w, c, p) // find empty neighbours Loading @@ -194,19 +194,19 @@ func processFishes(w *Wator, c *Ocean, n *Ocean) error { if (np != p) && (f.Age() >= w.BreedFish) { f.SetAge(0) fc := &fish.Fish{} n.cells[p] = fc n.fishes[p] = fc n.Cells[p] = fc n.Food[p] = fc } n.cells[np] = f n.fishes[np] = f n.Cells[np] = f n.Food[np] = f } return nil } func processSharks(w *Wator, c *Ocean, n *Ocean) error { for p, s := range c.sharks { for p, s := range c.Hunters { s.SetAge(s.Age() + 1) s.SetAte(s.Ate() + 1) var np position Loading @@ -215,10 +215,10 @@ func processSharks(w *Wator, c *Ocean, n *Ocean) error { if fMoved { // shark ate fish s.SetAte(0) c.cells[fp] = &water.Water{} n.cells[fp] = &water.Water{} delete(c.fishes, fp) delete(n.fishes, fp) c.Cells[fp] = &water.Water{} n.Cells[fp] = &water.Water{} delete(c.Food, fp) delete(n.Food, fp) np = fp } else { ep, eMoved := randomNeighbour(eps) Loading @@ -235,14 +235,14 @@ func processSharks(w *Wator, c *Ocean, n *Ocean) error { if (np != p) && (s.Age() > w.BreedShark) { s.SetAte(0) sc := &shark.Shark{} n.cells[p] = sc n.sharks[p] = sc n.Cells[p] = sc n.Hunters[p] = sc } // only move shark when not starved if s.Ate() < w.StarveShark { n.cells[np] = s n.sharks[np] = s n.Cells[np] = s n.Hunters[np] = s } } Loading