Merge branch 'master' into 'master'

Refactor; battle; added weapons

See merge request mollusk/trinket!1
This commit is contained in:
Mollusk 2017-10-11 11:48:26 +00:00
commit 3630326f44
5 changed files with 178 additions and 57 deletions

View File

@ -1,7 +1,6 @@
package main
import (
"fmt"
"math/rand"
"time"
)
@ -10,36 +9,78 @@ 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
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")
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 {
print("Attack Damage: ", hero.Damage, "\n")
}
damage = rand.Intn(enemyDamage)
hero.Health -= damage
print(hero.Name, " was hit for ", damage, " damage!\n")
if enemy.Name == "Goblin" {
print("This is a goblin character\n")
if hero.Health > 0 {
damage = rand.Intn(heroDamage)
enemy.Health -= damage
print(enemy.Name, " was hit for ", damage, " damage!\n")
}
}
time.Sleep(speed)
func spellOrAttack() string {
if rand.Intn(2) == 1{
return "spell"
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 {
return "attack"
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")
}

20
characters.go Normal file
View File

@ -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}
)

View File

@ -7,5 +7,13 @@ type Spell struct {
Cost int
Level int
Active bool
}
var (
firearrow = Spell{
Name: "Fire Arrow",
Damage: 50,
Cost: 10,
Level: 1,
Active: true}
)

82
main.go
View File

@ -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)
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
}

18
weapons.go Normal file
View File

@ -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}
)