Skip to content

Commit d76b2f1

Browse files
Merge pull request #597 from AnthonyMichaelTDM/rust-port-61_Math_Dice
Rust port of 61_Math_Dice
2 parents 61ee0dc + d30b6ba commit d76b2f1

File tree

3 files changed

+148
-0
lines changed

3 files changed

+148
-0
lines changed

61_Math_Dice/rust/Cargo.toml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[package]
2+
name = "rust"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
7+
8+
[dependencies]
9+
rand = "0.8.5"

61_Math_Dice/rust/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Original source downloaded [from Vintage Basic](http://www.vintage-basic.net/games.html)
2+
3+
Conversion to [Rust](https://www.rust-lang.org/) by Anthony Rubick [AnthonyMichaelTDM](https://github.com/AnthonyMichaelTDM)
4+
5+
If you wish to give the user more than 2 attempts to get the number, change value assigned to the num_tries variable at the start of the main function

61_Math_Dice/rust/src/main.rs

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
use rand::{Rng, prelude::{thread_rng}};
2+
use std::io;
3+
4+
fn main() {
5+
//DATA
6+
let num_tries:u8 = 2; //number of tries the player gets each round, must be at least 1
7+
let mut rng = thread_rng();
8+
let mut user_guess: u8;
9+
let mut dice_1:u8;
10+
let mut dice_2:u8;
11+
12+
//print welcome message
13+
welcome();
14+
15+
//game loop
16+
loop {
17+
//roll dice
18+
dice_1 = rng.gen_range(1..=6);
19+
dice_2 = rng.gen_range(1..=6);
20+
21+
//print dice
22+
print_dice(dice_1);
23+
println!(" +");
24+
print_dice(dice_2);
25+
println!(" =");
26+
27+
//get user guess, they have 2 tries
28+
for t in 0..num_tries {
29+
//get guess
30+
user_guess = get_number_from_user_input("", "That's not a valid number!", 1, 12);
31+
32+
//if they get it wrong
33+
if user_guess != (dice_1+dice_2) {
34+
//print different message depending on what try they're on
35+
if t < num_tries-1 { // user has tries left
36+
println!("NO, COUNT THE SPOTS AND GIVE ANOTHER ANSWER.");
37+
println!(" =");
38+
}
39+
else { //this is their last try
40+
println!("NO, THE ANSWER IS {}", dice_1+dice_2);
41+
}
42+
}
43+
else {
44+
println!("RIGHT!");
45+
break;
46+
}
47+
}
48+
49+
//play again
50+
println!("\nThe dice roll again....");
51+
}
52+
53+
}
54+
55+
/**
56+
* prints the welcome message to the console
57+
*/
58+
fn welcome() {
59+
println!("
60+
MATH DICE
61+
CREATIVE COMPUTING MORRISTOWN, NEW JERSEY
62+
\n\n
63+
THIS PROGRAM GENERATES SUCCESSIVE PICTURES OF TWO DICE.
64+
WHEN TWO DICE AND AN EQUAL SIGN FOLLOWED BY A QUESTION
65+
MARK HAVE BEEN PRINTED, TYPE YOUR ANSWER AND THE RETURN KEY.
66+
TO CONCLUDE THE LESSON, PRESS Ctrl+C AS YOUR ANSWER.\n
67+
");
68+
}
69+
70+
/**
71+
* print the dice,
72+
*/
73+
fn print_dice(dice_value:u8) {
74+
//data
75+
76+
//top
77+
println!(" ----- ");
78+
//first layer
79+
match dice_value {
80+
4|5|6 => println!("| * * |"),
81+
2|3 => println!("| * |"),
82+
_=>println!("| |"),
83+
}
84+
85+
//second layer
86+
match dice_value {
87+
1|3|5 => println!("| * |"),
88+
2|4 => println!("| |"),
89+
_=>println!("| * * |"),
90+
}
91+
92+
//third layer
93+
match dice_value {
94+
4|5|6 => println!("| * * |"),
95+
2|3 => println!("| * |"),
96+
_=>println!("| |"),
97+
}
98+
99+
//bottom
100+
println!(" ----- ");
101+
}
102+
103+
/**
104+
* gets a integer from user input
105+
*/
106+
fn get_number_from_user_input(prompt: &str, error_message: &str, min:u8, max:u8) -> u8 {
107+
//input loop
108+
return loop {
109+
let mut raw_input = String::new(); // temporary variable for user input that can be parsed later
110+
111+
//print prompt
112+
println!("{}", prompt);
113+
//read user input from standard input, and store it to raw_input
114+
//raw_input.clear(); //clear input
115+
io::stdin().read_line(&mut raw_input).expect( "CANNOT READ INPUT!");
116+
117+
//from input, try to read a number
118+
match raw_input.trim().parse::<u8>() {
119+
Ok(i) => {
120+
if i < min || i > max { //input out of desired range
121+
println!("{} ({}-{})", error_message, min,max);
122+
continue; // run the loop again
123+
}
124+
else {
125+
break i;// this escapes the loop, returning i
126+
}
127+
},
128+
Err(e) => {
129+
println!("{} {}", error_message, e.to_string().to_uppercase());
130+
continue; // run the loop again
131+
}
132+
};
133+
};
134+
}

0 commit comments

Comments
 (0)