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

moved module usage to command, and use only interfaces internally

parent 5251bde1
Loading
Loading
Loading
Loading
+15 −0
Original line number Diff line number Diff line
@@ -8,6 +8,9 @@ import (

	"golang.org/x/crypto/ssh/terminal"
	"repositories.muehmer.net/bsmrgo/wator"
	"repositories.muehmer.net/bsmrgo/wator/cell/fish"
	"repositories.muehmer.net/bsmrgo/wator/cell/shark"
	"repositories.muehmer.net/bsmrgo/wator/cell/water"
)

func main() {
@@ -53,6 +56,18 @@ func main() {
	//flag.IntVar(&w.MaxAgeFishes, "fish-max-age", )
	flag.Parse()

	w.CellCreator = func() wator.Celler {
		return water.New()
	}

	w.FoodCreator = func(age int) wator.Food {
		return fish.New(age)
	}

	w.HunterCreator = func(age, ate int) wator.Hunter {
		return shark.New(age, ate)
	}

	initialize(w)
	print(w)
	repl(w)
+3 −7
Original line number Diff line number Diff line
@@ -2,10 +2,6 @@ package wator

import (
	"math/rand"

	"repositories.muehmer.net/bsmrgo/wator/cell/fish"
	"repositories.muehmer.net/bsmrgo/wator/cell/shark"
	"repositories.muehmer.net/bsmrgo/wator/cell/water"
)

// Ocean is the container for fish and sharks.
@@ -27,7 +23,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.New()
			o.Cells[p] = w.CellCreator()
		}
	}
	return nil
@@ -43,7 +39,7 @@ func (o *Ocean) populateFishes(w *Wator) error {
			switch o.Cells[p].(type) {
			case Celler:
				a := rand.Intn(w.BreedFish)
				f := fish.New(a)
				f := w.FoodCreator(a)
				o.Cells[p] = f
				o.Food[p] = f
				done = true
@@ -63,7 +59,7 @@ func (o *Ocean) populateSharks(w *Wator) error {
			switch o.Cells[p].(type) {
			case Celler:
				a := rand.Intn(w.BreedShark)
				s := shark.New(a, 0)
				s := w.HunterCreator(a, 0)
				o.Cells[p] = s
				o.Hunters[p] = s
				done = true
+20 −8
Original line number Diff line number Diff line
@@ -6,10 +6,6 @@ import (
	"os"
	"strings"
	"time"

	"repositories.muehmer.net/bsmrgo/wator/cell/fish"
	"repositories.muehmer.net/bsmrgo/wator/cell/shark"
	"repositories.muehmer.net/bsmrgo/wator/cell/water"
)

// Wator is the simulation for the planet wator.
@@ -29,6 +25,9 @@ type Wator struct {
	last              *Ocean
	current           *Ocean
	next              *Ocean
	CellCreator       func() Celler
	FoodCreator       func(int) Food
	HunterCreator     func(int, int) Hunter
}

type position struct {
@@ -38,6 +37,19 @@ type position struct {

// Populate initializes the ocean.
func (w *Wator) Populate() error {

	if w.CellCreator == nil {
		panic("no cell creator function given")
	}

	if w.FoodCreator == nil {
		panic("no food creator function given")
	}

	if w.HunterCreator == nil {
		panic("no hunter creator function given")
	}

	o, err := createOcean()
	if err != nil {
		panic(err)
@@ -193,7 +205,7 @@ func processFishes(w *Wator, c *Ocean, n *Ocean) error {
		// fish can only give birth when moved
		if (np != p) && (f.Age() >= w.BreedFish) {
			f.SetAge(0)
			fc := &fish.Fish{}
			fc := w.FoodCreator(0)
			n.Cells[p] = fc
			n.Food[p] = fc
		}
@@ -215,8 +227,8 @@ 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{}
			c.Cells[fp] = w.CellCreator()
			n.Cells[fp] = w.CellCreator()
			delete(c.Food, fp)
			delete(n.Food, fp)
			np = fp
@@ -234,7 +246,7 @@ func processSharks(w *Wator, c *Ocean, n *Ocean) error {
		// sharks can only give birth when moved
		if (np != p) && (s.Age() > w.BreedShark) {
			s.SetAte(0)
			sc := &shark.Shark{}
			sc := w.HunterCreator(0, 0)
			n.Cells[p] = sc
			n.Hunters[p] = sc
		}