guessing game!

This commit is contained in:
Emily 2020-07-08 15:09:59 +10:00
parent c98e3fa6ed
commit 1ede30c296
171 changed files with 130 additions and 8 deletions

2
guessing_game/.gitignore vendored Normal file
View file

@ -0,0 +1,2 @@
target/
Cargo.lock

View file

@ -1,5 +1,84 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
[[package]]
name = "cfg-if"
version = "0.1.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822"
[[package]]
name = "getrandom"
version = "0.1.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7abc8dd8451921606d809ba32e95b6111925cd2906060d2dcc29c070220503eb"
dependencies = [
"cfg-if",
"libc",
"wasi",
]
[[package]]
name = "guessing_game"
version = "0.1.0"
dependencies = [
"rand",
]
[[package]]
name = "libc"
version = "0.2.72"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a9f8082297d534141b30c8d39e9b1773713ab50fdbe4ff30f750d063b3bfd701"
[[package]]
name = "ppv-lite86"
version = "0.2.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "237a5ed80e274dbc66f86bd59c1e25edc039660be53194b5fe0a482e0f2612ea"
[[package]]
name = "rand"
version = "0.7.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6a6b1679d49b24bbfe0c803429aa1874472f50d9b363131f0e89fc356b544d03"
dependencies = [
"getrandom",
"libc",
"rand_chacha",
"rand_core",
"rand_hc",
]
[[package]]
name = "rand_chacha"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f4c8ed856279c9737206bf725bf36935d8666ead7aa69b52be55af369d193402"
dependencies = [
"ppv-lite86",
"rand_core",
]
[[package]]
name = "rand_core"
version = "0.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "90bde5296fc891b0cef12a6d03ddccc162ce7b2aff54160af9338f8d40df6d19"
dependencies = [
"getrandom",
]
[[package]]
name = "rand_hc"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ca3129af7b92a17112d59ad498c6f81eaf463253766b90396d39ea7a39d6613c"
dependencies = [
"rand_core",
]
[[package]]
name = "wasi"
version = "0.9.0+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cccddf32554fecc6acb585f82a32a72e28b48f8c4c1883ddfeeeaa96f7d8e519"

View file

@ -7,3 +7,4 @@ edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
rand = "0.7.3"

View file

@ -1,12 +1,52 @@
/*
* Simple number guessing game I made using the tutorial at
* https://doc.rust-lang.org/stable/book/ch02-00-guessing-game-tutorial.html
*/
use std::io;
use std::cmp::Ordering;
use rand::Rng;
fn main() {
println!("Number guessing game!");
print!("Choose a number between 0 and 100: ")
// Generates random number between 0 and 100, thread_rng is the generator we're using
let number = rand::thread_rng().gen_range(0, 101);
let mut tries = 1;
println!("Choose a number between 0 and 100: ");
let mut guess = String::new();
loop {
// Defines a mutable variable that returns a string, don't know why constants exist yet
let mut guess = String::new();
io.stdin()
.read_line(&mut guess)
// the & symbol in front of mut means its a reference, whatever that means lol
io::stdin()
.read_line(&mut guess)
.expect("Failed to read line.");
// Remove whitespace and convert the input into an unsigned 32 bit integer, using match statement to handle err
let guess: u32 = match guess.trim().parse() {
Ok(num) => num,
Err(_) => {
println!("Invalid input, please type a number!");
continue;
}
};
// Checks whether the guess is higher, lower or equal to the random number
// I added a tries thing myself, that tells you how many turns it took for you to guess the number
match guess.cmp(&number) {
Ordering::Less => {
println!("Higher!");
tries += 1;
}
Ordering::Greater => {
println!("Lower!");
tries += 1;
}
Ordering::Equal => {
println!("You win! You took {} tries to guess the number.", tries);
break;
}
}
}
}

View file

