package main import ( "fmt" "math/rand" "time" ) type Game struct { Welcome string Health int CurrentLocation *Location } func (g *Game) Play() { fmt.Println(g.Welcome) for { fmt.Println(g.CurrentLocation.Description) g.ProcessEvents(g.CurrentLocation.Events) if g.Health <= 0 { fmt.Println("You are dead, game over!!!") return } fmt.Printf("Health: %d\n", g.Health) fmt.Println("You can go to these places:") for index, loc := range g.CurrentLocation.Transitions { fmt.Printf("\t%s - %s\n", index, loc.Name) } var name string fmt.Print("Where do you want to go? (0 - to quit) ") _, err := fmt.Scan(&name) if err != nil { fmt.Println(err); return } var newLoc *Location if v, ok := g.CurrentLocation.Transitions[name]; ok { newLoc = v } else if name == "0" { return } if newLoc != nil { g.CurrentLocation = newLoc } } } func (g *Game) ProcessEvents(events []string) { for _, evtName := range events { g.Health += evts[evtName].ProcessEvent() } } type Event struct { Type string Chance int Description string Health int Evt string } func (e *Event) ProcessEvent() int { s1 := rand.NewSource(time.Now().UnixNano()) r1 := rand.New(s1) if e.Chance >= r1.Intn(100) { hp := e.Health if e.Type == "Combat" { fmt.Println("Combat Event") } fmt.Printf("\t%s\n", e.Description) if e.Evt != "" { hp = hp + evts[e.Evt].ProcessEvent() } return hp } return 0 } type Location struct { Name string Description string Transitions map[string]*Location Events []string } var evts = map[string]*Event{ "alienAttack": {Type: "Combat", Chance: 20, Description: "An alien beams in front of you and shoots you with a ray gun.", Health: -50, Evt: "doctorTreatment"}, "doctorTreatment": {Type: "Story", Chance: 10, Description: "The doctor rushes in and inject you with a health boost.", Health: +30, Evt: ""}, "android": {Type: "Story", Chance: 50, Description: "Data is in the turbo lift and says hi to you", Health: 0, Evt: ""}, "relaxing": {Type: "Story", Chance: 100, Description: "In the lounge you are so relaxed that your health improves.", Health: +10, Evt: ""}, } var bridge *Location func init() { bridge = &Location{ Name: "Bridge", Description: "You are on the bridge of a spaceship sitting in the Captain's chair.", Events: []string{"alienAttack"}, } rr := &Location{ Name: "Ready Room", Description: "The Captain's ready room.", Transitions: map[string]*Location{"bridge": bridge}, Events: []string{"doctorTreatment"}} bridge.Transitions = map[string]*Location{"ready": rr} bridge.Transitions["lift"] = &Location{ Name: "Turbo Lift", Description: "A Turbo Lift that takes you anywhere in the ship.", Events: []string{"android"}, } bridge.Transitions["lift"].Transitions = map[string]*Location{ "bridge": bridge, "engineering": { Name: "Engineering", Description: "You are in engineering where you see the star drive", Events: []string{"alienAttack"}, Transitions: map[string]*Location{"lift": bridge.Transitions["lift"]}, }, "lounge": { Name: "Lounge", Description: "You are in the lounge, you feel very relaxed", Events: []string{"relaxing"}, Transitions: map[string]*Location{"lift": bridge.Transitions["lift"]}, }, } } func main() { g := &Game{ Health: 100, Welcome: "Welcome to the Starship Enterprise\n\n", CurrentLocation: bridge, } g.Play() }