```
feat(day1): 新增猜数字游戏项目 - 添加 Rust 项目基础结构 (Cargo.toml, Cargo.lock) - 实现猜数字游戏核心逻辑 - 集成随机数生成依赖 (rand 0.10.1) - 添加 .gitignore 配置排除 target 目录 - 实现基本游戏循环和用户交互功能 ```
This commit is contained in:
parent
cf27076fec
commit
8ac440532e
|
|
@ -0,0 +1 @@
|
||||||
|
target/
|
||||||
|
|
@ -0,0 +1,77 @@
|
||||||
|
# This file is automatically @generated by Cargo.
|
||||||
|
# It is not intended for manual editing.
|
||||||
|
version = 4
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "cfg-if"
|
||||||
|
version = "1.0.4"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "9330f8b2ff13f34540b44e946ef35111825727b38d33286ef986142615121801"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "chacha20"
|
||||||
|
version = "0.10.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "d524456ba66e72eb8b115ff89e01e497f8e6d11d78b70b1aa13c0fbd97540a81"
|
||||||
|
dependencies = [
|
||||||
|
"cfg-if",
|
||||||
|
"cpufeatures",
|
||||||
|
"rand_core",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "cpufeatures"
|
||||||
|
version = "0.3.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "8b2a41393f66f16b0823bb79094d54ac5fbd34ab292ddafb9a0456ac9f87d201"
|
||||||
|
dependencies = [
|
||||||
|
"libc",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "day1"
|
||||||
|
version = "0.1.0"
|
||||||
|
dependencies = [
|
||||||
|
"rand",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "getrandom"
|
||||||
|
version = "0.4.3"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "300e883d756b2e4ec94e02791f39b04b522276138852cfc41d9fb7e904106099"
|
||||||
|
dependencies = [
|
||||||
|
"cfg-if",
|
||||||
|
"libc",
|
||||||
|
"r-efi",
|
||||||
|
"rand_core",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "libc"
|
||||||
|
version = "0.2.186"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "68ab91017fe16c622486840e4c83c9a37afeff978bd239b5293d61ece587de66"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "r-efi"
|
||||||
|
version = "6.0.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "f8dcc9c7d52a811697d2151c701e0d08956f92b0e24136cf4cf27b57a6a0d9bf"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "rand"
|
||||||
|
version = "0.10.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "d2e8e8bcc7961af1fdac401278c6a831614941f6164ee3bf4ce61b7edb162207"
|
||||||
|
dependencies = [
|
||||||
|
"chacha20",
|
||||||
|
"getrandom",
|
||||||
|
"rand_core",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "rand_core"
|
||||||
|
version = "0.10.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "63b8176103e19a2643978565ca18b50549f6101881c443590420e4dc998a3c69"
|
||||||
|
|
@ -0,0 +1,7 @@
|
||||||
|
[package]
|
||||||
|
name = "day1"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2024"
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
rand = "0.10.1"
|
||||||
|
|
@ -0,0 +1,70 @@
|
||||||
|
use std::{cmp::Ordering, io};
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
println!("猜数游戏");
|
||||||
|
let secret_number: u32 = rand::random_range(1..=100);
|
||||||
|
// println!("The secret number is: {}", secret_number);
|
||||||
|
|
||||||
|
let mut count = 0;
|
||||||
|
loop {
|
||||||
|
let mut guess = String::new();
|
||||||
|
|
||||||
|
println!("请输入你的猜测:");
|
||||||
|
io::stdin().read_line(&mut guess).expect("读取输入错误!");
|
||||||
|
let guess_number: u32 = match guess.trim().parse() {
|
||||||
|
Ok(num) => num,
|
||||||
|
Err(_) => {
|
||||||
|
println!("请输入数字!");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
count += 1;
|
||||||
|
|
||||||
|
match guess_number.cmp(&secret_number) {
|
||||||
|
Ordering::Less => println!("小了!"),
|
||||||
|
Ordering::Greater => println!("大了!"),
|
||||||
|
Ordering::Equal => {
|
||||||
|
println!("猜对了!");
|
||||||
|
println!("你一共猜了{}次", count);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 猜数字游戏 — 可指定数字范围,返回猜测次数
|
||||||
|
fn guess_game(low: u32, high: u32) -> u32 {
|
||||||
|
let secret = rand::random_range(low..=high);
|
||||||
|
let mut guess = String::new();
|
||||||
|
let mut count = 0;
|
||||||
|
|
||||||
|
loop {
|
||||||
|
guess.clear();
|
||||||
|
println!("请输入你的猜测 ({low}~{high}):");
|
||||||
|
io::stdin().read_line(&mut guess).expect("读取输入错误!");
|
||||||
|
|
||||||
|
let guess_num: u32 = match guess.trim().parse() {
|
||||||
|
Ok(n) if n < low || n > high => {
|
||||||
|
println!("数字不在范围内,请重新输入!");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
Ok(n) => n,
|
||||||
|
Err(_) => {
|
||||||
|
println!("请输入有效的数字!");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
count += 1;
|
||||||
|
|
||||||
|
match guess_num.cmp(&secret) {
|
||||||
|
Ordering::Less => println!("小了!"),
|
||||||
|
Ordering::Greater => println!("大了!"),
|
||||||
|
Ordering::Equal => {
|
||||||
|
println!("猜对了! 答案是 {secret},你一共猜了 {count} 次");
|
||||||
|
return count;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue