Last active
December 17, 2016 00:43
-
-
Save ttdonovan/ce5795cfc2499d4d98bc6577839f0eb5 to your computer and use it in GitHub Desktop.
A barn yard simulator written in Rust.
This file contains hidden or 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
extern crate rand; | |
trait Animal<'a> { | |
fn make_sound(&self) -> String; | |
fn move_within(&mut self, yard: &'a Yard); | |
} | |
#[derive(Debug)] | |
struct Area { | |
zone: u32, | |
} | |
impl PartialEq for Area { | |
fn eq(&self, other: &Area) -> bool { | |
self.zone == other.zone | |
} | |
} | |
#[derive(Debug)] | |
struct Cow<'a> { | |
area: &'a Area, | |
} | |
impl<'a> Animal<'a> for Cow<'a> { | |
fn make_sound(&self) -> String { | |
"Moo!".to_string() | |
} | |
fn move_within(&mut self, yard: &'a Yard) { | |
let a = get_random_area(&yard.areas); | |
self.area = a; | |
} | |
} | |
#[derive(Debug)] | |
struct Pig<'a> { | |
area: &'a Area, | |
} | |
impl<'a> Animal<'a> for Pig<'a> { | |
fn make_sound(&self) -> String { | |
"Oink!".to_string() | |
} | |
fn move_within(&mut self, yard: &'a Yard) { | |
let a = get_random_area(&yard.areas); | |
self.area = a; | |
} | |
} | |
#[derive(Debug)] | |
struct Yard { | |
areas: Vec<Area>, | |
} | |
impl Yard { | |
fn new(size: u32) -> Yard { | |
Yard { | |
areas: create_areas(size), | |
} | |
} | |
fn add_cows(&self, qty: u32) -> Vec<Cow> { | |
let mut cows : Vec<Cow> = vec![]; | |
for _ in 0..qty { | |
let a = get_random_area(&self.areas); | |
cows.push(Cow{ area: a }); | |
} | |
cows | |
} | |
fn add_pigs(&self, qty: u32) -> Vec<Pig> { | |
let mut pigs : Vec<Pig> = vec![]; | |
for _ in 0..qty { | |
let a = get_random_area(&self.areas); | |
pigs.push(Pig{ area: a }); | |
} | |
pigs | |
} | |
} | |
fn create_areas(size: u32) -> Vec<Area> { | |
let mut areas = vec![]; | |
for i in 0..size { | |
areas.push(Area{ zone: i }); | |
} | |
areas | |
} | |
fn get_random_area(areas: &Vec<Area>) -> &Area { | |
let mut rng = rand::thread_rng(); | |
rand::sample(&mut rng, areas, 1)[0] | |
} | |
fn turn<'a>(yard: &'a Yard, mut cows: Vec<Cow<'a>>, mut pigs: Vec<Pig<'a>>) { | |
println!("Move the {} Cows and {} Pigs around the yard and make some noise.", cows.len(), pigs.len()); | |
for cow in &mut cows { | |
cow.move_within(yard); | |
} | |
for pig in &mut pigs { | |
pig.move_within(yard); | |
} | |
for area in &yard.areas { | |
println!("Find any Cows and Pigs in {:?}", area); | |
let area_cows = cows.iter().filter(|c| c.area == area); | |
let cow_sounds = area_cows.map(|c| c.make_sound()).collect::<Vec<String>>().join(", "); | |
let area_pigs = pigs.iter().filter(|p| p.area == area); | |
let pig_sounds = area_pigs.map(|c| c.make_sound()).collect::<Vec<String>>().join(", "); | |
if cow_sounds.len() != 0 && pig_sounds.len() != 0 { | |
println!("{}", vec![cow_sounds, pig_sounds].join(", ")); | |
} else { | |
println!("All is quite in {:?}", area); | |
} | |
} | |
} | |
fn main() { | |
println!("Welcome to the Barn Yard!"); | |
let yard = Yard::new(3); | |
let cows = yard.add_cows(2); | |
let pigs = yard.add_pigs(3); | |
turn(&yard, cows, pigs); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment