trinket/battle.go

86 lines
2.0 KiB
Go
Raw Normal View History

2017-10-08 22:17:53 -07:00
package main
import (
"math/rand"
"time"
)
func Battle(hero, enemy Character) {
2017-10-09 01:41:59 -07:00
speed := time.Second * 1
2017-10-08 22:17:53 -07:00
2017-10-11 06:30:53 -07:00
heroDamage := hero.Damage + hero.Weapons[hero.Gear.Weapon.Name].Damage
enemyDamage := enemy.Damage + enemy.Weapons[enemy.Gear.Weapon.Name].Damage
2017-10-08 22:17:53 -07:00
2017-10-09 01:41:59 -07:00
damage := 0
for {
heroInitiative := rand.Intn(7) + hero.Initiative
enemyInitiative := rand.Intn(7) + enemy.Initiative
if heroInitiative == enemyInitiative {
if flipCoin() == "heads" {
heroInitiative -= 1
} else {
enemyInitiative -= 1
}
}
2017-10-09 01:41:59 -07:00
printCombatantStats(&hero, &enemy, heroInitiative, enemyInitiative)
2017-10-11 06:30:53 -07:00
if heroInitiative > enemyInitiative {
2017-10-09 01:41:59 -07:00
damage = rand.Intn(heroDamage)
enemy.Health -= damage
print(enemy.Name, " was hit for ", damage, " damage!\n")
if enemy.Health > 0 {
damage = rand.Intn(enemyDamage)
hero.Health -= damage
print(hero.Name, " was hit for ", damage, " damage!\n")
}
} else {
damage = rand.Intn(enemyDamage)
hero.Health -= damage
print(hero.Name, " was hit for ", damage, " damage!\n")
if hero.Health > 0 {
damage = rand.Intn(heroDamage)
enemy.Health -= damage
print(enemy.Name, " was hit for ", damage, " damage!\n")
}
}
time.Sleep(speed)
if enemy.Health <= 0 || hero.Health <= 0 {
ClearScreen()
printCombatantStats(&hero, &enemy, heroInitiative, enemyInitiative)
print("It's over\n")
if enemy.Health <= 0 {
print("The ", enemy.Name, " is dead!\n")
} else {
print("Rest in peace ", hero.Name, "\n")
}
break
}
ClearScreen()
2017-10-08 22:17:53 -07:00
}
}
2017-10-09 01:41:59 -07:00
func flipCoin() string {
if rand.Intn(10) < 5 {
return "heads"
2017-10-09 00:26:36 -07:00
} else {
2017-10-09 01:41:59 -07:00
return "tails"
2017-10-09 00:26:36 -07:00
}
}
2017-10-09 01:41:59 -07:00
func printCombatantStats(hero, enemy *Character, heroInitiative, enemyInitiative int) {
print("Hero: ", hero.Name, "\t", "Enemy: ", enemy.Name, "\n")
print("Health: ", hero.Health, "\t", "Health: ", enemy.Health, "\n")
2017-10-11 06:30:53 -07:00
print("Initiative: ", heroInitiative, "\t", "Initiative: ", enemyInitiative, "\n")
print("Weapon: ", hero.Gear.Weapon.Name, "\t", "Weapon: ", enemy.Gear.Weapon.Name, "\n\n")
2017-10-09 01:41:59 -07:00
}