Created
March 12, 2016 02:19
-
-
Save tatsuya6502/cd6223281836c98efd8a to your computer and use it in GitHub Desktop.
Zundoko-Kiyoshi with 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; | |
#[derive(Clone, Copy, PartialEq)] | |
enum Sound { | |
Zun, | |
Doko, | |
} | |
impl Sound { | |
fn pick(mut rng: &mut rand::Rng) -> Self { | |
rand::sample(&mut rng, [Zun, Doko].iter(), 1)[0].clone() | |
} | |
} | |
impl ToString for Sound { | |
fn to_string(&self) -> String { | |
match *self { | |
Zun => "ズン".to_owned(), | |
Doko => "ドコ".to_owned(), | |
} | |
} | |
} | |
use Sound::*; | |
struct ZunDoko(Vec<Sound>); | |
impl ZunDoko { | |
fn play(goal: &[Sound]) -> Self { | |
let g_len = goal.len(); | |
let mut rng = rand::thread_rng(); | |
let mut zd = Vec::new(); | |
loop { | |
zd.push(Sound::pick(&mut rng)); | |
if let Some(&Doko) = zd.last() { | |
let len = zd.len(); | |
if len >= g_len && &zd[(len - g_len)..] == goal { | |
break; | |
} | |
} | |
} | |
ZunDoko(zd) | |
} | |
fn print(&self) { | |
for item in &self.0 { | |
print!("{} ", item.to_string()); | |
} | |
println!("キ・ヨ・シ! ({}回)\n", self.0.len()); | |
} | |
} | |
fn main() { | |
let num_threads = 8; | |
println!("\n{}人同時にいくよ! せーのっ\n", num_threads); | |
let goal = [Zun, Zun, Zun, Zun, Doko]; | |
let handles: Vec<_> = (0..num_threads).map(|_| { | |
std::thread::spawn(move || { | |
ZunDoko::play(&goal.clone()) | |
}) | |
}).collect(); | |
for h in handles { | |
let _ = h.join().map(|zundoko| zundoko.print()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment