From 9480cf199e06e48cfe477a15cae831a8c70b9a43 Mon Sep 17 00:00:00 2001 From: AnthonyMichaelTDM <68485672+AnthonyMichaelTDM@users.noreply.github.com> Date: Sat, 5 Mar 2022 20:36:28 -0800 Subject: [PATCH] rust port of 66_Number --- 66_Number/rust/Cargo.toml | 10 ++++++ 66_Number/rust/README.md | 9 ++++++ 66_Number/rust/src/main.rs | 63 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 82 insertions(+) create mode 100644 66_Number/rust/Cargo.toml create mode 100644 66_Number/rust/README.md create mode 100644 66_Number/rust/src/main.rs diff --git a/66_Number/rust/Cargo.toml b/66_Number/rust/Cargo.toml new file mode 100644 index 00000000..22ab0ba8 --- /dev/null +++ b/66_Number/rust/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "rust" +version = "0.1.0" +authors = ["AnthonyMichaelTDM <68485672+AnthonyMichaelTDM@users.noreply.github.com>"] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +rand = "0.8.5" diff --git a/66_Number/rust/README.md b/66_Number/rust/README.md new file mode 100644 index 00000000..8bf7896b --- /dev/null +++ b/66_Number/rust/README.md @@ -0,0 +1,9 @@ +Original source downloaded [from Vintage Basic](http://www.vintage-basic.net/games.html) + +Conversion to [Ruby](https://www.ruby-lang.org/en/) + +Converted to Ruby (with tons of inspiration from the Python version) by @marcheiligers + +Run `ruby amazing.rb`. + +Run `DEBUG=1 ruby amazing.ruby` to see how it works (requires at least Ruby 2.7). diff --git a/66_Number/rust/src/main.rs b/66_Number/rust/src/main.rs new file mode 100644 index 00000000..a63300d0 --- /dev/null +++ b/66_Number/rust/src/main.rs @@ -0,0 +1,63 @@ +use rand::{Rng, prelude::thread_rng}; +use std::io; + +fn main() { + //DATA + let mut points: usize = 100; + let mut rng = thread_rng(); + let mut number:u8; + + //print welcome message + welcome(); + + //game loop + while points <= 500 { + //generate number + number = rng.gen_range(1..=5); + //NOTE: while looking at the original basic, I realized that the outcome of your guess is effectively random + //so instead of generating 5 variables with random values between 1-5 and doing something depedning which one has the value they guess... + //why not just let them "guess" and do a random action without using uneeded variables? .. so that's what I did. + + //let them "guess" + println!("GUESS A NUMBER FROM 1 TO 5");//print prompt + if let Ok(_i) = io::stdin().read_line(&mut String::new()) {} // get input from standard in, and do nothing with it even if an error is thrown + + //do something depending on the previously generated random number + match number { + 1 => if points>=5{points -= 5},//the if statement here prevents overflow, points is stored as an unsigned integer, so we can't let it be negative + 2 => points += 5, + 3 => {//jackpot + points *= 2; + println!("YOU HIT THE JACKPOT!!!"); + }, + 4 => points += 1, + 5 => points /= 2, + _ => {}, + }; + + //tell then how many points they have + println!("YOU HAVE {} POINTS.", points); + } + + //print +} + +/** + * print the welcome message + */ +fn welcome() { + println!(" + CREATIVE COMPUTING MORRISTOWN, NEW JERSEY + + + + YOU HAVE 100 POINTS. BY GUESSING NUMBERS FROM 1 TO 5, YOU + CAN GAIN OR LOSE POINTS DEPENDING UPON HOW CLOSE YOU GET TO + A RANDOM NUMBER SELECTED BY THE COMPUTER. + + YOU OCCASIONALLY WILL GET A JACKPOT WHICH WILL DOUBLE(!) + YOUR POINT COUNT. YOU WIN WHEN YOU GET 500 POINTS + + "); +} +