From 6db16d0ed3e7b2163892e0524b7bc8caa291a0d4 Mon Sep 17 00:00:00 2001 From: Logen Kain Date: Mon, 9 Oct 2017 00:25:48 -0700 Subject: [PATCH 1/8] Added weapons --- weapons.go | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 weapons.go diff --git a/weapons.go b/weapons.go new file mode 100644 index 0000000..99b7171 --- /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: 25, + Active: false} + + GoblinClaw = Weapon{ + Name: "Goblin Claw", + Damage: 5} +) From b962452832e8409a925c851bcacd54b4f9b29fe0 Mon Sep 17 00:00:00 2001 From: Logen Kain Date: Mon, 9 Oct 2017 00:26:36 -0700 Subject: [PATCH 2/8] Updated battle.go to allow for weapons --- battle.go | 34 ++++++++++++++++------------------ 1 file changed, 16 insertions(+), 18 deletions(-) diff --git a/battle.go b/battle.go index bea9a45..d9c34b1 100644 --- a/battle.go +++ b/battle.go @@ -7,26 +7,25 @@ import ( ) type Character struct { - Name string - Health int - Damage int - Spell Spell + Name string + Health int + Damage int + Weapons [5]Weapon + Spell Spell } - - func Battle(hero, enemy Character, speed time.Duration) { speed *= time.Second rand.Seed(time.Now().Unix()) - fmt.Println("Random Int: ",rand.Intn(11)) + fmt.Println("Random Int: ", rand.Intn(11)) //time.Sleep(speed) - print("Hero: ",hero.Name, "\n") - print("Enemy: ",enemy.Name, "\n") + print("Hero: ", hero.Name, "\n") + print("Enemy: ", enemy.Name, "\n") - if spellOrAttack() == "spell"{ - print("Spell Damage: ",hero.Spell.Damage, "\n") - }else{ + if spellOrAttack() == "spell" { + print("Spell Damage: ", hero.Spell.Damage, "\n") + } else { print("Attack Damage: ", hero.Damage, "\n") } @@ -35,11 +34,10 @@ func Battle(hero, enemy Character, speed time.Duration) { } } - func spellOrAttack() string { - if rand.Intn(2) == 1{ + if rand.Intn(2) == 1 { return "spell" - }else{ - return "attack" - } -} \ No newline at end of file + } else { + return "attack" + } +} From d623b8ba2ac4c0ef5c9a31f4f9954816abcf1f75 Mon Sep 17 00:00:00 2001 From: Logen Kain Date: Mon, 9 Oct 2017 00:27:09 -0700 Subject: [PATCH 3/8] Added weapons to goblin and player --- main.go | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/main.go b/main.go index 9ad68fd..af5401b 100644 --- a/main.go +++ b/main.go @@ -1,29 +1,33 @@ package main - +import "fmt" //Characters var ( player = Character{ - Name: "Daniel", - Health: 100, - Damage: 60, - Spell: firearrow} + Name: "Daniel", + Health: 100, + Damage: 60, + Spell: firearrow, + Weapons: [5]Weapon{Broadsword}} goblin = Character{ - Name: "Goblin", - Health: 100, - Damage: 20} + Name: "Goblin", + Health: 100, + Damage: 20, + Weapons: [5]Weapon{GoblinClaw}} firearrow = Spell{ - Name: "Fire Arrow", + Name: "Fire Arrow", Damage: 50, Cost: 10, - Level: 1, + Level: 1, Active: true} ) -func main(){ + +func main() { Battle(player, goblin, 1) -} \ No newline at end of file + fmt.Println(goblin.Weapons[0].Damage) +} From 3f438abb68d3304e73f7497327d926f609f0d622 Mon Sep 17 00:00:00 2001 From: Logen Kain Date: Mon, 9 Oct 2017 00:41:51 -0700 Subject: [PATCH 4/8] Removed time from battle function;added initGame and ClearScreen_ --- battle.go | 7 +++---- main.go | 22 ++++++++++++++++++++-- 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/battle.go b/battle.go index d9c34b1..bf67b12 100644 --- a/battle.go +++ b/battle.go @@ -14,12 +14,11 @@ type Character struct { Spell Spell } -func Battle(hero, enemy Character, speed time.Duration) { - speed *= time.Second +func Battle(hero, enemy Character) { + speed := time.Second - rand.Seed(time.Now().Unix()) fmt.Println("Random Int: ", rand.Intn(11)) - //time.Sleep(speed) + time.Sleep(speed) print("Hero: ", hero.Name, "\n") print("Enemy: ", enemy.Name, "\n") diff --git a/main.go b/main.go index af5401b..7c66d91 100644 --- a/main.go +++ b/main.go @@ -1,6 +1,12 @@ package main -import "fmt" +import ( + "fmt" + "math/rand" + "os" + "os/exec" + "time" +) //Characters var ( @@ -28,6 +34,18 @@ var ( ) func main() { - Battle(player, goblin, 1) + initGame() + Battle(player, goblin) fmt.Println(goblin.Weapons[0].Damage) } + +func initGame() { + rand.Seed(time.Now().Unix()) + ClearScreen() +} + +func ClearScreen() { + cmd := exec.Command("clear") + cmd.Stdout = os.Stdout + cmd.Run() +} From 86d1316ff100815107518ab905c9e2e6f5a6415d Mon Sep 17 00:00:00 2001 From: Logen Kain Date: Mon, 9 Oct 2017 01:41:59 -0700 Subject: [PATCH 5/8] 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{ From b0aaf57d0c569b6b7f007aa2f55dc393d9c5e406 Mon Sep 17 00:00:00 2001 From: Logen Kain Date: Mon, 9 Oct 2017 01:45:53 -0700 Subject: [PATCH 6/8] Moved spells from main.go to magic.go --- magic.go | 16 ++++++++++++---- main.go | 7 ------- 2 files changed, 12 insertions(+), 11 deletions(-) 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 797d7a0..c641f59 100644 --- a/main.go +++ b/main.go @@ -25,13 +25,6 @@ var ( Damage: 20, Weapons: [5]Weapon{GoblinClaw}, Initiative: 3} - - firearrow = Spell{ - Name: "Fire Arrow", - Damage: 50, - Cost: 10, - Level: 1, - Active: true} ) func main() { From d9460a7c593844c826c9ed3b266546fd28f758fe Mon Sep 17 00:00:00 2001 From: Logen Kain Date: Mon, 9 Oct 2017 01:48:49 -0700 Subject: [PATCH 7/8] Moved characters from main.go to characters.go --- characters.go | 20 ++++++++++++++++++++ main.go | 20 -------------------- 2 files changed, 20 insertions(+), 20 deletions(-) create mode 100644 characters.go 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/main.go b/main.go index c641f59..d3e0599 100644 --- a/main.go +++ b/main.go @@ -7,26 +7,6 @@ import ( "time" ) -//Characters -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} -) - func main() { initGame() Battle(player, goblin) From 3bd8fcd49d2eb53278472a8d47b488ebfeba175c Mon Sep 17 00:00:00 2001 From: Logen Kain Date: Mon, 9 Oct 2017 02:12:28 -0700 Subject: [PATCH 8/8] Changed seed to nano; added mainloop --- main.go | 43 +++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 41 insertions(+), 2 deletions(-) diff --git a/main.go b/main.go index d3e0599..bf593f5 100644 --- a/main.go +++ b/main.go @@ -1,6 +1,8 @@ package main import ( + "bufio" + "fmt" "math/rand" "os" "os/exec" @@ -9,11 +11,36 @@ import ( func main() { initGame() - Battle(player, goblin) + 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().Unix()) + rand.Seed(time.Now().UnixNano()) ClearScreen() } @@ -22,3 +49,15 @@ func ClearScreen() { 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 +}