Add guessing_game

This commit is contained in:
daleclack 2022-07-01 12:41:08 +08:00
parent 1cd94553f4
commit dc67163bac
4 changed files with 165 additions and 0 deletions

View File

@ -0,0 +1,47 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "lldb",
"request": "launch",
"name": "Debug executable 'guessing_game'",
"cargo": {
"args": [
"build",
"--bin=guessing_game",
"--package=guessing_game",
"--release"
],
"filter": {
"name": "guessing_game",
"kind": "bin"
}
},
"args": [],
"cwd": "${workspaceFolder}"
},
{
"type": "lldb",
"request": "launch",
"name": "Debug unit tests in executable 'guessing_game'",
"cargo": {
"args": [
"test",
"--no-run",
"--bin=guessing_game",
"--package=guessing_game",
"--release"
],
"filter": {
"name": "guessing_game",
"kind": "bin"
}
},
"args": [],
"cwd": "${workspaceFolder}"
}
]
}

75
Rust_tests/guessing_game/Cargo.lock generated Normal file
View File

@ -0,0 +1,75 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "cfg-if"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "getrandom"
version = "0.2.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4eb1a864a501629691edf6c15a593b7a51eebaa1e8468e9ddc623de7c9b58ec6"
dependencies = [
"cfg-if",
"libc",
"wasi",
]
[[package]]
name = "guessing_game"
version = "0.1.0"
dependencies = [
"rand",
]
[[package]]
name = "libc"
version = "0.2.126"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "349d5a591cd28b49e1d1037471617a32ddcda5731b99419008085f72d5a53836"
[[package]]
name = "ppv-lite86"
version = "0.2.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "eb9f9e6e233e5c4a35559a617bf40a4ec447db2e84c20b55a6f83167b7e57872"
[[package]]
name = "rand"
version = "0.8.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404"
dependencies = [
"libc",
"rand_chacha",
"rand_core",
]
[[package]]
name = "rand_chacha"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88"
dependencies = [
"ppv-lite86",
"rand_core",
]
[[package]]
name = "rand_core"
version = "0.6.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d34f1408f55294453790c48b2f1ebbb1c5b4b7563eb1f418bcfcfdbb06ebb4e7"
dependencies = [
"getrandom",
]
[[package]]
name = "wasi"
version = "0.11.0+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423"

View File

@ -0,0 +1,9 @@
[package]
name = "guessing_game"
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"

View File

@ -0,0 +1,34 @@
use rand::Rng;
use std::cmp::Ordering;
use std::io;
fn main() {
println!("Guess number!");
let test_number = rand::thread_rng().gen_range(1..100); //Generate a random number
println!("The test number: {}", test_number);
loop {
let mut guess = String::new();
println!("Input a number");
io::stdin().read_line(&mut guess).expect("Can't read line"); //Read the number
let guess: u32 = match guess.trim().parse(){ //Use match to handle errors when parse string to number
Ok(num) => num,
Err(_) => continue,
}; //Transform string input to number
println!("The number you guess is: {}", guess); //Print the numbers
match guess.cmp(&test_number) {
//Compare the input number and the generated number
Ordering::Less => println!("Too small!"),
Ordering::Greater => println!("Too big!"),
Ordering::Equal =>{
println!("Winned!");
break;
} ,
}
}
}