@ -1 +0,0 @@
{"config":{"output_file":null,"full_docs":false,"pub_only":false,"reachable_only":false,"distro_crate":false,"signatures":false,"borrow_data":false},"version":"0.19.0","compilation":{"directory":"/home/emily/Documents/Projects/rust-practice/guessing_game","program":"/home/emily/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/bin/rls","arguments":[],"output":"/home/emily/Documents/Projects/rust-practice/guessing_game/target/rls/debug/deps/libguessing_game-26c3e7d98df99986.rmeta"},"prelude":{"crate_id":{"name":"guessing_game","disambiguator":[14273697690769000070,13341332783349054977]},"crate_root":"src","external_crates":[{"file_name":"/home/emily/Documents/Projects/rust-practice/guessing_game/src/main.rs","num":1,"id":{"name":"std","disambiguator":[12673765241971086519,1979875191208036248]}},{"file_name":"/home/emily/Documents/Projects/rust-practice/guessing_game/src/main.rs","num":2,"id":{"name":"core","disambiguator":[16221439354469382097,15565837348772828253]}},{"file_name":"/home/emily/Documents/Projects/rust-practice/guessing_game/src/main.rs","num":3,"id":{"name":"compiler_builtins","disambiguator":[15613252303691562375,4556165055238676072]}},{"file_name":"/home/emily/Documents/Projects/rust-practice/guessing_game/src/main.rs","num":4,"id":{"name":"rustc_std_workspace_core","disambiguator":[12269617896898487533,65142111397584226]}},{"file_name":"/home/emily/Documents/Projects/rust-practice/guessing_game/src/main.rs","num":5,"id":{"name":"alloc","disambiguator":[15862783141653200230,4773813417861420487]}},{"file_name":"/home/emily/Documents/Projects/rust-practice/guessing_game/src/main.rs","num":6,"id":{"name":"libc","disambiguator":[501305773035659233,3889300012469539238]}},{"file_name":"/home/emily/Documents/Projects/rust-practice/guessing_game/src/main.rs","num":7,"id":{"name":"unwind","disambiguator":[9126002216567348836,15610270334556716503]}},{"file_name":"/home/emily/Documents/Projects/rust-practice/guessing_game/src/main.rs","num":8,"id":{"name":"cfg_if","disambiguator":[9479301707518706798,13352731276321566042]}},{"file_name":"/home/emily/Documents/Projects/rust-practice/guessing_game/src/main.rs","num":9,"id":{"name":"backtrace","disambiguator":[15203654865662241151,17897348793673631766]}},{"file_name":"/home/emily/Documents/Projects/rust-practice/guessing_game/src/main.rs","num":10,"id":{"name":"rustc_demangle","disambiguator":[10290502124864695413,7672610073830174493]}},{"file_name":"/home/emily/Documents/Projects/rust-practice/guessing_game/src/main.rs","num":11,"id":{"name":"backtrace_sys","disambiguator":[14838544226648722576,13006716085524361330]}},{"file_name":"/home/emily/Documents/Projects/rust-practice/guessing_game/src/main.rs","num":12,"id":{"name":"hashbrown","disambiguator":[9246889555580827340,16878203682218964580]}},{"file_name":"/home/emily/Documents/Projects/rust-practice/guessing_game/src/main.rs","num":13,"id":{"name":"rustc_std_workspace_alloc","disambiguator":[8284748314555817558,4769950866534510046]}},{"file_name":"/home/emily/Documents/Projects/rust-practice/guessing_game/src/main.rs","num":14,"id":{"name":"panic_unwind","disambiguator":[14671874173783541769,17969261176539820175]}}],"span":{"file_name":"src/main.rs","byte_start":0,"byte_end":208,"line_start":1,"line_end":12,"column_start":1,"column_end":2}},"imports":[{"kind":"Use","ref_id":{"krate":1,"index":2274},"span":{"file_name":"src/main.rs","byte_start":9,"byte_end":11,"line_start":1,"line_end":1,"column_start":10,"column_end":12},"alias_span":null,"name":"io","value":"","parent":{"krate":0,"index":0}}],"defs":[{"kind":"Mod","id":{"krate":0,"index":0},"span":{"file_name":"src/main.rs","byte_start":0,"byte_end":208,"line_start":1,"line_end":12,"column_start":1,"column_end":2},"name":"","qualname":"::","value":"src/main.rs","parent":null,"children":[{"krate":0,"index":1},{"krate":0,"index":2},{"krate":0,"index":3},{"krate":0,"index":4}],"decl_id":null,"docs":"","sig":null,"attributes":[]},{"kind":"Function","id":{"krate":0,"index":4},"span":{"file_name":"src/main.rs","byte_start":17,"byte_end":21,"line_start":3,"line_end":3,"column_start":4,"column_end":8},"name":"main","qualname":"::main","value":"fn () -> ()","parent":null,"children":[],"decl_id":null,"docs":"","sig":null,"attributes":[]}],"impls":[],"refs":[{"kind":"Mod","span":{"file_name":"src/main.rs","byte_start":4,"byte_end":7,"line_start":1,"line_end":1,"column_start":5,"column_end":8},"ref_id":{"krate":1,"index":0}}],"macro_refs":[],"relations":[]}

File diff suppressed because one or more lines are too long

Some files were not shown because too many files have changed in this diff Show more