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 serde_json::Value;
use std::collections::HashSet;
use std::fs::File;
use std::io::BufReader;
fn main() {
let file = File::open("team_names.json").expect("Unable to open JSON");
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();
if let Some(chosen_name) = team_names_list.choose(&mut rng) {
println!("Randomly chosen team name: {}", chosen_name);
} else {
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",
"The Ballers",
"Keyboard Warriors"
],
"team_colors": [
"Red",
"Orange",
"Yellow",
"Green",
"Blue",
"Purple",
"Gray",
"White",
"Black",
"Crimson",
"Teal",
"Ivory",
"Beige",
"Brown",
"Magenta"
]
}