Skip to content

Instantly share code, notes, and snippets.

@skyforce77
Created January 26, 2017 09:45
Show Gist options
  • Save skyforce77/e65b34bd739d3289b927eec47419ec41 to your computer and use it in GitHub Desktop.
Save skyforce77/e65b34bd739d3289b927eec47419ec41 to your computer and use it in GitHub Desktop.
package main
import "fmt"
type Monster struct {
name string
strengh int16
life int16
speed float32
}
func (entity *Monster) damage(damager *Monster) {
if entity.life >= damager.strengh {
entity.life -= damager.strengh
} else {
entity.life = 0
}
fmt.Println(damager.print(),"->",entity.print())
}
func (entity *Monster) isDead() bool {
return entity.life <= 0
}
func (entity *Monster) print() string {
return fmt.Sprint(entity.name,"[",entity.life,"]")
}
func battle(a *Monster, b *Monster) {
var counter int = 0
for !a.isDead() && !b.isDead() {
a.damage(b)
b.damage(a)
counter += 1
}
if a.isDead() {
fmt.Println(a.print(),"is dead");
}
if b.isDead() {
fmt.Println(b.print(),"is dead");
}
}
func main() {
var pikachu Monster = Monster{"pikachu",5,200,1.0}
var lucario Monster = Monster{"lucario",8,150,0.8}
battle(&pikachu, &lucario)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment