From cd1dfd92fdcd1d751eb6d6b2a551078bbde5dd74 Mon Sep 17 00:00:00 2001 From: AnthonyMichaelTDM <68485672+AnthonyMichaelTDM@users.noreply.github.com> Date: Sun, 27 Feb 2022 20:07:18 -0800 Subject: [PATCH 1/5] readme's added --- 02_Amazing/rust/README.md | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 02_Amazing/rust/README.md diff --git a/02_Amazing/rust/README.md b/02_Amazing/rust/README.md new file mode 100644 index 00000000..f84e546c --- /dev/null +++ b/02_Amazing/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 From 020fb6271fee2d22e4beff516c941367b27eac3c Mon Sep 17 00:00:00 2001 From: AnthonyMichaelTDM <68485672+AnthonyMichaelTDM@users.noreply.github.com> Date: Sun, 27 Feb 2022 20:37:43 -0800 Subject: [PATCH 2/5] bug fix didn't have a minimum accepted input size, and panicked during testing --- 02_Amazing/rust/src/main.rs | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/02_Amazing/rust/src/main.rs b/02_Amazing/rust/src/main.rs index bc85189f..a1471a6a 100644 --- a/02_Amazing/rust/src/main.rs +++ b/02_Amazing/rust/src/main.rs @@ -163,12 +163,6 @@ fn main() { } println!("."); } - - - - - - } fn get_user_input(prompt: &str) -> usize { @@ -187,7 +181,15 @@ fn get_user_input(prompt: &str) -> usize { //from input, try to read a number match raw_input.trim().parse::() { - Ok(i) => break i, // this escapes the loop, returning i + Ok(i) => { + if i>1 { //min size 1 + break i; // this escapes the loop, returning i + } + else { + println!("INPUT OUT OF RANGE. TRY AGAIN."); + continue;// run the loop again + } + } Err(e) => { println!("MEANINGLESS DIMENSION. TRY AGAIN. {}", e.to_string().to_uppercase()); continue; // run the loop again From 099a5be209952755f5d43a25c458884893b7f995 Mon Sep 17 00:00:00 2001 From: AnthonyMichaelTDM <68485672+AnthonyMichaelTDM@users.noreply.github.com> Date: Sun, 27 Feb 2022 20:49:25 -0800 Subject: [PATCH 3/5] added exit condition when running this as a .exe, it would close immediately after generating the maze, this is no longer the case. --- 02_Amazing/rust/src/main.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/02_Amazing/rust/src/main.rs b/02_Amazing/rust/src/main.rs index a1471a6a..7abc67cf 100644 --- a/02_Amazing/rust/src/main.rs +++ b/02_Amazing/rust/src/main.rs @@ -131,7 +131,6 @@ fn main() { if used[row][col] != 0 {break;} } } - } // Add a random exit col=rng.gen_range(0..width); @@ -163,6 +162,10 @@ fn main() { } println!("."); } + + //stops the program from ending until you give input, useful when running a compiled .exe + println!("\n\npress ENTER to exit"); + io::stdin().read_line(&mut String::new()).expect("closing"); } fn get_user_input(prompt: &str) -> usize { From 2b48798f203462f1bd1499e5392d0e2cfc1e480a Mon Sep 17 00:00:00 2001 From: AnthonyMichaelTDM <68485672+AnthonyMichaelTDM@users.noreply.github.com> Date: Thu, 3 Mar 2022 12:04:57 -0800 Subject: [PATCH 4/5] remove stuff that is in rust-port-amazing branch --- 02_Amazing/rust/Cargo.toml | 9 -- 02_Amazing/rust/README.md | 3 - 02_Amazing/rust/src/main.rs | 202 ------------------------------------ 3 files changed, 214 deletions(-) delete mode 100644 02_Amazing/rust/Cargo.toml delete mode 100644 02_Amazing/rust/README.md delete mode 100644 02_Amazing/rust/src/main.rs diff --git a/02_Amazing/rust/Cargo.toml b/02_Amazing/rust/Cargo.toml deleted file mode 100644 index 276ea543..00000000 --- a/02_Amazing/rust/Cargo.toml +++ /dev/null @@ -1,9 +0,0 @@ -[package] -name = "rust" -version = "0.1.0" -edition = "2018" - -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html - -[dependencies] -rand = "0.8.5" \ No newline at end of file diff --git a/02_Amazing/rust/README.md b/02_Amazing/rust/README.md deleted file mode 100644 index f84e546c..00000000 --- a/02_Amazing/rust/README.md +++ /dev/null @@ -1,3 +0,0 @@ -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/02_Amazing/rust/src/main.rs b/02_Amazing/rust/src/main.rs deleted file mode 100644 index 7abc67cf..00000000 --- a/02_Amazing/rust/src/main.rs +++ /dev/null @@ -1,202 +0,0 @@ -use rand::{Rng, thread_rng, prelude::SliceRandom}; -use std::{io, collections::HashSet}; - -fn main() { - //DATA - enum Direction { - LEFT=0, - UP=1, - RIGHT=2, - DOWN=3, - } - impl Direction { - fn val(&self) -> usize { - match self { - Direction::LEFT=>0, - Direction::UP=>1, - Direction::RIGHT=>2, - Direction::DOWN=>3, - } - } - } - const EXIT_DOWN:usize = 1; - const EXIT_RIGHT:usize = 2; - let mut rng = thread_rng(); //rng - /* - vector of: - vectors of: - integers - Initially set to 0, unprocessed cells. - Filled in with consecutive non-zero numbers as cells are processed - */ - let mut used; //2d vector - /* - vector of: - vectors of: - integers - Remains 0 if there is no exit down or right - Set to 1 if there is an exit down - Set to 2 if there is an exit right - Set to 3 if there are exits down and right - */ - let mut walls; //2d vector - let width; - let height; - let entrance_column; //rng, column of entrance - let mut row; - let mut col; - let mut count; - - - - //print welcome message - println!(" - AMAZING PROGRAM - CREATIVE COMPUTING MORRISTOWN, NEW JERSEY\n\n\n\n"); - - //prompt for input - width = get_user_input("What is your width?"); - print!("\n"); //one blank line below - height = get_user_input("What is your height?"); - print!("\n\n\n\n");//4 blank lines below - - //generate maze - //initialize used and wall vectors - //2d vectors when you don't know the sizes at compile time are wierd, but here's how it's done :) - used = vec![0; (width * height) as usize]; - let mut used: Vec<_> = used.as_mut_slice().chunks_mut(width as usize).collect(); - let used = used.as_mut_slice(); //accessible as used[][] - //2d vectors when you don't know the sizes at compile time are wierd, but here's how it's done :) - walls = vec![0; (width * height) as usize]; - let mut walls: Vec<_> = walls.as_mut_slice().chunks_mut(width as usize).collect(); - let walls = walls.as_mut_slice(); //accessible as walls[][] - - entrance_column=rng.gen_range(0..width-1); - row = 0; - col = entrance_column; - count = 1; - used[row][col] = count; - count += 1; - - while count != width*height + 1 { - //remove possible directions that are blocked or - //hit cells already processed - let mut possible_directions: HashSet = vec![Direction::LEFT.val(),Direction::UP.val(),Direction::RIGHT.val(),Direction::DOWN.val()].into_iter().collect(); //create it as a vector bc that's easy, then convert it to a hashset - if col==0 || used[row][col-1]!=0 { - possible_directions.remove(&Direction::LEFT.val()); - } - if row==0 || used[row-1][col]!=0 { - possible_directions.remove(&Direction::UP.val()); - } - if col==width-1 || used[row][col+1]!=0 { - possible_directions.remove(&Direction::RIGHT.val()); - } - if row==height-1 || used[row+1][col]!=0 { - possible_directions.remove(&Direction::DOWN.val()); - } - - //If we can move in a direction, move and make opening - if possible_directions.len() != 0 { //all values in possible_directions are not NONE - let pos_dir_vec: Vec<_> = possible_directions.into_iter().collect(); // convert the set to a vector to get access to the choose method - //select a random direction - match pos_dir_vec.choose(&mut rng).expect("error") { - 0=> { - col -= 1; - walls[row][col] = EXIT_RIGHT; - }, - 1=> { - row -= 1; - walls[row][col] = EXIT_DOWN; - }, - 2=>{ - walls[row][col] = walls[row][col] + EXIT_RIGHT; - col += 1; - }, - 3=>{ - walls[row][col] = walls[row][col] + EXIT_DOWN; - row += 1; - }, - _=>{}, - } - used[row][col]=count; - count += 1; - } - //otherwise, move to the next used cell, and try again - else { - loop { - if col != width-1 {col += 1;} - else if row != height-1 {row+=1; col=0;} - else {row=0;col=0;} - - if used[row][col] != 0 {break;} - } - } - } - // Add a random exit - col=rng.gen_range(0..width); - row=height-1; - walls[row][col]+=1; - - //print maze - //first line - for c in 0..width { - if c == entrance_column { - print!(". "); - } - else { - print!(".--"); - } - } - println!("."); - //rest of maze - for r in 0..height { - print!("I"); - for c in 0..width { - if walls[r][c]<2 {print!(" I");} - else {print!(" ");} - } - println!(); - for c in 0..width { - if walls[r][c] == 0 || walls[r][c]==2 {print!(":--");} - else {print!(": ");} - } - println!("."); - } - - //stops the program from ending until you give input, useful when running a compiled .exe - println!("\n\npress ENTER to exit"); - io::stdin().read_line(&mut String::new()).expect("closing"); -} - -fn get_user_input(prompt: &str) -> usize { - //DATA - let mut raw_input = String::new(); // temporary variable for user input that can be parsed later - - //input loop - return loop { - - //print prompt - println!("{}", prompt); - - //read user input from standard input, and store it to raw_input - raw_input.clear(); //clear input - io::stdin().read_line(&mut raw_input).expect( "CANNOT READ INPUT!"); - - //from input, try to read a number - match raw_input.trim().parse::() { - Ok(i) => { - if i>1 { //min size 1 - break i; // this escapes the loop, returning i - } - else { - println!("INPUT OUT OF RANGE. TRY AGAIN."); - continue;// run the loop again - } - } - Err(e) => { - println!("MEANINGLESS DIMENSION. TRY AGAIN. {}", e.to_string().to_uppercase()); - continue; // run the loop again - } - }; - } -} From d30b6bac150d00b128338bd0d403d07ea05a87e3 Mon Sep 17 00:00:00 2001 From: AnthonyMichaelTDM <68485672+AnthonyMichaelTDM@users.noreply.github.com> Date: Thu, 3 Mar 2022 13:14:49 -0800 Subject: [PATCH 5/5] rust port of 61_Math_Dice --- 61_Math_Dice/rust/Cargo.toml | 9 +++ 61_Math_Dice/rust/README.md | 5 ++ 61_Math_Dice/rust/src/main.rs | 134 ++++++++++++++++++++++++++++++++++ 3 files changed, 148 insertions(+) create mode 100644 61_Math_Dice/rust/Cargo.toml create mode 100644 61_Math_Dice/rust/README.md create mode 100644 61_Math_Dice/rust/src/main.rs diff --git a/61_Math_Dice/rust/Cargo.toml b/61_Math_Dice/rust/Cargo.toml new file mode 100644 index 00000000..3b1d02f5 --- /dev/null +++ b/61_Math_Dice/rust/Cargo.toml @@ -0,0 +1,9 @@ +[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] +rand = "0.8.5" diff --git a/61_Math_Dice/rust/README.md b/61_Math_Dice/rust/README.md new file mode 100644 index 00000000..881f0dc1 --- /dev/null +++ b/61_Math_Dice/rust/README.md @@ -0,0 +1,5 @@ +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) + +If you wish to give the user more than 2 attempts to get the number, change value assigned to the num_tries variable at the start of the main function \ No newline at end of file diff --git a/61_Math_Dice/rust/src/main.rs b/61_Math_Dice/rust/src/main.rs new file mode 100644 index 00000000..35216933 --- /dev/null +++ b/61_Math_Dice/rust/src/main.rs @@ -0,0 +1,134 @@ +use rand::{Rng, prelude::{thread_rng}}; +use std::io; + +fn main() { + //DATA + let num_tries:u8 = 2; //number of tries the player gets each round, must be at least 1 + let mut rng = thread_rng(); + let mut user_guess: u8; + let mut dice_1:u8; + let mut dice_2:u8; + + //print welcome message + welcome(); + + //game loop + loop { + //roll dice + dice_1 = rng.gen_range(1..=6); + dice_2 = rng.gen_range(1..=6); + + //print dice + print_dice(dice_1); + println!(" +"); + print_dice(dice_2); + println!(" ="); + + //get user guess, they have 2 tries + for t in 0..num_tries { + //get guess + user_guess = get_number_from_user_input("", "That's not a valid number!", 1, 12); + + //if they get it wrong + if user_guess != (dice_1+dice_2) { + //print different message depending on what try they're on + if t < num_tries-1 { // user has tries left + println!("NO, COUNT THE SPOTS AND GIVE ANOTHER ANSWER."); + println!(" ="); + } + else { //this is their last try + println!("NO, THE ANSWER IS {}", dice_1+dice_2); + } + } + else { + println!("RIGHT!"); + break; + } + } + + //play again + println!("\nThe dice roll again...."); + } + +} + +/** + * prints the welcome message to the console + */ +fn welcome() { + println!(" + MATH DICE + CREATIVE COMPUTING MORRISTOWN, NEW JERSEY + \n\n + THIS PROGRAM GENERATES SUCCESSIVE PICTURES OF TWO DICE. + WHEN TWO DICE AND AN EQUAL SIGN FOLLOWED BY A QUESTION + MARK HAVE BEEN PRINTED, TYPE YOUR ANSWER AND THE RETURN KEY. + TO CONCLUDE THE LESSON, PRESS Ctrl+C AS YOUR ANSWER.\n + "); +} + +/** + * print the dice, + */ +fn print_dice(dice_value:u8) { + //data + + //top + println!(" ----- "); + //first layer + match dice_value { + 4|5|6 => println!("| * * |"), + 2|3 => println!("| * |"), + _=>println!("| |"), + } + + //second layer + match dice_value { + 1|3|5 => println!("| * |"), + 2|4 => println!("| |"), + _=>println!("| * * |"), + } + + //third layer + match dice_value { + 4|5|6 => println!("| * * |"), + 2|3 => println!("| * |"), + _=>println!("| |"), + } + + //bottom + println!(" ----- "); +} + +/** + * gets a integer from user input + */ +fn get_number_from_user_input(prompt: &str, error_message: &str, min:u8, max:u8) -> u8 { + //input loop + return loop { + let mut raw_input = String::new(); // temporary variable for user input that can be parsed later + + //print prompt + println!("{}", prompt); + //read user input from standard input, and store it to raw_input + //raw_input.clear(); //clear input + io::stdin().read_line(&mut raw_input).expect( "CANNOT READ INPUT!"); + + //from input, try to read a number + match raw_input.trim().parse::() { + Ok(i) => { + if i < min || i > max { //input out of desired range + println!("{} ({}-{})", error_message, min,max); + continue; // run the loop again + } + else { + break i;// this escapes the loop, returning i + } + }, + Err(e) => { + println!("{} {}", error_message, e.to_string().to_uppercase()); + continue; // run the loop again + } + }; + }; +} \ No newline at end of file