package main
import (
"fmt"
"math/rand"
"time"
)
type action interface {
move() string
eat() string
}
type Animal string
type cat struct {
name string
Animal
}
type dog struct {
name string
Animal
}
type pig struct {
name string
Animal
}
const hours int = 18
func printAction(action action) {
ran := rand.Intn(2)
if ran == 0 {
fmt.Println(action.move())
} else {
fmt.Println(action.eat())
}
}
func (a Animal) String() string {
return fmt.Sprintf("The animal is " + string(a))
}
func (a Animal) move() string {
actions := []string{"playing", "running", "stupefying"}
ran := rand.Intn(3)
return fmt.Sprintf("The %v is %v.", string(a), actions[ran])
}
func (a Animal) eat() string {
food := []string{"apple", "banana", "orange", "peer"}
ran := rand.Intn(4)
return fmt.Sprintf("The %v is eatting %v", string(a), food[ran])
}
func main() {
for i := 1; i <= hours; i++ {
fmt.Println("It's", i, "o'clock")
animals := []string{"cat", "dog", "pig"}
ran1 := rand.Intn(100) % 3
if i <= hours/2 {
switch animals[ran1] {
case "cat":
myAnimal := cat{name: "myCat", Animal: "cat"}
fmt.Println("the", myAnimal, "named", myAnimal.name)
printAction(myAnimal)
case "dog":
myAnimal := dog{name: "myDog", Animal: "dog"}
fmt.Println("the", myAnimal, "named", myAnimal.name)
printAction(myAnimal)
case "pig":
myAnimal := pig{name: "myPig", Animal: "pig"}
fmt.Println("the", myAnimal, "named", myAnimal.name)
printAction(myAnimal)
}
} else {
fmt.Println(animals[ran1], "is sleeping.")
}
if i == hours {
i = 0
}
fmt.Println()
time.Sleep(time.Duration(2) * time.Second)
}
}