README updated

- fixed help message game
This commit is contained in:
2024-06-06 11:12:52 +02:00
parent f966a8047c
commit 3d10db930a
3 changed files with 67 additions and 11 deletions

View File

@@ -1,2 +1,35 @@
# upo-rust
Repo for Rust projects and exercises
### Repo per gli esercizi di Rust
Qui si possono trovare tutti gil esercizi assegnati.\
In particolare ogni esercizio ha una parte di codice per la soluzione e una parte di test.\
Per far partire tutti i test bisogna usare il comando:
```console
$ cargo test --workspace
```
Questo perchè alcuni test sono in packages interni e quindi non verrebbero fatti partire nel caso in cui si faccia solamente cargo test.\
Nel caso si voglia far partire l'unico possibile eseguibile (ovvero il gioco dell'esercizio 3) utilizzare il comando:
```console
$ cargo run -p rogue_lib
```
### Esercizi
- [Esercizio 1: Anagrammi](https://github.com/Berack96/upo-rust/blob/main/src/es01_anagram.rs)\
In questo esercizio bisogna controllare se due stringhe sono l'una l'anagramma dell'altra.
- [Esercizio 2 Razionali](https://github.com/Berack96/upo-rust/blob/main/src/es02_rational.rs)\
In questo esercizio bisogna creare un'implementazione dei numeri razionali.
- [Esercizio 3: Rogue](https://github.com/Berack96/upo-rust/blob/main/rogue_lib/src/lib.rs)\
In questo esercizio è stato implementato un gioco ispirato a Rogue. Ho seguito il testo dell'esercizio ma ho anche implementato altre cose utilizzando anche dei Trait in modo da poter controllare i comportamenti delle Entità, l'interfaccia del giocatore ed eventuali effetti che si possono trovare in giro nel piano.\
Il gioco si trova in un package interno ed ha un main semplice che lo esegue.\
Il lib espone le varie interfacce e implementa una versione del gioco tramite console.
- [Esercizio 4: Razionali con Trait](https://github.com/Berack96/upo-rust/blob/main/src/es04_rational_traits.rs)\
In questo esercizio sono stati implementati i trait Add e Mul per i razionali (Esercizio 2)
- [Esercizio 5: Banca](https://github.com/Berack96/upo-rust/blob/main/src/es05_bank.rs)\
In questo esercizio è stata implementato un account di una banca con possibili stati in cui si può trovare
- [Esercizio 6: Lista (con Copy)](https://github.com/Berack96/upo-rust/blob/main/src/es06_list.rs)\
In questo esercizio è stata implementata una lista doppiamente linkata che accetta un elemento che implementa il trait Copy
- [Esercizio 7: Lista Generica](https://github.com/Berack96/upo-rust/blob/main/src/es07_list_generic.rs)\
In questo esercizio è stata implementata una lista doppiamente linkata che accetta un qualsiasi elemento
- [Esercizio 8: Folds](https://github.com/Berack96/upo-rust/blob/main/src/es08_folds.rs)\
In questo esercizio sono state implementate due funzioni che permettono la conta di quante vocali si trovano in una frase
- [Esercizio 9: Asta](https://github.com/Berack96/upo-rust/blob/main/src/es09_auction.rs)\
In questo esercizio è stata implementata un'asta in cui il banditore, data una lista di prodotti e dati dei partecipanti (ognuno avente il proprio thread), coordina la vendita dei prodotti tramite invio di messaggi.

View File

@@ -56,7 +56,6 @@ pub fn run_console(player: String, seed: u64) {
game.add_player(player, Box::new(ConsoleInput));
while game.has_players() {
let _ = game.save("save.json");
game.compute_turn();
}
}
@@ -174,7 +173,7 @@ impl Behavior for ConsoleInput {
self.print_floor(floor, "".to_string());
}
fn on_death(&mut self, floor: FloorView) {
self.print_floor(floor, "YOU DIED!".to_string());
self.print_floor(floor, format!("{}YOU DIED!{}\n", COLOR_ENEMY, COLOR_RESET));
}
fn get_next_action(&mut self, entity: &Entity) -> Option<Action> {
let prompt = "Insert your action [? for help]: ";
@@ -190,17 +189,17 @@ impl Behavior for ConsoleInput {
'a' => return Some(Action::Move(Direction::Left)),
's' => return Some(Action::Move(Direction::Down)),
'd' => return Some(Action::Move(Direction::Right)),
'q' => return None,
'q' => {
let _ = term.write_line("");
return None;
}
'?' => {
let _ = term.write_line("");
let _ = term.write_line("[wasd] => for movement");
let _ = term.write_line("[space] => for attacking the enemy in front");
let _ = term.write_line("[z] => for doing nothing");
let _ = term.write_line("[q] => for exit the game");
let _ = term.write("Press ANY button to continue...".as_bytes());
let (message, lines) = get_help_message();
let _ = term.write(message.as_bytes());
let _ = term.read_char(); // waiting for user acknowledgment
let _ = term.clear_line(); // clear line "press button..."
let _ = term.clear_last_lines(4); // this number is from the previous message (4 total lines of help)
let _ = term.clear_last_lines(lines); // this number is from the message
let _ = term.move_cursor_up(1); // moving up since the first write_line put me down by one
let _ = term.move_cursor_right(prompt.len()); // moving at the end of the prompt
}
@@ -210,3 +209,27 @@ impl Behavior for ConsoleInput {
}
}
}
fn get_help_message() -> (String, usize) {
let help_message = vec![
format!(
"{}Objective{}: survive and reach the next floor through {}",
COLOR_PLAYER_HEALTH,
COLOR_RESET,
Cell::Exit.as_char()
),
format!(
"Special effect cell are colored with {}{}",
COLOR_EFFECT, COLOR_RESET
),
"[wasd] => for movement".to_string(),
"[space] => for attacking the enemy in front".to_string(),
"[z] => for doing nothing".to_string(),
"[q] => for exit the game".to_string(),
"Press ANY button to continue...".to_string(),
];
let count = help_message.len() - 1; // the last one doesn't have the newline
let string = help_message.join("\n");
(string, count)
}

View File

@@ -1,4 +1,4 @@
fn main() {
let seed = rand::random();
rogue_lib::run_console("Jack".to_string(), seed);
rogue_lib::run_console("Player".to_string(), seed);
}