diff --git a/battle.go b/battle.go index bea9a45..aa5d144 100644 --- a/battle.go +++ b/battle.go @@ -1,45 +1,86 @@ package main import ( - "fmt" "math/rand" "time" ) type Character struct { - Name string - Health int - Damage int - Spell Spell + 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 -func Battle(hero, enemy Character, speed time.Duration) { - speed *= time.Second + heroInitiative := rand.Intn(7) + hero.Initiative + enemyInitiative := rand.Intn(7) + enemy.Initiative - rand.Seed(time.Now().Unix()) - fmt.Println("Random Int: ",rand.Intn(11)) - //time.Sleep(speed) - print("Hero: ",hero.Name, "\n") - print("Enemy: ",enemy.Name, "\n") + damage := 0 - if spellOrAttack() == "spell"{ - print("Spell Damage: ",hero.Spell.Damage, "\n") - }else{ - print("Attack Damage: ", hero.Damage, "\n") - } + for { + printCombatantStats(&hero, &enemy, heroInitiative, enemyInitiative) - if enemy.Name == "Goblin" { - print("This is a goblin character\n") - } -} + 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") -func spellOrAttack() string { - if rand.Intn(2) == 1{ - return "spell" - }else{ - return "attack" + if hero.Health > 0 { + damage = rand.Intn(heroDamage) + enemy.Health -= damage + print(enemy.Name, " was hit for ", damage, " damage!\n") + } } -} \ No newline at end of file + + 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") +} diff --git a/characters.go b/characters.go new file mode 100644 index 0000000..ead64e2 --- /dev/null +++ b/characters.go @@ -0,0 +1,20 @@ +package main + +var ( + player = Character{ + + 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}, + Initiative: 3} +) diff --git a/magic.go b/magic.go index 3352fbe..672f6f5 100644 --- a/magic.go +++ b/magic.go @@ -2,10 +2,18 @@ package main //Spell contains all the magic spells type Spell struct { - Name string + Name string Damage int - Cost int - Level int + Cost int + Level int Active bool - } + +var ( + firearrow = Spell{ + Name: "Fire Arrow", + Damage: 50, + Cost: 10, + Level: 1, + Active: true} +) diff --git a/main.go b/main.go index 9ad68fd..bf593f5 100644 --- a/main.go +++ b/main.go @@ -1,29 +1,63 @@ package main - - -//Characters -var ( - player = Character{ - - Name: "Daniel", - Health: 100, - Damage: 60, - Spell: firearrow} - - goblin = Character{ - - Name: "Goblin", - Health: 100, - Damage: 20} - - firearrow = Spell{ - Name: "Fire Arrow", - Damage: 50, - Cost: 10, - Level: 1, - Active: true} +import ( + "bufio" + "fmt" + "math/rand" + "os" + "os/exec" + "time" ) -func main(){ - Battle(player, goblin, 1) -} \ No newline at end of file + +func main() { + initGame() + var answer string + + for { + print("Type 'q' to quit\n") + answer, _ = input("Would you like to fight a goblin? y/n ") + + if answer == "n" { + print("What a pussy! \n") + time.Sleep(time.Second) + ClearScreen() + } else if answer == "y" { + print("Sweeeeeet! \n") + time.Sleep(time.Second) + ClearScreen() + Battle(player, goblin) + } else if answer == "q" { + ClearScreen() + print("Ta!\n") + break + } else { + print("Try again, \n") + time.Sleep(time.Second) + ClearScreen() + } + + } +} + +func initGame() { + rand.Seed(time.Now().UnixNano()) + ClearScreen() +} + +func ClearScreen() { + cmd := exec.Command("clear") + cmd.Stdout = os.Stdout + cmd.Run() +} + +func input(s string) (string, error) { + stdin_buf := bufio.NewReader(os.Stdin) + + fmt.Print(s) + + text, err := stdin_buf.ReadString('\n') + if err != nil { + panic(err) + } + return text[:len(text)-1], err +} diff --git a/weapons.go b/weapons.go new file mode 100644 index 0000000..eda9753 --- /dev/null +++ b/weapons.go @@ -0,0 +1,18 @@ +package main + +type Weapon struct { + Name string + Damage int + Active bool +} + +var ( + Broadsword = Weapon{ + Name: "Broadsword", + Damage: 3, + Active: false} + + GoblinClaw = Weapon{ + Name: "Goblin Claw", + Damage: 5} +)