Skip to content

Commit 354c1f9

Browse files
Merge pull request #697 from Jay-0331/main
rust conversion of stars
2 parents 7224c4b + 34baeec commit 354c1f9

File tree

3 files changed

+97
-0
lines changed

3 files changed

+97
-0
lines changed

82_Stars/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 = "stars"
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.3"

82_Stars/rust/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Original source downloaded [from Vintage Basic](http://www.vintage-basic.net/games.html)
2+
3+
Conversion to [Rust](https://www.rust-lang.org/)

82_Stars/rust/src/main.rs

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
use rand::Rng;
2+
use std::io;
3+
4+
fn main() {
5+
println!(
6+
"{: >39}\n{: >57}\n\n\n",
7+
"STARS", "CREATIVE COMPUTING MORRISTOWN, NEW JERSEY"
8+
);
9+
// STARS - PEOPLE'S COMPUTER CENTER, MENLO PARK, CA
10+
// A IS LIMIT ON NUMBER, M IS NUMBER OF GUESSES
11+
let a: u32 = 101;
12+
let m: u32 = 7;
13+
let mut need_instrut = String::new();
14+
15+
println!("DO YOU WANT INSTRUCTIONS?");
16+
io::stdin()
17+
.read_line(&mut need_instrut)
18+
.expect("Failed to get input");
19+
20+
if need_instrut[..1].to_ascii_lowercase().eq("y") {
21+
println!("I AM THINKING OF A WHOLE NUMBER FROM 1 TO {}", a - 1);
22+
println!("TRY TO GUESS MY NUMBER. AFTER YOU GUESS, I");
23+
println!("WILL TYPE ONE OR MORE STARS (*). THE MORE");
24+
println!("STARS I TYPE, THE CLOSER YOU ARE TO MY NUMBER.");
25+
println!("ONE STAR (*) MEANS FAR AWAY, SEVEN STARS (*******)");
26+
println!("MEANS REALLY CLOSE! YOU GET {} GUESSES.\n\n", m);
27+
}
28+
29+
loop {
30+
println!("\nOK, I AM THINKING OF A NUMBER, START GUESSING.\n");
31+
let rand_number: i32 = rand::thread_rng().gen_range(1..a) as i32; // generates a random number between 1 and 100
32+
33+
// GUESSING BEGINS, HUMAN GETS M GUESSES
34+
for i in 0..m {
35+
let mut guess = String::new();
36+
println!("YOUR GUESS?");
37+
io::stdin()
38+
.read_line(&mut guess)
39+
.expect("Failed to get input");
40+
let guess: i32 = match guess.trim().parse() {
41+
Ok(num) => num,
42+
Err(_) => {
43+
println!("PLEASE ENTER A NUMBER VALUE.\n");
44+
continue;
45+
}
46+
};
47+
if guess == rand_number {
48+
print!("");
49+
for _i in 0..50 {
50+
print!("*");
51+
}
52+
println!("!!!");
53+
println!("YOU GOT IT IN {} GUESSES!!! LET'S PLAY AGAIN...\n", i + 1);
54+
break;
55+
} else {
56+
match_guess(rand_number - guess);
57+
}
58+
59+
if i == 6 {
60+
println!(
61+
"SORRY, THAT'S {} GUESSES. THE NUMBER WAS {}",
62+
m, rand_number
63+
);
64+
}
65+
}
66+
}
67+
}
68+
69+
fn match_guess(diff: i32) {
70+
if diff.abs() >= 64 {
71+
println!("*\n");
72+
} else if diff.abs() >= 32 {
73+
println!("**\n");
74+
} else if diff.abs() >= 16 {
75+
println!("***\n");
76+
} else if diff.abs() >= 8 {
77+
println!("****\n");
78+
} else if diff.abs() >= 4 {
79+
println!("*****\n");
80+
} else if diff.abs() >= 2 {
81+
println!("******\n");
82+
} else {
83+
println!("*******\n");
84+
}
85+
}

0 commit comments

Comments
 (0)