diff --git a/33_Dice/rust/Cargo.toml b/33_Dice/rust/Cargo.toml new file mode 100644 index 00000000..3b1d02f5 --- /dev/null +++ b/33_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/33_Dice/rust/README.md b/33_Dice/rust/README.md new file mode 100644 index 00000000..fc6468b9 --- /dev/null +++ b/33_Dice/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/) diff --git a/33_Dice/rust/src/main.rs b/33_Dice/rust/src/main.rs new file mode 100644 index 00000000..8c7e5885 --- /dev/null +++ b/33_Dice/rust/src/main.rs @@ -0,0 +1,85 @@ +//////////////////////////////////////////////////////////////////////////////////////////////////////////////// +// +// Dice +// +// From: BASIC Computer Games (1978) +// Edited by David H. Ahl +// +// "Not exactly a game, this program simulates rolling +// a pair of dice a large number of times and prints out +// the frequency distribution. You simply input the +// number of rolls. It is interesting to see how many +// rolls are necessary to approach the theoretical +// distribution: +// +// 2 1/36 2.7777...% +// 3 2/36 5.5555...% +// 4 3/36 8.3333...% +// etc. +// +// "Daniel Freidus wrote this program while in the +// seventh grade at Harrison Jr-Sr High School, +// Harrison, New York." +// +// Rust Port by Jay, 2022 +// +//////////////////////////////////////////////////////////////////////////////////////////////////////////////// + +use rand::Rng; +use std::io::{self, Write}; + +fn main() { + let mut frequency: [i32; 13] = [0; 13]; + println!( + "{: >38}\n{: >57}\n\n", + "DICE", "CREATIVE COMPUTING MORRISTOWN, NEW JERSEY" + ); + // DANNY FREIDUS + println!("THIS PROGRAM SIMULATES THE ROLLING OF A"); + println!("PAIR OF DICE."); + println!("YOU ENTER THE NUMBER OF TIMES YOU WANT THE COMPUTER TO"); + println!("'ROLL' THE DICE. WATCH OUT, VERY LARGE NUMBERS TAKE"); + println!("A LONG TIME. IN PARTICULAR, NUMBERS OVER 5000."); + let mut playing = true; + while playing { + let n = match readinput(&"HOW MANY ROLLS").trim().parse::() { + Ok(num) => num, + Err(_) => { + println!("PLEASE ENTER A NUMBER"); + continue; + } + }; + // Dice Rolled n times + for _i in 0..n { + let die_1 = rand::thread_rng().gen_range(1..=6); + let die_2 = rand::thread_rng().gen_range(1..=6); + let total = die_1 + die_2; + frequency[total] += 1; + } + + // Results tabel + println!("\nTOTAL SPOTS NUMBER OF TIMES"); + for i in 2..13 { + println!("{:^4}\t\t{}", i, frequency[i]); + } + + // Continue the game + let reply = readinput("TRY AGAIN").to_ascii_uppercase(); + if reply.starts_with("Y") || reply.eq("YES") { + frequency = [0; 13]; + } else { + playing = false; + } + } +} + +// function for getting input on same line +fn readinput(str: &str) -> String { + print!("\n{}? ", str); + let mut input = String::new(); + io::stdout().flush().unwrap(); + io::stdin() + .read_line(&mut input) + .expect("Failed to get Input"); + input +}