func (m *Monster) simulate() { for { s := yulib.RandInt(0, 2) switch s { case 0: m.backe() case 1: m.iss() } time.Sleep(time.Second * 5) } }
// Erzeugt zufälliges Objekt func generateObject() Object { s := yulib.RandInt(0, 10) v := 0 switch { case s < 5: v = SMALL case s >= 5 && s < 8: v = MIDDLE case s >= 8: v = BIG } result := Object{ Value: v, } return result }
func (m *Monster) iss() { n := yulib.RandInt(1, MAGEN_MAX) fmt.Println("Monster <" + m.name + "> : moechte " + Itoa(n) + " Kekse essen") for i := 0; i < n; i++ { timeout := time.After(5 * time.Second) select { case <-keksdose: time.Sleep(time.Second / 4) // iss! PrintKeksdose() case <-timeout: fmt.Println("Monster <" + m.name + "> wartet zu lange.. Bricht ab..") return } } fmt.Println("Monster <" + m.name + "> ist satt") }
func (m *Monster) backe() { fmt.Println("Monster <" + m.name + "> : starte backen..") // Erzeuge zufällig viele Kekse kekseAnzahl := yulib.RandInt(3, OFEN_MAX) time.Sleep(time.Second / 2) // backen.. fmt.Println("Monster <" + m.name + "> : backen beendet [" + Itoa(kekseAnzahl) + "]") for i := 0; i < kekseAnzahl; i++ { timeout := time.After(5 * time.Second) select { case keksdose <- true: // Füge Kekse in die Keksdose ein PrintKeksdose() case <-timeout: fmt.Println("Monster <" + m.name + "> : wartet zu lange, die Kekse sind kalt...") return } } }