Created
October 11, 2016 02:04
-
-
Save TheTechmage/8c71ff61d52adeeb19f7d54dc9bb957d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"log" | |
"os" | |
"strconv" | |
) | |
type Ninja struct { | |
Name string | |
Health int64 | |
Verbose bool | |
} | |
func (n *Ninja) SetName(name string) { | |
n.Name = name | |
} | |
func (n *Ninja) SetHealth(h int64) { | |
n.Health = h | |
} | |
func (n *Ninja) SetHealthS(h string) error { | |
health, err := strconv.ParseInt(h, 10, 0) | |
if err != nil { | |
return err | |
} | |
n.SetHealth(health) | |
return nil | |
} | |
func (n *Ninja) Damage(amount int64) { | |
n.Health = n.Health - amount | |
} | |
func (n *Ninja) Heal(amount int64) { | |
n.Health = n.Health + amount | |
} | |
func (n *Ninja) say(s string) { | |
fmt.Printf("%s: %s\n", n.Name, s) | |
} | |
func (n *Ninja) Stats() { | |
fmt.Printf("\tName: %s\n\t-> Health: %d\n", n.Name, n.Health) | |
} | |
func (n *Ninja) end() bool { | |
if n.Health <= 0 { | |
n.Stats() | |
fmt.Println("You cannot go further, your HP reached 0!") | |
return false | |
} | |
if n.Verbose { | |
n.Stats() | |
} | |
return true | |
} | |
func (n *Ninja) Shout() bool { | |
n.say("I am hiding, huehuehue!") | |
n.Damage(5) | |
return n.end() | |
} | |
func (n *Ninja) Eat() bool { | |
n.say("I am eating! Nom nom nom!") | |
n.Heal(10) | |
return n.end() | |
} | |
func (n *Ninja) Fight() bool { | |
n.say("Yee Samurai, I'll get you! Kyah!") | |
n.Damage(20) | |
return n.end() | |
} | |
func (n *Ninja) Conquer() bool { | |
n.say("Yee castle is now mine!") | |
n.Damage(30) | |
return n.end() | |
} | |
func setNinjaHealth(n *Ninja, s *string) { | |
for { | |
fmt.Print("Ninja health: ") | |
fmt.Scanln(s) | |
err := n.SetHealthS(*s) | |
if err != nil { | |
log.Println(err) | |
} else { | |
break | |
} | |
} | |
} | |
func main() { | |
var ninja Ninja | |
ninja.Verbose = true | |
var input string | |
fmt.Print("Ninja name: ") | |
fmt.Scanln(&input) | |
ninja.SetName(input) | |
setNinjaHealth(&ninja, &input) | |
if !ninja.Shout() { | |
os.Exit(1) | |
} | |
if !ninja.Eat() { | |
os.Exit(1) | |
} | |
if !ninja.Eat() { | |
os.Exit(1) | |
} | |
if !ninja.Fight() { | |
os.Exit(1) | |
} | |
if !ninja.Shout() { | |
os.Exit(1) | |
} | |
if !ninja.Eat() { | |
os.Exit(1) | |
} | |
if !ninja.Conquer() { | |
os.Exit(1) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment