trinket/battle.go

87 lines
1.9 KiB
Go

package main
import (
"math/rand"
"time"
)
type Character struct {
Name string
Health int
Damage int
Weapons [5]Weapon
Spell Spell
Initiative int
}
func Battle(hero, enemy Character) {
speed := time.Second * 1
heroDamage := hero.Damage + hero.Weapons[0].Damage
enemyDamage := enemy.Damage + enemy.Weapons[0].Damage
heroInitiative := rand.Intn(7) + hero.Initiative
enemyInitiative := rand.Intn(7) + enemy.Initiative
damage := 0
for {
printCombatantStats(&hero, &enemy, heroInitiative, enemyInitiative)
if heroInitiative >= enemyInitiative {
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()
}
}
func flipCoin() string {
if rand.Intn(10) < 5 {
return "heads"
} else {
return "tails"
}
}
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")
print("Initiative: ", heroInitiative, "\t", "Initiative: ", enemyInitiative, "\n\n")
}