trictrac/client_cli/src/app.rs

78 lines
2.6 KiB
Rust
Raw Normal View History

2024-02-18 18:40:45 +01:00
use pretty_assertions::assert_eq;
2024-02-17 12:55:36 +01:00
use store::GameState;
2024-02-09 17:47:34 +01:00
// Application.
#[derive(Debug, Default)]
pub struct App {
// should the application exit?
pub should_quit: bool,
2024-02-17 12:55:36 +01:00
pub game: GameState,
2024-02-09 17:47:34 +01:00
}
impl App {
// Constructs a new instance of [`App`].
pub fn new() -> Self {
Self::default()
}
2024-02-17 12:55:36 +01:00
// Constructs a new instance of [`App`].
pub fn start(&mut self) {
self.game = GameState::new();
}
2024-02-09 17:47:34 +01:00
pub fn input(&mut self, input: &str) {
println!("'{}'", input);
2024-02-17 12:55:36 +01:00
println!("'{}'", self.display());
2024-02-09 17:47:34 +01:00
if input == "quit" {
self.quit();
}
}
// Set running to false to quit the application.
pub fn quit(&mut self) {
self.should_quit = true;
}
2024-02-17 12:55:36 +01:00
pub fn display(&mut self) -> String {
2024-02-18 18:40:45 +01:00
let mut board = "".to_owned();
board = board + &self.game.board.to_display_grid(9);
2024-02-17 12:55:36 +01:00
board
2024-02-09 17:47:34 +01:00
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
2024-02-17 12:55:36 +01:00
fn test_display() {
let expected = "
2024-02-18 18:40:45 +01:00
13 14 15 16 17 18 19 20 21 22 23 24
----------------------------------------------------------------
| | | X |
| | | X |
| | | X |
| | | X |
| | | X |
| | | X |
| | | X |
| | | X |
| | | 15 |
|----------------------------- | | ------------------------------|
| | | 15 |
| | | O |
| | | O |
| | | O |
| | | O |
| | | O |
| | | O |
| | | O |
| | | O |
----------------------------------------------------------------
12 11 10 9 8 7 6 5 4 3 2 1
";
2024-02-09 17:47:34 +01:00
let mut app = App::default();
2024-02-18 18:40:45 +01:00
self::assert_eq!(app.display(), expected);
2024-02-09 17:47:34 +01:00
}
}