Commit c3b08951 authored by Boris Mühmer's avatar Boris Mühmer
Browse files

changed naming

parent 76bf4ce7
Loading
Loading
Loading
Loading
+16 −16
Original line number Diff line number Diff line
@@ -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
}

@@ -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
@@ -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
			}
		}
@@ -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
			}
		}
@@ -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
	}
@@ -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
	}
@@ -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
	}
+17 −17
Original line number Diff line number Diff line
@@ -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")
@@ -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
}
@@ -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
@@ -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
@@ -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)
@@ -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
		}
	}