Compare commits

...

7 commits

Author SHA1 Message Date
robin c0856a6f8d README.md aktualisiert 2024-08-17 20:27:25 +02:00
robin 147add5654 src/main.rs aktualisiert 2024-08-08 15:33:20 +02:00
robin 7b47cdc1a6 src/main.rs aktualisiert 2024-08-06 15:12:14 +02:00
robin ae920f8598 src/main.rs aktualisiert 2024-07-01 11:31:54 +02:00
robin e66e16cc91 src/main.rs aktualisiert
I added another funktion but I don´t know how I should implement enums
2024-06-12 21:02:14 +02:00
robin fe150166a4 src/main.rs aktualisiert
Fix the issue #6 partly it still accepts letters and symbols
2024-06-11 22:14:25 +02:00
robin 6bdff369e0 src/main.rs aktualisiert 2024-06-11 22:05:24 +02:00
2 changed files with 92 additions and 32 deletions

View file

@ -1,3 +1,3 @@
# Rust_lernen # Rust_lernen
Ich versuche rust zu lernen ich hoffe das ich mehr google als Jakob frage Ich versuche rust zu lernen ich hoffe das ich mehr google als viridian frage
gessing game finished at 04.04.2024 on 21:25 Cest gessing game finished at 04.04.2024 on 21:25 Cest

View file

@ -1,34 +1,94 @@
use std::io;
use rand::Rng; use rand::Rng;
fn main(){ use std::fs::{read, File};
let mut score:i16 = 0; use std::process::exit;
while score < 32 { use std::{fs, io};
println!("Vor dir sind drei Türen, eine von ihnen tötet dich");
println!("Durch welche gehst du? Gib eine Zahl zwischen 1 und 3 ein");
let secretnumber = rand::thread_rng().gen_range(1..=3);
let mut guess = String::new();
fn main() {
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.trim.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;
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!(
"You survived, the ghost was behind the {} door",
secretnumber
);
score = score + 1;
}else if score == 32{
println!("You won");
break
} else {
println!("You are death");
break
}
}
store();
}
fn input() -> i8 {
let mut guess = String::new();
io::stdin() io::stdin()
.read_line(&mut guess) .read_line(&mut guess)
.expect("Failed to read line"); .expect("Failed to read line");
if guess.chars().any(|c| c.is_digit(10)){ let guess: i8 = guess.trim().parse().expect("Please type a number!");
return guess;
let guess: u32 = guess.trim().parse().expect("Please type a number!"); }
fn vergleich(guess: i8, secretnumber: i8) -> bool {
if guess == secretnumber { if guess == secretnumber {
println!("Dein score: {}", score); true
println!("Du bist tot"); } else {
std::process::exit(0); false
} }
if guess > 4 {
println!("die option ist unbekannt");
} }
else { fn store() {
println!("Du hast überlebt"); println!("Where do you want to store your score? Server: s/ lokal: l");
score = score += 1; let mut storage = String::new();
println!("Der Geist ist hinter der Tür {}", secretnumber); io::stdin()
}} .read_line(&mut storage)
else { .expect("fatal error accorded");
println!("Die Option ist unbekannt") match storage.trim() {
}} "s" => println!("sorry but this is not avalibil"),
println!("Du hast gewonnen");
"l" => println!("sorry but this is not avalibil"),
_ => invalid_input(2),
}
menu()
} }