Compare commits
7 commits
main
...
guessing-g
Author | SHA1 | Date | |
---|---|---|---|
|
c0856a6f8d | ||
|
147add5654 | ||
|
7b47cdc1a6 | ||
|
ae920f8598 | ||
|
e66e16cc91 | ||
|
fe150166a4 | ||
|
6bdff369e0 |
|
@ -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
|
110
src/main.rs
110
src/main.rs
|
@ -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()
|
||||||
}
|
}
|
Loading…
Reference in a new issue