Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file not shown.
29 changes: 29 additions & 0 deletions Solutions/DSMdongly/game-shop/part-1/src/game-shop/item/item.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package item

import (
"fmt"
)

type Item struct {
Name string
Description string
Weight int
Value int
}

func NewItem(nme string, dsc string, wgt int, val int) *Item {
return &Item{
Name: nme,
Description: dsc,
Weight: wgt,
Value: val,
}
}

func (ite Item) Describe() {
fmt.Println()
fmt.Printf("Name = %s\n", ite.Name)
fmt.Printf("Description = %s\n", ite.Description)
fmt.Printf("Weight = %d lbs\n", ite.Weight)
fmt.Printf("Value = %d gold coins\n", ite.Value)
}
13 changes: 13 additions & 0 deletions Solutions/DSMdongly/game-shop/part-1/src/game-shop/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package main

import (
"game-shop/item"
)

func main() {
swd := item.NewItem("Excalibur", "The legendary sword of King Arthur", 12, 1024)
swd.Describe()

arm := item.NewItem("Steel Armor", "Protective covering made by steel", 15, 805)
arm.Describe()
}
Binary file not shown.
20 changes: 20 additions & 0 deletions Solutions/DSMdongly/game-shop/part-2/src/game-shop/item/armor.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package item

import "fmt"

type Armor struct {
Item
Defense int
}

func NewArmor(nme string, dsc string, wgt int, val int, def int) *Armor {
return &Armor{
Item: *NewItem(nme, dsc, wgt, val),
Defense: def,
}
}

func (arm Armor) Describe() {
arm.Item.Describe()
fmt.Printf("Defense = %d\n", arm.Defense)
}
27 changes: 27 additions & 0 deletions Solutions/DSMdongly/game-shop/part-2/src/game-shop/item/item.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package item

import "fmt"

type Item struct {
Name string
Description string
Weight int
Value int
}

func NewItem(nme string, dsc string, wgt int, val int) *Item {
return &Item{
Name: nme,
Description: dsc,
Weight: wgt,
Value: val,
}
}

func (ite Item) Describe() {
fmt.Println()
fmt.Printf("Name = %s\n", ite.Name)
fmt.Printf("Description = %s\n", ite.Description)
fmt.Printf("Weight = %d lbs\n", ite.Weight)
fmt.Printf("Value = %d gold coins\n", ite.Value)
}
20 changes: 20 additions & 0 deletions Solutions/DSMdongly/game-shop/part-2/src/game-shop/item/weapon.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package item

import "fmt"

type Weapon struct {
Item
Damage int
}

func NewWeapon(nme string, dsc string, wgt int, val int, dmg int) *Weapon {
return &Weapon{
Item: *NewItem(nme, dsc, wgt, val),
Damage: dmg,
}
}

func (wep Weapon) Describe() {
wep.Item.Describe()
fmt.Printf("Damage = %d\n", wep.Damage)
}
13 changes: 13 additions & 0 deletions Solutions/DSMdongly/game-shop/part-2/src/game-shop/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package main

import (
"game-shop/item"
)

func main() {
wep := item.NewWeapon("Excalibur", "The legendary sword of King Arthur", 12, 1024, 24)
wep.Describe()

arm := item.NewArmor("Steel Armor", "Protective covering made by steel", 15, 805, 18)
arm.Describe()
}
Binary file not shown.
Binary file not shown.
20 changes: 20 additions & 0 deletions Solutions/DSMdongly/game-shop/part-3/src/game-shop/item/armor.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package item

import "fmt"

type Armor struct {
Item
Defense int
}

func NewArmor(nme string, dsc string, wgt int, val int, def int) *Armor {
return &Armor{
Item: *NewItem(nme, dsc, wgt, val),
Defense: def,
}
}

func (arm Armor) Describe() {
arm.Item.Describe()
fmt.Printf("Defense = %d\n", arm.Defense)
}
27 changes: 27 additions & 0 deletions Solutions/DSMdongly/game-shop/part-3/src/game-shop/item/item.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package item

import "fmt"

type Item struct {
Name string
Description string
Weight int
Value int
}

func NewItem(nme string, dsc string, wgt int, val int) *Item {
return &Item{
Name: nme,
Description: dsc,
Weight: wgt,
Value: val,
}
}

