feat(grow): add grow method
This commit is contained in:
@@ -32,7 +32,7 @@ type Hamster struct {
|
|||||||
Name string
|
Name string
|
||||||
Number int
|
Number int
|
||||||
Sexe string
|
Sexe string
|
||||||
Age int
|
Age int // in days
|
||||||
Father *Hamster
|
Father *Hamster
|
||||||
Mother *Hamster
|
Mother *Hamster
|
||||||
HungerLevel int8
|
HungerLevel int8
|
||||||
@@ -44,6 +44,7 @@ type Hamster struct {
|
|||||||
Gestation bool
|
Gestation bool
|
||||||
GestationPeriod int8
|
GestationPeriod int8
|
||||||
GestationCooldown int8
|
GestationCooldown int8
|
||||||
|
GestationFather *Hamster
|
||||||
Child []*Hamster
|
Child []*Hamster
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -51,6 +52,24 @@ func (h *Hamster) Die() {
|
|||||||
h.Alive = false
|
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) {
|
func (h *Hamster) Fuck(another *Hamster) (bool, error) {
|
||||||
if h.Sexe == another.Sexe {
|
if h.Sexe == another.Sexe {
|
||||||
return false, errors.New("can't fuck together")
|
return false, errors.New("can't fuck together")
|
||||||
@@ -63,13 +82,14 @@ func (h *Hamster) Fuck(another *Hamster) (bool, error) {
|
|||||||
rand := randNumber(1, 100)
|
rand := randNumber(1, 100)
|
||||||
|
|
||||||
if rand <= GestationLuck {
|
if rand <= GestationLuck {
|
||||||
female := func() *Hamster {
|
female,male := func() (*Hamster,*Hamster) {
|
||||||
if h.Sexe == FEMALE {
|
if h.Sexe == FEMALE {
|
||||||
return h
|
return h,another
|
||||||
} else {
|
} else {
|
||||||
return another
|
return another,h
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
female.GestationFather = male
|
||||||
female.Gestation = true
|
female.Gestation = true
|
||||||
female.GestationPeriod = 0
|
female.GestationPeriod = 0
|
||||||
female.GestationCooldown = TotalGestationPeriod + GestationCooldown
|
female.GestationCooldown = TotalGestationPeriod + GestationCooldown
|
||||||
@@ -91,7 +111,7 @@ func Born(father *Hamster, mother *Hamster) ([]*Hamster, error) {
|
|||||||
|
|
||||||
numberOfChild := randNumberChild()
|
numberOfChild := randNumberChild()
|
||||||
child := make([]*Hamster, numberOfChild)
|
child := make([]*Hamster, numberOfChild)
|
||||||
for i := 1; i <= numberOfChild; i++ {
|
for i := 0; i < numberOfChild; i++ {
|
||||||
child[i] = &Hamster{
|
child[i] = &Hamster{
|
||||||
Name: fmt.Sprintf("Hamster %d", GlobalHamsterNumber),
|
Name: fmt.Sprintf("Hamster %d", GlobalHamsterNumber),
|
||||||
Number: GlobalHamsterNumber,
|
Number: GlobalHamsterNumber,
|
||||||
|
|||||||
@@ -39,6 +39,104 @@ func TestHamster_Die(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestHamster_Grow(t *testing.T) {
|
||||||
|
testCases := []struct {
|
||||||
|
caseName string
|
||||||
|
hamster *Hamster
|
||||||
|
expectedHamster *Hamster
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
caseName: "Age increment",
|
||||||
|
hamster: &Hamster{
|
||||||
|
Age: 0,
|
||||||
|
Alive: true,
|
||||||
|
},
|
||||||
|
expectedHamster: &Hamster{
|
||||||
|
Age: 1,
|
||||||
|
Alive: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
caseName: "Gestation decrement",
|
||||||
|
hamster: &Hamster{
|
||||||
|
Age: 0,
|
||||||
|
Gestation: true,
|
||||||
|
GestationPeriod: 1,
|
||||||
|
Alive: true,
|
||||||
|
},
|
||||||
|
expectedHamster: &Hamster{
|
||||||
|
Age: 1,
|
||||||
|
Gestation: true,
|
||||||
|
GestationPeriod: 2,
|
||||||
|
Alive: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
caseName: "Gestation Cooldown doesn't decrement",
|
||||||
|
hamster: &Hamster{
|
||||||
|
Age: 0,
|
||||||
|
Gestation: true,
|
||||||
|
GestationCooldown: 17,
|
||||||
|
GestationPeriod: 1,
|
||||||
|
Alive: true,
|
||||||
|
},
|
||||||
|
expectedHamster: &Hamster{
|
||||||
|
Age: 1,
|
||||||
|
GestationCooldown: 17,
|
||||||
|
Gestation: true,
|
||||||
|
GestationPeriod: 2,
|
||||||
|
Alive: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
caseName: "Gestation Cooldown decrement",
|
||||||
|
hamster: &Hamster{
|
||||||
|
Age: 0,
|
||||||
|
Gestation: false,
|
||||||
|
GestationCooldown: 17,
|
||||||
|
GestationPeriod: 0,
|
||||||
|
Alive: true,
|
||||||
|
},
|
||||||
|
expectedHamster: &Hamster{
|
||||||
|
Age: 1,
|
||||||
|
GestationCooldown: 16,
|
||||||
|
Gestation: false,
|
||||||
|
GestationPeriod: 0,
|
||||||
|
Alive: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
caseName: "Hamster die",
|
||||||
|
hamster: &Hamster{
|
||||||
|
Age: 365*3,
|
||||||
|
Gestation: false,
|
||||||
|
GestationCooldown: 17,
|
||||||
|
GestationPeriod: 0,
|
||||||
|
Alive: true,
|
||||||
|
},
|
||||||
|
expectedHamster: &Hamster{
|
||||||
|
Age: 365*3,
|
||||||
|
Alive:false,
|
||||||
|
GestationCooldown: 17,
|
||||||
|
Gestation: false,
|
||||||
|
GestationPeriod: 0,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tc := range testCases {
|
||||||
|
t.Run(tc.caseName, func(t *testing.T) {
|
||||||
|
if tc.hamster.Alive == !tc.expectedHamster.Alive {
|
||||||
|
randomizer.FakeRandomizer(364)
|
||||||
|
}
|
||||||
|
tc.hamster.Grow()
|
||||||
|
if !reflect.DeepEqual(tc.hamster,tc.expectedHamster) {
|
||||||
|
t.Errorf("Got %v expect %v", tc.hamster, tc.expectedHamster)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestHamster_Fuck(t *testing.T) {
|
func TestHamster_Fuck(t *testing.T) {
|
||||||
testCases := []struct {
|
testCases := []struct {
|
||||||
caseName string
|
caseName string
|
||||||
@@ -180,31 +278,6 @@ func TestHamster_Fuck(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestBorn(t *testing.T) {
|
|
||||||
type args struct {
|
|
||||||
father *Hamster
|
|
||||||
mother *Hamster
|
|
||||||
}
|
|
||||||
tests := []struct {
|
|
||||||
name string
|
|
||||||
args args
|
|
||||||
want []*Hamster
|
|
||||||
wantErr bool
|
|
||||||
}{
|
|
||||||
// TODO: Add test cases.
|
|
||||||
}
|
|
||||||
for _, tt := range tests {
|
|
||||||
got, err := Born(tt.args.father, tt.args.mother)
|
|
||||||
if (err != nil) != tt.wantErr {
|
|
||||||
t.Errorf("%q. Born() error = %v, wantErr %v", tt.name, err, tt.wantErr)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if !reflect.DeepEqual(got, tt.want) {
|
|
||||||
t.Errorf("%q. Born() = %v, want %v", tt.name, got, tt.want)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func Test_randSexe(t *testing.T) {
|
func Test_randSexe(t *testing.T) {
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
name string
|
name string
|
||||||
|
|||||||
Reference in New Issue
Block a user