diff --git a/32_Diamond/rust/Cargo.toml b/32_Diamond/rust/Cargo.toml new file mode 100644 index 00000000..1ec69633 --- /dev/null +++ b/32_Diamond/rust/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "rust" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/32_Diamond/rust/README.md b/32_Diamond/rust/README.md new file mode 100644 index 00000000..f84e546c --- /dev/null +++ b/32_Diamond/rust/README.md @@ -0,0 +1,3 @@ +Original source downloaded [from Vintage Basic](http://www.vintage-basic.net/games.html) + +Conversion to [Rust](https://www.rust-lang.org/) by Anthony Rubick [AnthonyMichaelTDM](https://github.com/AnthonyMichaelTDM) \ No newline at end of file diff --git a/32_Diamond/rust/src/lib.rs b/32_Diamond/rust/src/lib.rs new file mode 100644 index 00000000..0d820562 --- /dev/null +++ b/32_Diamond/rust/src/lib.rs @@ -0,0 +1,169 @@ +/* + lib.rs contains all the logic of the program +*/ + +use std::{error::Error, fmt::Display, str::FromStr, io::{self, Write}}; + +const LINE_WIDTH:usize = 60; +const EDGE: &str = "C"; +const EDGE_WIDTH: usize = 2; +const FILL: &str = "!"; + +/// handles setup for the game +pub struct Config { + diamond_size: usize, +} +impl Config { + /// creates and returns a new Config from user input + pub fn new() -> Result> { + //DATA + let mut config: Config = Config { diamond_size: 0 }; + + //get data from user input + + //get num players + println!("FOR A PRETTY DIAMOND PATTERN,"); + //input looop + config.diamond_size = loop { + match get_number_from_input("TYPE IN AN ODD NUMBER BETWEEN 5 AND 21 ", 5, 21) { + Ok(num) => break num, + Err(e) => { + eprintln!("{}",e); + continue; + }, + } + }; + + //return new config + return Ok(config); + } +} + +/// run the program +pub fn run(config: &Config) -> Result<(), Box> { + //DATA + let diamonds = LINE_WIDTH / config.diamond_size; + let mut line: String; + + //print out diamond + for i in 1..diamonds { + let mut x = 1; + let mut y = config.diamond_size.clone(); + for n in x..y { + line = String::new(); + + //add (diamond size - n) / 2 spaces + line += &n_spaces( (config.diamond_size - n) / 2); + + for m in 1..diamonds { + let mut c = 1; + + for a in 1..n { + if c > EDGE_WIDTH { + line += FILL; + } else { + line += EDGE; + c = c + 1; + } + } + + //53 + } + + + + + //print line + println!("{}", line); + } + } + + + //return to main + Ok(()) +} + +/// returns n spaces in a string +fn n_spaces(n:usize) -> String { + let mut output = String::new(); + + for _i in 0..n { + output += " "; + } + + output +} + + +/// gets a string from user input +fn get_string_from_user_input(prompt: &str) -> Result> { + //DATA + let mut raw_input = String::new(); + + //print prompt + print!("{}", prompt); + //make sure it's printed before getting input + io::stdout().flush().unwrap(); + + //read user input from standard input, and store it to raw_input, then return it or an error as needed + raw_input.clear(); //clear input + match io::stdin().read_line(&mut raw_input) { + Ok(_num_bytes_read) => return Ok(String::from(raw_input.trim())), + Err(err) => return Err(format!("ERROR: CANNOT READ INPUT!: {}", err).into()), + } +} + +/// generic function to get a number from the passed string (user input) +/// pass a min lower than the max to have minimun and maximun bounds +/// pass a min higher than the max to only have a minumum bound +/// pass a min equal to the max to only have a maximun bound +/// +/// Errors: +/// no number on user input +fn get_number_from_input(prompt: &str, min:T, max:T) -> Result> { + //DATA + let raw_input: String; + let processed_input: String; + + + //input looop + raw_input = loop { + match get_string_from_user_input(prompt) { + Ok(input) => break input, + Err(e) => { + eprintln!("{}",e); + continue; + }, + } + }; + + //filter out num-numeric characters from user input + processed_input = raw_input.chars().filter(|c| c.is_numeric()).collect(); + + //from input, try to read a number + match processed_input.trim().parse() { + Ok(i) => { + //what bounds must the input fall into + if min < max { //have a min and max bound: [min,max] + if i >= min && i <= max {//is input valid, within bounds + return Ok(i); //exit the loop with the value i, returning it + } else { //print error message specific to this case + return Err(format!("ONLY BETWEEN {} AND {}, PLEASE!", min, max).into()); + } + } else if min > max { //only a min bound: [min, infinity) + if i >= min { + return Ok(i); + } else { + return Err(format!("NO LESS THAN {}, PLEASE!", min).into()); + } + } else { //only a max bound: (-infinity, max] + if i <= max { + return Ok(i); + } else { + return Err(format!("NO MORE THAN {}, PLEASE!", max).into()); + } + } + }, + Err(_e) => return Err(format!("Error: couldn't find a valid number in {}",raw_input).into()), + } +} \ No newline at end of file diff --git a/32_Diamond/rust/src/main.rs b/32_Diamond/rust/src/main.rs new file mode 100644 index 00000000..8aab7350 --- /dev/null +++ b/32_Diamond/rust/src/main.rs @@ -0,0 +1,40 @@ +use std::process; //allows for some better error handling + +mod lib; +use lib::Config; + +/// main function +/// responsibilities: +/// - Calling the command line logic with the argument values +/// - Setting up any other configuration +/// - Calling a run function in lib.rs +/// - Handling the error if run returns an error +fn main() { + //greet user + welcome(); + + // set up other configuration + let config = Config::new().unwrap_or_else(|err| { + eprintln!("Problem configuring program: {}", err); + process::exit(1); + }); + + // run the program + if let Err(e) = lib::run(&config) { + eprintln!("Application Error: {}", e); //use the eprintln! macro to output to standard error + process::exit(1); //exit the program with an error code + } + + //end of program + println!("THANKS FOR PLAYING!"); +} + +/// welcome message +fn welcome() { + print!(" + DIAMOND + CREATIVE COMPUTING MORRISTOWN, NEW JERSEY + + + "); +}