func (ite Item) Describe() {
fmt.Println()
fmt.Printf("Name = %s\n", ite.Name)
fmt.Printf("Description = %s\n", ite.Description)
fmt.Printf("Weight = %d lbs\n", ite.Weight)
fmt.Printf("Value = %d gold coins\n", ite.Value)
}
23 changes: 23 additions & 0 deletions Solutions/DSMdongly/game-shop/part-3/src/game-shop/item/potion.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package item

import "fmt"

type Potion struct {
Item
Type string
Capacity int
}

func NewPotion(nme string, dsc string, wgt int, val int, typ string, cap int) *Potion {
return &Potion{
Item: *NewItem(nme, dsc, wgt, val),
Type: typ,
Capacity: cap,
}
}

func (pot Potion) Describe() {
pot.Item.Describe()
fmt.Printf("Type = %s\n", pot.Type)
fmt.Printf("Capacity = %d\n", pot.Capacity)
}
20 changes: 20 additions & 0 deletions Solutions/DSMdongly/game-shop/part-3/src/game-shop/item/weapon.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package item

import "fmt"

type Weapon struct {
Item
Damage int
}

func NewWeapon(nme string, dsc string, wgt int, val int, dmg int) *Weapon {
return &Weapon{
Item: *NewItem(nme, dsc, wgt, val),
Damage: dmg,
}
}

func (wep Weapon) Describe() {
wep.Item.Describe()
fmt.Printf("Damage = %d\n", wep.Damage)
}
87 changes: 87 additions & 0 deletions Solutions/DSMdongly/game-shop/part-3/src/game-shop/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package main

import (
"fmt"
"game-shop/item"
"game-shop/shop"
"log"
"strconv"
"time"
)

func main() {
esh := InitEquipmentShop()
psh := InitPotionShop()

SHOP_SELECTION:
for {
fmt.Println()
fmt.Println("Shop Select")
fmt.Println()
fmt.Println("1. Weapon/Armor Shop")
fmt.Println("2. Potion Shop")
fmt.Println("3. Exit")
fmt.Println()

buf := ""
fmt.Scanln(&buf)

flg, err := strconv.Atoi(buf)

if err != nil {
log.Fatal(err)
}

switch flg {
case 1:
fmt.Println()
fmt.Println("Welcome to Weapon/Armor Shop!")
fmt.Println()

time.Sleep(time.Millisecond * 500)
esh.ShowItemList()

break
case 2:
fmt.Println()
fmt.Println("Welcome to Potion Shop!")
fmt.Println()

time.Sleep(time.Millisecond * 500)
psh.ShowItemList()

break
case 3:
break SHOP_SELECTION
default:
fmt.Println()
fmt.Println("Invalid number! Try again.")

continue SHOP_SELECTION
}
}
}

func InitEquipmentShop() *shop.Shop {
eqps := make([]shop.Item, 0)
eqps = append(eqps, item.NewWeapon("Sword", "Medium DMG", 3, 10, 10))
eqps = append(eqps, item.NewWeapon("Cap", "Light Armor", 1, 5, 5))
eqps = append(eqps, item.NewWeapon("Gloves", "Light Armor", 1, 5, 5))
eqps = append(eqps, item.NewWeapon("Axe", "High DMG", 5, 15, 15))
eqps = append(eqps, item.NewWeapon("Boots", "Light Armor", 1, 5, 5))

esh := shop.NewShop("Weapon/Armor Shop", eqps)
return esh
}

func InitPotionShop() *shop.Shop {
pots := make([]shop.Item, 0)
pots = append(pots, item.NewPotion("Small Health Potion", "Recovery 100 HP", 2, 5, "Health", 100))
pots = append(pots, item.NewPotion("Small Mana Potion", "Recovery 50 MP", 1, 30, "Mana", 50))
pots = append(pots, item.NewPotion("Medium Health Potion", "Recovery 200 HP", 4, 120, "Health", 200))
pots = append(pots, item.NewPotion("Small Health Potion", "Recovery 100 MP", 2, 75, "Mana", 100))
pots = append(pots, item.NewPotion("Large Health Potion", "Recovery 300 HP", 6, 200, "Health", 300))

psh := shop.NewShop("Potion Shop", pots)
return psh
}
23 changes: 23 additions & 0 deletions Solutions/DSMdongly/game-shop/part-3/src/game-shop/shop/shop.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package shop

type Item interface {
Describe()
}

type Shop struct {
Name string
Items []Item
}

func NewShop(nme string, ites []Item) *Shop {
return &Shop{
Name: nme,
Items: ites,
}
}

func (shp Shop) ShowItemList() {
for _, ite := range shp.Items {
ite.Describe()
}
}