-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #717 from Jay-0331/main
Rust Translation for Dice
- Loading branch information
Showing
6 changed files
with
191 additions
and
8,656 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.