Skip to content

Commit 6e61790

Browse files
committed
feat(book): add guessing_game
0 parents  commit 6e61790

File tree

9 files changed

+260
-0
lines changed

9 files changed

+260
-0
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.history
2+
3+
/target
4+
/experiments

.vscode/settings.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"workbench.colorCustomizations": {
3+
"[Eva Light Bold]": {
4+
"terminal.ansiYellow": "#b8ac04",
5+
"terminal.ansiBrightBlack": "#f890c0"
6+
}
7+
},
8+
"conventionalCommits.scopes": [
9+
"book"
10+
]
11+
}

Cargo.lock

Lines changed: 93 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[workspace]
2+
members = ["experiments", "codewars/*", "book/*"]

LICENSE

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2013 Mike Gehard, Exercism
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy of
6+
this software and associated documentation files (the "Software"), to deal in
7+
the Software without restriction, including without limitation the rights to
8+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9+
the Software, and to permit persons to whom the Software is furnished to do so,
10+
subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

book/guessing_game/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/target

book/guessing_game/Cargo.lock

Lines changed: 85 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

book/guessing_game/Cargo.toml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[package]
2+
name = "guessing_game"
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"

book/guessing_game/src/main.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
use rand::Rng;
2+
use std::cmp::Ordering;
3+
use std::io;
4+
5+
fn main() {
6+
println!("Guess the number!");
7+
8+
let secret_number = rand::thread_rng().gen_range(1..101);
9+
10+
loop {
11+
println!("Please input your guess.");
12+
13+
let mut guess = String::new();
14+
15+
io::stdin()
16+
.read_line(&mut guess)
17+
.expect("Failed to read line");
18+
19+
let guess: u32 = match guess.trim().parse() {
20+
Ok(num) => num,
21+
Err(_) => continue,
22+
};
23+
24+
println!("You guessed {}", guess);
25+
26+
match guess.cmp(&secret_number) {
27+
Ordering::Less => println!("Bigger"),
28+
Ordering::Equal => {
29+
println!("You win!");
30+
break;
31+
}
32+
Ordering::Greater => println!("Less"),
33+
}
34+
}
35+
}

0 commit comments

Comments
 (0)