Skip to content

Instantly share code, notes, and snippets.

@AregevDev
Last active February 22, 2019 19:12
Show Gist options
  • Save AregevDev/f59e46440f6499b55e76cedf36cc1048 to your computer and use it in GitHub Desktop.
Save AregevDev/f59e46440f6499b55e76cedf36cc1048 to your computer and use it in GitHub Desktop.
A Rust simulation of Star Wars III's order 66 scene
use std::fmt::{Display, Formatter, Result};
use std::sync::{Arc, Mutex};
enum Killer {
AnakinSkywalker,
Clones,
}
impl Display for Killer {
fn fmt(&self, f: &mut Formatter) -> Result {
match *self {
Killer::Clones => write!(f, "The clones"),
Killer::AnakinSkywalker => write!(f, "Anakin Skywalker"),
}
}
}
enum Color {
Blue,
Green,
Purple,
}
struct Jedi {
name: String,
light_saber: Color,
planet: String,
killer: Killer,
killed: bool,
}
impl Jedi {
fn new(name: String, light_saber: Color, planet: String, killer: Killer, killed: bool) -> Self {
Jedi {
name,
light_saber,
planet,
killer,
killed,
}
}
fn betray(&self) {
if self.name != "Younglings" {
println!(
"{} was betrayed by: {} on planet: {}",
self.name, self.killer, self.planet
);
} else {
println!(
"{} were betrayed by: {} on planet: {}",
self.name, self.killer, self.planet
);
}
if self.killed {
println!("Killed by {}", self.killer);
}
}
}
fn main() {
println!("Commander rustc, the time has come, execute order 66");
println!("Will be done my lord");
let jedi_arr = Arc::new(Mutex::new(vec![
Jedi::new(
"Obi-Wan Kenobi".into(),
Color::Blue,
"Utapau".into(),
Killer::Clones,
false,
),
Jedi::new(
"Ki-Adi-Mundi".into(),
Color::Blue,
"Mygeeto".into(),
Killer::Clones,
true,
),
Jedi::new(
"Aayla Secura".into(),
Color::Blue,
"Felucia".into(),
Killer::Clones,
true,
),
Jedi::new(
"Plo Koon".into(),
Color::Blue,
"Cato Neimoidia".into(),
Killer::Clones,
true,
),
Jedi::new(
"Stass Allie".into(),
Color::Blue,
"Saleucami".into(),
Killer::Clones,
true,
),
Jedi::new(
"Yoda".into(),
Color::Green,
"Kashyyyk".into(),
Killer::Clones,
false,
),
Jedi::new(
"Mace Windu".into(),
Color::Purple,
"Coruscant".into(),
Killer::AnakinSkywalker,
true,
),
Jedi::new(
"Younglings".into(),
Color::Green,
"Coruscant".into(),
Killer::AnakinSkywalker,
true,
),
]));
let mut handles = vec![];
for i in 0..7 {
let jedi_arr = Arc::clone(&jedi_arr);
let handle = std::thread::spawn(move || {
let jedi = jedi_arr.lock().unwrap();
jedi[i].betray();
});
handles.push(handle);
}
for handle in handles {
handle.join().unwrap();
}
println!("Order 66 has ended, the Jedi are no more, long live The Empire!");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment