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,45 +1,86 @@
package main package main
import ( import (
"fmt"
"math/rand" "math/rand"
"time" "time"
) )
type Character struct { type Character struct {
Name string Name string
Health int Health int
Damage int Damage int
Spell Spell 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) { heroInitiative := rand.Intn(7) + hero.Initiative
speed *= time.Second enemyInitiative := rand.Intn(7) + enemy.Initiative
rand.Seed(time.Now().Unix()) damage := 0
fmt.Println("Random Int: ",rand.Intn(11))
//time.Sleep(speed)
print("Hero: ",hero.Name, "\n")
print("Enemy: ",enemy.Name, "\n")
if spellOrAttack() == "spell"{ for {
print("Spell Damage: ",hero.Spell.Damage, "\n") printCombatantStats(&hero, &enemy, heroInitiative, enemyInitiative)
}else{
print("Attack Damage: ", hero.Damage, "\n")
}
if enemy.Name == "Goblin" { if heroInitiative >= enemyInitiative {
print("This is a goblin character\n") 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 hero.Health > 0 {
if rand.Intn(2) == 1{ damage = rand.Intn(heroDamage)
return "spell" enemy.Health -= damage
}else{ print(enemy.Name, " was hit for ", damage, " damage!\n")
return "attack" }
} }
}
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")
}

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

@ -2,10 +2,18 @@ package main
//Spell contains all the magic spells //Spell contains all the magic spells
type Spell struct { type Spell struct {
Name string Name string
Damage int Damage int
Cost int Cost int
Level int Level int
Active bool Active bool
} }
var (
firearrow = Spell{
Name: "Fire Arrow",
Damage: 50,
Cost: 10,
Level: 1,
Active: true}
)

86
main.go
View File

@ -1,29 +1,63 @@
package main package main
import (
"bufio"
//Characters "fmt"
var ( "math/rand"
player = Character{ "os"
"os/exec"
Name: "Daniel", "time"
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}
) )
func main(){
Battle(player, goblin, 1) 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
}

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