package main import ( "math/rand" "time" ) //Battle allow two character types to battle each other func Battle(hero, enemy Character) { speed := time.Second * 1 heroDamage := hero.Damage + hero.Weapons[hero.Gear.Weapon.Name].Damage enemyDamage := enemy.Damage + enemy.Weapons[enemy.Gear.Weapon.Name].Damage damage := 0 for { heroInitiative := rand.Intn(7) + hero.Initiative enemyInitiative := rand.Intn(7) + enemy.Initiative if heroInitiative == enemyInitiative { if flipCoin() == "heads" { heroInitiative-- //does the same as -= 1 } else { enemyInitiative-- } } 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" } //no need for 'else', this format is the Go style 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") print("Weapon: ", hero.Gear.Weapon.Name, "\t", "Weapon: ", enemy.Gear.Weapon.Name, "\n\n") }