added random color choser

This commit is contained in:
Wyatt J. Miller 2024-08-02 14:27:55 -04:00
parent 7b8a4e6360
commit 7ff45d9da9
2 changed files with 38 additions and 2 deletions

View File

@ -1,19 +1,38 @@
use rand::seq::SliceRandom; use rand::seq::SliceRandom;
use serde_json::Value; use serde_json::Value;
use std::collections::HashSet;
use std::fs::File; use std::fs::File;
use std::io::BufReader; use std::io::BufReader;
fn main() { fn main() {
let file = File::open("team_names.json").expect("Unable to open JSON"); let file = File::open("team_names.json").expect("Unable to open JSON");
let reader = BufReader::new(file); let reader = BufReader::new(file);
let team_names_json: Value = serde_json::from_reader(reader).expect("Unable to parse JSON"); let teams_json: Value = serde_json::from_reader(reader).expect("Unable to parse JSON");
let team_names_list = team_names_json["team_names"].as_array().unwrap(); let team_names_list = teams_json["team_names"].as_array().unwrap();
let team_colors = teams_json["team_colors"]
.as_array()
.unwrap()
.iter()
.filter_map(|color| color.as_str())
.collect::<Vec<&str>>();
let mut selected_colors = HashSet::new();
let mut rng = rand::thread_rng(); let mut rng = rand::thread_rng();
if let Some(chosen_name) = team_names_list.choose(&mut rng) { if let Some(chosen_name) = team_names_list.choose(&mut rng) {
println!("Randomly chosen team name: {}", chosen_name); println!("Randomly chosen team name: {}", chosen_name);
} else { } else {
println!("No team names available"); println!("No team names available");
} }
while selected_colors.len() < 3 {
if let Some(color) = team_colors.choose(&mut rng) {
selected_colors.insert(*color);
}
}
println!("Randomly selected team colors: ");
for color in selected_colors {
println!("- {}", color);
}
} }

View File

@ -21,5 +21,22 @@
"Station Grass", "Station Grass",
"The Ballers", "The Ballers",
"Keyboard Warriors" "Keyboard Warriors"
],
"team_colors": [
"Red",
"Orange",
"Yellow",
"Green",
"Blue",
"Purple",
"Gray",
"White",
"Black",
"Crimson",
"Teal",
"Ivory",
"Beige",
"Brown",
"Magenta"
] ]
} }