Skip to content

Commit cb9225a

Browse files
authored
Update game.rs
1 parent 8356104 commit cb9225a

File tree

1 file changed

+192
-1
lines changed

1 file changed

+192
-1
lines changed

hacker/atomic/src/game.rs

Lines changed: 192 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,192 @@
1-
1+
use colored::*;
2+
use std::io::{self};
3+
use rand::Rng;
4+
pub fn play_game() {
5+
loop {
6+
println!("{}", "========== Welcome to Hacker Adventure! ==========".purple().bold().on_black());
7+
println!("{}", "You are a fun-loving hacker trying to 'hack' into silly systems for laughs.".cyan().bold().on_black());
8+
println!("{}", "Choose your adventure level:".cyan().bold().on_black());
9+
println!("{}", "1. Easy (Coffee Machine Hack - Guess the PIN)".white().bold());
10+
println!("{}", "2. Medium (Cat Meme Database - Decode the Puzzle)".white().bold());
11+
println!("{}", "3. Hard (Alien UFO Control - Multi-Step Challenge)".white().bold());
12+
println!("{}", "4. Expert (Quantum Computer Hack - Advanced Riddles and Guesses)".white().bold());
13+
let mut input = String::new();
14+
io::stdin().read_line(&mut input).expect("Failed to read line");
15+
let choice: u32 = match input.trim().parse() {
16+
Ok(num) => num,
17+
Err(_) => {
18+
println!("{}", "Invalid choice! Defaulting to Medium.".red().bold().on_black());
19+
2
20+
}
21+
};
22+
let mut score = 0;
23+
let mut won = false;
24+
match choice {
25+
1 => {
26+
println!("{}", "Level 1: Hacking the Office Coffee Machine!".green().bold().on_black());
27+
println!("{}", "Guess the 4-digit PIN (0000-9999). You have 10 attempts.".cyan().on_black());
28+
let pin = rand::thread_rng().gen_range(0..10000);
29+
let mut attempts = 0;
30+
while attempts < 10 {
31+
attempts += 1;
32+
println!("{}", format!("Attempt {}/10: Enter PIN:", attempts).yellow().bold().on_black());
33+
let mut guess = String::new();
34+
io::stdin().read_line(&mut guess).expect("Failed to read line");
35+
let guess: u32 = match guess.trim().parse() {
36+
Ok(num) => num,
37+
Err(_) => continue,
38+
};
39+
if guess == pin {
40+
println!("{}", "Success! Coffee for everyone! +100 points.".green().bold().on_black());
41+
score += 100;
42+
won = true;
43+
break;
44+
} else if guess < pin {
45+
println!("{}", "Too low! The machine buzzes angrily.".yellow().on_black());
46+
} else {
47+
println!("{}", "Too high! The machine steams up.".yellow().on_black());
48+
}
49+
}
50+
if !won {
51+
println!("{}", format!("Failed! The PIN was {}. No coffee today.", pin).red().bold().on_black());
52+
}
53+
}
54+
2 => {
55+
println!("{}", "Level 2: Infiltrating the Cat Meme Database!".green().bold().on_black());
56+
println!("{}", "Solve the riddle to decode the access key.".cyan().on_black());
57+
let riddles = vec![
58+
("What has keys but can't open locks?", "keyboard"),
59+
("I'm light as a feather, but the strongest hacker can't hold me for much more than a minute. What am I?", "breath"),
60+
("What do you call a hacker who skips school?", "truant"),
61+
("What gets wetter as it dries?", "towel"),
62+
("I speak without a mouth and hear without ears. I have no body, but I come alive with the wind. What am I?", "echo"),
63+
];
64+
for (riddle, answer) in riddles {
65+
println!("{}", riddle.magenta().bold().on_black());
66+
let mut guess = String::new();
67+
io::stdin().read_line(&mut guess).expect("Failed to read line");
68+
if guess.trim().to_lowercase() == answer {
69+
println!("{}", "Correct! +50 points.".green().on_black());
70+
score += 50;
71+
} else {
72+
println!("{}", format!("Wrong! It was '{}'.", answer).red().on_black());
73+
}
74+
}
75+
if score >= 150 {
76+
won = true;
77+
println!("{}", "Database hacked! Endless cat memes unlocked!".green().bold().on_black());
78+
} else {
79+
println!("{}", "Access denied! Try harder next time.".red().bold().on_black());
80+
}
81+
}
82+
3 => {
83+
println!("{}", "Level 3: Taking over an Alien UFO!".green().bold().on_black());
84+
println!("{}", "Complete all challenges to win.".cyan().on_black());
85+
// Challenge 1: Guess number
86+
let num = rand::thread_rng().gen_range(1..101);
87+
let mut attempts = 0;
88+
let mut success = false;
89+
while attempts < 5 {
90+
attempts += 1;
91+
println!("{}", "Challenge 1: Guess the alien code (1-100):".yellow().bold().on_black());
92+
let mut guess = String::new();
93+
io::stdin().read_line(&mut guess).expect("Failed to read line");
94+
let guess: i32 = match guess.trim().parse() {
95+
Ok(num) => num,
96+
Err(_) => continue,
97+
};
98+
if guess == num {
99+
println!("{}", "Code cracked! +100 points.".green().on_black());
100+
score += 100;
101+
success = true;
102+
break;
103+
} else if guess < num {
104+
println!("{}", "Too low! Aliens chuckle.".yellow().on_black());
105+
} else {
106+
println!("{}", "Too high! UFO wobbles.".yellow().on_black());
107+
}
108+
}
109+
if !success {
110+
println!("{}", "Challenge failed! UFO escapes.".red().bold().on_black());
111+
continue;
112+
}
113+
// Challenge 2: Choose path
114+
println!("{}", "Challenge 2: Choose your hack path:".yellow().bold().on_black());
115+
println!("{}", "1. Brute force (risky)".white().bold());
116+
println!("{}", "2. Stealth mode (safe)".white().bold());
117+
let mut choice = String::new();
118+
io::stdin().read_line(&mut choice).expect("Failed to read line");
119+
if choice.trim() == "2" {
120+
println!("{}", "Stealth success! +100 points.".green().on_black());
121+
score += 100;
122+
} else {
123+
if rand::thread_rng().gen_bool(0.5) {
124+
println!("{}", "Brute force worked! +150 points.".green().on_black());
125+
score += 150;
126+
} else {
127+
println!("{}", "Brute force failed! -50 points.".red().on_black());
128+
score -= 50;
129+
}
130+
}
131+
// Challenge 3: Final puzzle
132+
println!("{}", "Final Challenge: What do hackers do at the beach?".magenta().bold().on_black());
133+
println!("{}", "Hint: It involves waves.".cyan().on_black());
134+
let mut guess = String::new();
135+
io::stdin().read_line(&mut guess).expect("Failed to read line");
136+
if guess.trim().to_lowercase().contains("surf") {
137+
println!("{}", "Correct! They surf the web. +200 points.".green().on_black());
138+
score += 200;
139+
won = true;
140+
} else {
141+
println!("{}", "Wrong! UFO self-destructs.".red().on_black());
142+
}
143+
}
144+
4 => {
145+
println!("{}", "Level 4: Hacking a Quantum Computer!".green().bold().on_black());
146+
println!("{}", "Solve advanced riddles and guess the quantum state.".cyan().on_black());
147+
let riddles = vec![
148+
("I am not alive, but I grow; I don't have lungs, but I need air; I don't have a mouth, but water kills me. What am I?", "fire"),
149+
("What can travel around the world while staying in a corner?", "stamp"),
150+
("What has a head, a tail, is brown, and has no legs?", "penny"),
151+
];
152+
for (riddle, answer) in riddles {
153+
println!("{}", riddle.magenta().bold().on_black());
154+
let mut guess = String::new();
155+
io::stdin().read_line(&mut guess).expect("Failed to read line");
156+
if guess.trim().to_lowercase() == answer {
157+
println!("{}", "Correct! +100 points.".green().on_black());
158+
score += 100;
159+
} else {
160+
println!("{}", format!("Wrong! It was '{}'.", answer).red().on_black());
161+
}
162+
}
163+
// Quantum guess: Even or odd
164+
let quantum = rand::thread_rng().gen_range(1..1001);
165+
println!("{}", "Final Quantum Challenge: Guess if the state is even or odd (number 1-1000).".yellow().bold().on_black());
166+
let mut guess = String::new();
167+
io::stdin().read_line(&mut guess).expect("Failed to read line");
168+
let is_even = quantum % 2 == 0;
169+
let guessed_even = guess.trim().to_lowercase() == "even";
170+
if (is_even && guessed_even) || (!is_even && !guessed_even) {
171+
println!("{}", "Quantum state hacked! +300 points.".green().on_black());
172+
score += 300;
173+
won = true;
174+
} else {
175+
println!("{}", format!("Wrong! It was {}. Quantum collapse!", if is_even { "even" } else { "odd" }).red().on_black());
176+
}
177+
}
178+
_ => continue,
179+
}
180+
println!("{}", format!("Your score: {}", score).blue().bold().on_black());
181+
if won {
182+
println!("{}", "You win the level!".green().bold().on_black());
183+
}
184+
println!("{}", "Play again? (y/n)".cyan().bold().on_black());
185+
let mut again = String::new();
186+
io::stdin().read_line(&mut again).expect("Failed to read line");
187+
if again.trim().to_lowercase() != "y" {
188+
break;
189+
}
190+
}
191+
println!("{}", "========== Thanks for playing Hacker Adventure! ==========".purple().bold().on_black());
192+
}

0 commit comments

Comments
 (0)