From 86d1316ff100815107518ab905c9e2e6f5a6415d Mon Sep 17 00:00:00 2001 From: Logen Kain Date: Mon, 9 Oct 2017 01:41:59 -0700 Subject: [PATCH] Added fighting; adjusted weapon stats --- battle.go | 88 ++++++++++++++++++++++++++++++++++++++++-------------- main.go | 22 +++++++------- weapons.go | 2 +- 3 files changed, 78 insertions(+), 34 deletions(-) diff --git a/battle.go b/battle.go index bf67b12..aa5d144 100644 --- a/battle.go +++ b/battle.go @@ -1,42 +1,86 @@ package main import ( - "fmt" "math/rand" "time" ) type Character struct { - Name string - Health int - Damage int - Weapons [5]Weapon - Spell Spell + Name string + Health int + Damage int + Weapons [5]Weapon + Spell Spell + Initiative int } func Battle(hero, enemy Character) { - speed := time.Second + speed := time.Second * 1 - fmt.Println("Random Int: ", rand.Intn(11)) - time.Sleep(speed) - print("Hero: ", hero.Name, "\n") - print("Enemy: ", enemy.Name, "\n") + heroDamage := hero.Damage + hero.Weapons[0].Damage + enemyDamage := enemy.Damage + enemy.Weapons[0].Damage - if spellOrAttack() == "spell" { - print("Spell Damage: ", hero.Spell.Damage, "\n") - } else { - print("Attack Damage: ", hero.Damage, "\n") - } + 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() - if enemy.Name == "Goblin" { - print("This is a goblin character\n") } } -func spellOrAttack() string { - if rand.Intn(2) == 1 { - return "spell" +func flipCoin() string { + if rand.Intn(10) < 5 { + return "heads" } 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") +} diff --git a/main.go b/main.go index 7c66d91..797d7a0 100644 --- a/main.go +++ b/main.go @@ -1,7 +1,6 @@ package main import ( - "fmt" "math/rand" "os" "os/exec" @@ -12,18 +11,20 @@ import ( var ( player = Character{ - Name: "Daniel", - Health: 100, - Damage: 60, - Spell: firearrow, - Weapons: [5]Weapon{Broadsword}} + Name: "Daniel", + Health: 100, + Damage: 30, + Spell: firearrow, + Weapons: [5]Weapon{Broadsword}, + Initiative: 4} goblin = Character{ - Name: "Goblin", - Health: 100, - Damage: 20, - Weapons: [5]Weapon{GoblinClaw}} + Name: "Goblin", + Health: 100, + Damage: 20, + Weapons: [5]Weapon{GoblinClaw}, + Initiative: 3} firearrow = Spell{ Name: "Fire Arrow", @@ -36,7 +37,6 @@ var ( func main() { initGame() Battle(player, goblin) - fmt.Println(goblin.Weapons[0].Damage) } func initGame() { diff --git a/weapons.go b/weapons.go index 99b7171..eda9753 100644 --- a/weapons.go +++ b/weapons.go @@ -9,7 +9,7 @@ type Weapon struct { var ( Broadsword = Weapon{ Name: "Broadsword", - Damage: 25, + Damage: 3, Active: false} GoblinClaw = Weapon{