Added fighting; adjusted weapon stats

This commit is contained in:
Logen Kain 2017-10-09 01:41:59 -07:00
parent 3f438abb68
commit 86d1316ff1
3 changed files with 78 additions and 34 deletions

View File

@ -1,7 +1,6 @@
package main package main
import ( import (
"fmt"
"math/rand" "math/rand"
"time" "time"
) )
@ -12,31 +11,76 @@ type Character struct {
Damage int Damage int
Weapons [5]Weapon Weapons [5]Weapon
Spell Spell Spell Spell
Initiative int
} }
func Battle(hero, enemy Character) { func Battle(hero, enemy Character) {
speed := time.Second 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")
}
}
fmt.Println("Random Int: ", rand.Intn(11))
time.Sleep(speed) time.Sleep(speed)
print("Hero: ", hero.Name, "\n")
print("Enemy: ", enemy.Name, "\n")
if spellOrAttack() == "spell" { if enemy.Health <= 0 || hero.Health <= 0 {
print("Spell Damage: ", hero.Spell.Damage, "\n") ClearScreen()
printCombatantStats(&hero, &enemy, heroInitiative, enemyInitiative)
print("It's over\n")
if enemy.Health <= 0 {
print("The ", enemy.Name, " is dead!\n")
} else { } else {
print("Attack Damage: ", hero.Damage, "\n") print("Rest in peace ", hero.Name, "\n")
} }
if enemy.Name == "Goblin" { break
print("This is a goblin character\n") }
ClearScreen()
} }
} }
func spellOrAttack() string { func flipCoin() string {
if rand.Intn(2) == 1 { if rand.Intn(10) < 5 {
return "spell" return "heads"
} else { } else {
return "attack" 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")
}

10
main.go
View File

@ -1,7 +1,6 @@
package main package main
import ( import (
"fmt"
"math/rand" "math/rand"
"os" "os"
"os/exec" "os/exec"
@ -14,16 +13,18 @@ var (
Name: "Daniel", Name: "Daniel",
Health: 100, Health: 100,
Damage: 60, Damage: 30,
Spell: firearrow, Spell: firearrow,
Weapons: [5]Weapon{Broadsword}} Weapons: [5]Weapon{Broadsword},
Initiative: 4}
goblin = Character{ goblin = Character{
Name: "Goblin", Name: "Goblin",
Health: 100, Health: 100,
Damage: 20, Damage: 20,
Weapons: [5]Weapon{GoblinClaw}} Weapons: [5]Weapon{GoblinClaw},
Initiative: 3}
firearrow = Spell{ firearrow = Spell{
Name: "Fire Arrow", Name: "Fire Arrow",
@ -36,7 +37,6 @@ var (
func main() { func main() {
initGame() initGame()
Battle(player, goblin) Battle(player, goblin)
fmt.Println(goblin.Weapons[0].Damage)
} }
func initGame() { func initGame() {

View File

@ -9,7 +9,7 @@ type Weapon struct {
var ( var (
Broadsword = Weapon{ Broadsword = Weapon{
Name: "Broadsword", Name: "Broadsword",
Damage: 25, Damage: 3,
Active: false} Active: false}
GoblinClaw = Weapon{ GoblinClaw = Weapon{