Created
August 16, 2020 21:48
-
-
Save borakasmer/2f8aa6adc34dba9e50424fe98edd21ed to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import "fmt" | |
var dataHistoryList map[int]Fighter | |
type Fighter interface{ | |
GetId() FighterType | |
Fight() | |
Clone() Fighter | |
} | |
type Soldier struct { | |
id FighterType | |
gunHit int | |
life int | |
mana int | |
} | |
type FighterType int | |
const ( | |
SoldierType FighterType = 1 | |
ElfType = 2 | |
DwarfType = 3 | |
) | |
func NewSoldier(gunHit,life,mana int) Soldier{ | |
soldier := Soldier{SoldierType,gunHit,life,mana} | |
return soldier | |
} | |
func (s Soldier) GetId() FighterType{ | |
return s.id | |
} | |
func (s Soldier)Clone() Fighter { | |
return NewSoldier(s.gunHit,s.life,s.mana) | |
} | |
func (s Soldier)Fight(){ | |
fmt.Println("Soldier Gun Hit-Points :",s.gunHit) | |
} | |
type Elf struct { | |
id FighterType | |
arrowHit int | |
life int | |
mana int | |
} | |
func NewElf(arrowHit,life,mana int) Elf{ | |
elf := Elf{ElfType,arrowHit,life,mana} | |
return elf | |
} | |
func (e Elf) GetId() FighterType{ | |
return e.id | |
} | |
func (e Elf)Clone()Fighter { | |
return NewElf(e.arrowHit,e.life,e.mana) | |
} | |
func (e Elf)Fight(){ | |
fmt.Println("Elf Arrow Hit-Points :",e.arrowHit) | |
} | |
type Dwarf struct{ | |
id FighterType | |
axePoint int | |
life int | |
mana int | |
} | |
func NewDwarf(axePoint,life,mana int) Dwarf{ | |
dwarf := Dwarf{DwarfType,axePoint,life,mana} | |
return dwarf | |
} | |
func (d Dwarf) GetId() FighterType{ | |
return d.id | |
} | |
func (d Dwarf)Clone()Fighter { | |
return NewDwarf(d.axePoint,d.life,d.mana) | |
} | |
func (d Dwarf)Fight(){ | |
fmt.Println("Dwarf Axe Hit-Points :",d.axePoint) | |
} | |
func loadCache(){ | |
soldier :=NewSoldier(30,20,5) | |
elf := NewElf(15,30,3) | |
dwarf := NewDwarf(50,40,15) | |
dataHistoryList = make(map[int]Fighter) | |
dataHistoryList[int(soldier.id)]=soldier | |
dataHistoryList[int(elf.id)]=elf | |
dataHistoryList[int(dwarf.id)]=dwarf | |
} | |
func main() { | |
loadCache() | |
fighter :=dataHistoryList[int(SoldierType)] | |
f,ok := fighter.(Soldier) | |
if(ok){ | |
f.gunHit=45 | |
f.Fight() | |
} | |
fighter2 :=dataHistoryList[int(SoldierType)] | |
fighter2.Fight() | |
elf :=dataHistoryList[int(ElfType)] | |
elf.Fight() | |
dwarf :=dataHistoryList[int(DwarfType)] | |
dwarf.Fight() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment