Skip to content

Instantly share code, notes, and snippets.

@steveklabnik
Last active December 10, 2015 07:48
Embed
What would you like to do?
trait Monster {
fn attack(&self);
static fn new() -> self;
}
struct IndustrialRaverMonkey {
life: int,
strength: int,
charisma: int,
weapon: int,
}
struct DwarvenAngel {
life: int,
strength: int,
charisma: int,
weapon: int,
}
struct AssistantViceTentacleAndOmbudsman {
life: int,
strength: int,
charisma: int,
weapon: int,
}
struct TeethDeer {
life: int,
strength: int,
charisma: int,
weapon: int,
}
struct IntrepidDecomposedCyclist {
life: int,
strength: int,
charisma: int,
weapon: int,
}
struct Dragon {
life: int,
strength: int,
charisma: int,
weapon: int,
}
impl IndustrialRaverMonkey: Monster {
fn attack(&self) {
io::println(fmt!("The monkey attacks for %d.", self.strength))
}
static fn new() -> IndustrialRaverMonkey {
IndustrialRaverMonkey {life: 46, strength: 35, charisma: 91, weapon: 2}
}
}
impl DwarvenAngel: Monster {
fn attack(&self) {
io::println(fmt!("The angel attacks for %d.", self.strength))
}
static fn new() -> DwarvenAngel {
DwarvenAngel {life: 540, strength: 6, charisma: 144, weapon: 50}
}
}
impl AssistantViceTentacleAndOmbudsman: Monster {
fn attack(&self) {
io::println(fmt!("The tentacle attacks for %d.", self.strength))
}
static fn new() -> AssistantViceTentacleAndOmbudsman {
AssistantViceTentacleAndOmbudsman {life: 320, strength: 6, charisma: 144, weapon: 50}
}
}
impl TeethDeer: Monster {
fn attack(&self) {
io::println(fmt!("The deer attacks for %d.", self.strength))
}
static fn new() -> TeethDeer {
TeethDeer {life: 655, strength: 192, charisma: 19, weapon: 109}
}
}
impl IntrepidDecomposedCyclist: Monster {
fn attack(&self) {
io::println(fmt!("The cyclist attacks for %d.", self.strength))
}
static fn new() -> IntrepidDecomposedCyclist {
IntrepidDecomposedCyclist {life: 901, strength: 560, charisma: 422, weapon: 105}
}
}
impl Dragon: Monster {
fn attack(&self) {
io::println(fmt!("The dragon attacks for %d.", self.strength))
}
static fn new() -> Dragon {
Dragon {life: 1340, strength: 451, charisma: 1020, weapon: 939}
}
}
fn monsters_attack(monsters: &[@Monster]) {
for monsters.each |monster| {
monster.attack();
}
}
fn main() {
let monkey: @IndustrialRaverMonkey = @Monster::new();
let angel: @DwarvenAngel = @Monster::new();
let tentacle: @AssistantViceTentacleAndOmbudsman = @Monster::new();
let deer: @TeethDeer = @Monster::new();
let cyclist: @IntrepidDecomposedCyclist = @Monster::new();
let dragon: @Dragon = @Monster::new();
let dwemthys_vector: @[@Monster] = @[monkey as @Monster, angel as @Monster, tentacle as @Monster, deer as @Monster, cyclist as @Monster, dragon as @Monster];
monsters_attack(dwemthys_vector);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment