feat(grow): add grow method
This commit is contained in:
@@ -32,7 +32,7 @@ type Hamster struct {
|
||||
Name string
|
||||
Number int
|
||||
Sexe string
|
||||
Age int
|
||||
Age int // in days
|
||||
Father *Hamster
|
||||
Mother *Hamster
|
||||
HungerLevel int8
|
||||
@@ -44,6 +44,7 @@ type Hamster struct {
|
||||
Gestation bool
|
||||
GestationPeriod int8
|
||||
GestationCooldown int8
|
||||
GestationFather *Hamster
|
||||
Child []*Hamster
|
||||
}
|
||||
|
||||
@@ -51,6 +52,24 @@ 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")
|
||||
@@ -63,13 +82,14 @@ func (h *Hamster) Fuck(another *Hamster) (bool, error) {
|
||||
rand := randNumber(1, 100)
|
||||
|
||||
if rand <= GestationLuck {
|
||||
female := func() *Hamster {
|
||||
female,male := func() (*Hamster,*Hamster) {
|
||||
if h.Sexe == FEMALE {
|
||||
return h
|
||||
return h,another
|
||||
} else {
|
||||
return another
|
||||
return another,h
|
||||
}
|
||||
}()
|
||||
female.GestationFather = male
|
||||
female.Gestation = true
|
||||
female.GestationPeriod = 0
|
||||
female.GestationCooldown = TotalGestationPeriod + GestationCooldown
|
||||
@@ -91,7 +111,7 @@ func Born(father *Hamster, mother *Hamster) ([]*Hamster, error) {
|
||||
|
||||
numberOfChild := randNumberChild()
|
||||
child := make([]*Hamster, numberOfChild)
|
||||
for i := 1; i <= numberOfChild; i++ {
|
||||
for i := 0; i < numberOfChild; i++ {
|
||||
child[i] = &Hamster{
|
||||
Name: fmt.Sprintf("Hamster %d", GlobalHamsterNumber),
|
||||
Number: GlobalHamsterNumber,
|
||||
|
||||
Reference in New Issue
Block a user