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 Translation for Dice #717

Merged
merged 2 commits into from
Apr 14, 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
9 changes: 9 additions & 0 deletions 33_Dice/rust/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"
3 changes: 3 additions & 0 deletions 33_Dice/rust/README.md
Original file line number Diff line number Diff line change
@@ -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/)
85 changes: 85 additions & 0 deletions 33_Dice/rust/src/main.rs
Original file line number Diff line number Diff line change
@@ -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::<i32>() {
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
}
2,834 changes: 7 additions & 2,827 deletions 82_Stars/rust/Cargo.toml

Large diffs are not rendered by default.

2,693 changes: 2 additions & 2,691 deletions 82_Stars/rust/README.md

Large diffs are not rendered by default.

3,223 changes: 85 additions & 3,138 deletions 82_Stars/rust/src/main.rs

Large diffs are not rendered by default.