1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
//! contains some helper functions for prompting input from the player

use std::io;
use std::io::prelude::*;

pub fn select_level() -> usize {
    loop {
        let response = request("Choose level to play [1-9] ");
        if let Ok(n) = response.parse::<usize>() {
            if (1..=9).contains(&n) {
                break n;
            }
        }
        println!("{} is not a valid level.", response);
    }
}

/// Helper function for prompting the player with a yes/no question
pub fn ask(message: &str) -> bool {
    let mut message = message.to_owned();
    message.push_str(" [y/n] ");
    request(&message).to_lowercase() == "y"
}

/// Helper function for prompting the player for a response
pub fn request(message: &str) -> String {
    print!("{}", message);
    io::stdout().flush().unwrap();
    let mut response = String::new();
    io::stdin().read_line(&mut response).unwrap();
    response.trim().to_string()
}