Generate Chess960 starting position: Difference between revisions

Line 2,045:
}
}</lang>
 
 
===Rust 1.57 nightly===
<lang Rust>
// Chess960: regex and unicode version, create 5 valid random positions.
 
use rand::{seq::SliceRandom, thread_rng};
use regex::Regex;
 
fn vec_to_string(v: Vec<&str>) -> String {
let mut is_string = String::new();
for ele in v {
is_string.push_str(&ele.to_string())
}
is_string
}
fn is_knight_king_ok(str_to_check: Vec<&str>) -> bool {
Regex::new(r"(.*♖.*♔.*♖.*)")
.unwrap()
.is_match(vec_to_string(str_to_check.clone()).as_str())
}
fn is_two_bishops_ok(str_to_check: Vec<&str>) -> bool {
Regex::new(r"(.*♗.{0}♗.*|.*♗.{2}♗.*|.*♗.{4}♗.*|.*♗.{6}♗.*)")
.unwrap()
.is_match(vec_to_string(str_to_check.clone()).as_str())
}
fn create_rnd_candidate() -> [&'static str; 8] {
let mut prng = thread_rng();
let mut chaine = ["♖", "♘", "♗", "♔", "♕", "♗", "♘", "♖"];
 
loop {
chaine.shuffle(&mut prng);
if is_candidate_valide(chaine) {
break chaine;
}
}
}
fn is_candidate_valide(s: [&str; 8]) -> bool {
is_knight_king_ok(s.to_vec()) && is_two_bishops_ok(s.to_vec())
}
fn main() {
for _ in 0..5 {
println!("{:?}", create_rnd_candidate());
}
}</lang>
{{out}}
<pre>["♕", "♘", "♗", "♗", "♖", "♘", "♔", "♖"]
["♖", "♔", "♗", "♕", "♘", "♗", "♖", "♘"]
["♗", "♖", "♕", "♔", "♘", "♗", "♖", "♘"]
["♘", "♖", "♘", "♕", "♔", "♖", "♗", "♗"]
["♖", "♗", "♕", "♔", "♗", "♘", "♖", "♘"]</pre>
 
=={{header|Scala}}==
Anonymous user