src/main.rs aktualisiert

This commit is contained in:
robin 2024-10-28 14:59:51 +01:00
parent c0856a6f8d
commit 2c96e3c332

View file

@ -1,47 +1,71 @@
use bevy::prelude::{warn, error};
use bevy::prelude::{Component};
use bevy::app::{App, Startup};
use rand::Rng; use rand::Rng;
use std::fs::{read, File}; use std::io;
use std::process::exit; use std::process::exit;
use std::{fs, io}; use bevy::DefaultPlugins;
use bevy::log::info;
use bevy::utils::{error, warn};
#[derive(Component)]
pub enum GameState {
Startup,
Menu,
InGame,
}
fn main() { fn main() {
start(); App::new().add_plugins(DefaultPlugins).add_systems(Startup, start).run();
} }
fn invalid_input(function: i8) { fn invalid_input(function: i8) {
println!("Invalid Input"); println!("Invalid Input");
warn!("Invalid Input");
match function { match function {
1 => menu(), 1 => menu(),
2 => store(), 2 => store(),
3=> { 3 => {
println!("Please input a valid name"); println!("Please input a valid name");
start() start()
} }
_ => panic!("A fatal Error occurred"), 4 => {
println!("Please input a valid door");
game_loop(0)
}
_ => {
error!("Invalid Input got called without a trace, this will result in a crash.");
panic!("A fatal Error occurred")
}
} }
} }
fn start() { fn start() {
info!("Game starts");
println!("Hello what is your Name?"); println!("Hello what is your Name?");
let mut name = String::new(); let mut name = String::new();
io::stdin().read_line(&mut name).expect("error"); io::stdin().read_line(&mut name).expect("error");
if name.trim.is_empty() == true{ if name.is_empty() == true {
invalid_input(3) invalid_input(3)
}else { } else {
println!("Hello {}", name); println!("Hello {}", name);
menu();} menu();
}
} }
fn menu() { fn menu() {
info!("Menu");
println!("Start the Game:s Quit:q let somebody else play:e "); println!("Start the Game:s Quit:q let somebody else play:e ");
let mut doing = String::new(); let mut doing = String::new();
io::stdin().read_line(&mut doing).expect("An fatal error"); io::stdin().read_line(&mut doing).expect("An fatal error");
match doing.trim() { match doing.trim() {
"q" => exit(1), "q" => exit(0),
"e" => start(), "e" => start(),
"s" => game_loop(), "s" => game_loop(1),
_ => invalid_input(1), _ => invalid_input(1),
} }
} }
fn game_loop() { fn game_loop(from:i8) {
info!("Ingame");
let mut score: i8 = 0; let mut score: i8 = 0;
loop { loop {
println!("Your score is: {}", score); 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."); 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 secretnumber: i8 = rand::thread_rng().gen_range(1..=3);
@ -53,12 +77,12 @@ fn game_loop() {
secretnumber secretnumber
); );
score = score + 1; score = score + 1;
}else if score == 32{ } else if score == 32 {
println!("You won"); println!("You won");
break
} else { } else {
println!("You are death"); println!("You are death");
break break;
} }
} }
store(); store();
@ -68,9 +92,10 @@ fn input() -> i8 {
io::stdin() io::stdin()
.read_line(&mut guess) .read_line(&mut guess)
.expect("Failed to read line"); .expect("Failed to read line");
let guess: i8 = guess.trim().parse().expect("Please type a number!"); let guess: i8 = guess.trim().parse().expect("Please type a number!");
return guess; return guess;}
}
fn vergleich(guess: i8, secretnumber: i8) -> bool { fn vergleich(guess: i8, secretnumber: i8) -> bool {
if guess == secretnumber { if guess == secretnumber {
true true