Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rust port of 66_Number #607

Merged
merged 1 commit into from
Mar 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 10 additions & 0 deletions 66_Number/rust/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"
9 changes: 9 additions & 0 deletions 66_Number/rust/README.md
Original file line number Diff line number Diff line change
@@ -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).
63 changes: 63 additions & 0 deletions 66_Number/rust/src/main.rs
Original file line number Diff line number Diff line change
@@ -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

");
}