package main | |
import "fmt" | |
type Weapon interface { | |
Strength() int | |
} | |
/* type KnightWeapon interface { */ | |
/* Weapon */ | |
/* } */ | |
/* type MagicianWeapon interface { */ | |
/* Weapon */ | |
/* } */ | |
type weapon struct { | |
strength int | |
} | |
func (w *weapon) Strength() int { | |
return w.strength | |
} | |
type knightWeapon struct { | |
*weapon | |
} | |
type magicianWeapon struct { | |
*weapon | |
} | |
var NullWeapon *weapon = &weapon{0} | |
var LongSword *knightWeapon = &knightWeapon{ &weapon{8} } | |
var Rod *magicianWeapon = &magicianWeapon{ &weapon{2} } | |
type HasWeapon interface { | |
Weapon() Weapon | |
} | |
type Player interface { | |
HasWeapon | |
Name() string | |
Attack(otherPlayer Player) | |
Damaged(damage int) | |
IsDead() bool | |
} | |
type player struct { | |
name string | |
hp int | |
mp int | |
strength int | |
weapon Weapon | |
} | |
func (p *player) Name() string { | |
return p.name | |
} | |
func (p *player) Weapon() Weapon { | |
return p.weapon | |
} | |
func (p *player) Attack(otherPlayer Player) { | |
fmt.Printf("%sは%sを攻撃した\n", p.name, otherPlayer.Name()) | |
damage := p.strength + p.Weapon().Strength() | |
otherPlayer.Damaged(damage) | |
} | |
func (p *player) Damaged(damage int) { | |
fmt.Printf("=> %sは%dのダメージを受けた\n", p.name, damage) | |
if p.hp - damage < 0 { | |
p.hp = 0 | |
} else { | |
p.hp -= damage | |
} | |
if p.IsDead() { | |
fmt.Printf("%sはしんでしまった\n", p.name) | |
} | |
} | |
func (p *player) IsDead() bool { | |
return p.hp <= 0 | |
} | |
type CanEquip interface { | |
Equip(w Weapon) | |
} | |
type Knight interface { | |
Player | |
CanEquip | |
} | |
type knight struct { | |
*player | |
} | |
func (p *knight) Equip(w Weapon) { | |
switch w.(type) { | |
case *knightWeapon: | |
p.weapon = w | |
} | |
} | |
func NewKnightPlayer(name string) Knight { | |
knight := &knight{ &player{ name: name, hp: 20, mp: 0, strength: 5, weapon: NullWeapon } } | |
return knight | |
} | |
type Magician interface { | |
Player | |
CanEquip | |
Mera(player Player) | |
} | |
type magician struct { | |
*player | |
} | |
func (p *magician) Equip(w Weapon) { | |
switch w.(type) { | |
case *magicianWeapon: | |
p.weapon = w | |
} | |
} | |
func (p *magician) Mera(otherPlayer Player) { | |
fmt.Printf("%sはメラを唱えた\n", p.name) | |
if p.mp < 3 { | |
fmt.Printf("...しかしMPが足りない!!\n") | |
} else { | |
p.mp -= 3 | |
otherPlayer.Damaged(10) | |
} | |
} | |
func NewMagicianPlayer(name string) Magician { | |
magician := &magician{ &player{ name: name, hp: 15, mp: 5, strength: 3, weapon: NullWeapon } } | |
return magician | |
} | |
func main() { | |
shinpei := NewMagicianPlayer("しんぺい") | |
takashi := NewKnightPlayer("たかし") | |
shinpei.Equip(Rod) | |
takashi.Equip(LongSword) | |
shinpei.Mera(takashi) | |
takashi.Attack(shinpei) | |
shinpei.Mera(takashi) | |
takashi.Attack(shinpei) | |
} |
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
Show comment Hide comment
interfaceつかいこなせてない...くるしい... だれかかっちょよくなおして... |
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
Show comment Hide comment
というかかき途中でタイムアップなのだ! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
interfaceつかいこなせてない...くるしい...
だれかかっちょよくなおして...