package model import ( "errors" "fmt" uuid "github.com/satori/go.uuid" "hamster-tycoon/randomizer" ) const ( Full = iota AlmostFull Middle AlmostEmpty Empty ) const ( TotalGestationPeriod = 15 MinChild = 5 MaxChild = 9 GestationCooldown = (3 * 7) + 2 GestationMinAge = 10 * 6 MaxAge = 365 * 2 GestationLuck = 70 FEMALE = "F" MALE = "M" ) var GlobalHamsterNumber = 1 type Hamster struct { ID string Name string Number int Sexe string Age int // in days Father *Hamster Mother *Hamster HungerLevel int8 ThirstLevel int8 Weight float64 Height float64 Alive bool Selled bool Gestation bool GestationPeriod int8 GestationCooldown int8 GestationFather *Hamster Child []*Hamster } func (h *Hamster) Die() { h.Alive = false } func (h *Hamster) Grow() { if h.Age >= MaxAge { if randNumber(h.Age-MaxAge, 365) > 364 { h.Die() return } } h.Age++ if h.Gestation { h.GestationPeriod++ } else { if h.GestationCooldown > 0 { h.GestationCooldown-- } } fmt.Println(h.GestationCooldown) } func (h *Hamster) Fuck(another *Hamster) (bool, error) { if h.Sexe == another.Sexe { return false, errors.New("can't fuck together") } if h.Age < GestationMinAge || another.Age < GestationMinAge { return false, errors.New("one of the hamster is too young") } rand := randNumber(1, 100) if rand <= GestationLuck { female, male := func() (*Hamster, *Hamster) { if h.Sexe == FEMALE { return h, another } else { return another, h } }() female.GestationFather = male female.Gestation = true female.GestationPeriod = 0 female.GestationCooldown = TotalGestationPeriod + GestationCooldown } return rand <= GestationLuck, nil } func Born(father *Hamster, mother *Hamster) ([]*Hamster, error) { if !mother.Alive || mother.Selled { return nil, errors.New("the mother is not here") } if mother.GestationPeriod != TotalGestationPeriod { return nil, errors.New("not the time to make child") } mother.GestationCooldown = 0 mother.Gestation = false mother.GestationPeriod = 0 numberOfChild := randNumberChild() child := make([]*Hamster, numberOfChild) for i := 0; i < numberOfChild; i++ { child[i] = &Hamster{ ID: uuid.NewV4().String(), Name: fmt.Sprintf("Hamster %d", GlobalHamsterNumber), Number: GlobalHamsterNumber, Age: 1, Sexe: randSexe(), Father: father, Mother: mother, HungerLevel: Full, ThirstLevel: Full, Alive: true, Selled: false, Gestation: false, GestationPeriod: 0, GestationCooldown: 30, } } mother.Child = append(mother.Child, child...) father.Child = append(father.Child, child...) return child, nil } func randNumberChild() int { return randNumber(MinChild, MaxChild) } func randSexe() string { return func() string { if randNumber(1, 2) == 2 { return MALE } else { return FEMALE } }() } func randNumber(min int, max int) int { return min + randomizer.Rand(max) }