Created
January 26, 2017 09:45
-
-
Save skyforce77/e65b34bd739d3289b927eec47419ec41 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" | |
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