src/main.rs aktualisiert

This commit is contained in:
robin 2024-08-06 15:12:14 +02:00
parent ae920f8598
commit 7b47cdc1a6

View file

@ -1,35 +1,67 @@
use rand::Rng;
use std::io;
use std::fs::{read, File};
use std::process::exit;
use std::{fs, io};
fn main() {
game_loop();
start();
}
fn invalid_input(function: i8) {
println!("Invalid Input");
match function {
1 => menu(),
2 => store(),
3=> {
println!("Please input a valid name");
start()
}
_ => panic!("A fatal Error occurred"),
}
}
fn start() {
println!("Hello what is your Name?");
let mut name = String::new();
io::stdin().read_line(&mut name).expect("error");
if name.is_empty() == true{
invalid_input(3)
}else {
println!("Hello {}", name);
menu();}
}
fn menu() {
println!("Start the Game:s Quit:q let somebody else play:e ");
let mut doing = String::new();
io::stdin().read_line(&mut doing).expect("An fatal error");
match doing.trim() {
"q" => exit(1),
"e" => start(),
"s" => game_loop(),
_ => invalid_input(1),
}
}
fn game_loop() {
let mut score: i8 = 0;
while score < 32 {
println!("Dein score ist: {}", score);
println!("Vor dir sind drei Türen, eine von ihnen tötet dich. Durch welche gehst du? Gib eine Zahl zwischen 1 und 3 ein");
loop {
println!("Your score is: {}", score);
println!("In front of you are three doors, one of them kills you.Which one do you choose? Enter a number between 1 and 3.");
let secretnumber: i8 = rand::thread_rng().gen_range(1..=3);
let guess = input();
let ergebnis = vergleich(guess, secretnumber);
if ergebnis == false {
println!(
"Du hast überlebt, der Geist war hinter der Tür {}",
"You survived, the ghost was behind the {} door",
secretnumber
);
score = score + 1;
}else if score == 32{
println!("You won");
break
} else {
println!("Du bist tot");
score = 0;
let mut idk: bool = idk();
if idk == true {
} else {
panic!()
}
println!("You are death");
break
}
}
println!("Du hast gewonnen ");
store();
}
fn input() -> i8 {
let mut guess = String::new();
@ -46,17 +78,17 @@ fn vergleich(guess: i8, secretnumber: i8) -> bool {
false
}
}
fn store() {
println!("Where do you want to store your score? Server: s/ lokal: l");
let mut storage = String::new();
io::stdin()
.read_line(&mut storage)
.expect("fatal error accorded");
match storage.trim() {
"s" => println!("sorry but this is not avalibil"),
fn idk() -> bool {
println!("Wilst du das Spiel nocheinmal spielen (y/n)");
let mut idks = String::new();
io::stdin().read_line(&mut idks).expect("A Error eccurt");
match idks {
y => return true,
n => return false,
_ => panic!(),
"l" => println!("sorry but this is not avalibil"),
_ => invalid_input(2),
}
}
fn quit() {
panic!()
menu()
}