Create a separate Ui struct

This commit is contained in:
2021-12-12 11:49:06 +01:00
parent 1f5e58d981
commit 6d433a6d01
2 changed files with 29 additions and 14 deletions

View File

@@ -1,19 +1,6 @@
use std::io::BufRead;
mod board;
mod ui;
fn main() -> Result<(), ()> {
let mut board = board::Board::new();
board.reset();
let stdin = std::io::stdin();
let mut input_lines = stdin.lock().lines();
loop {
match input_lines.next() {
None => break Ok(()),
Some(line) => match &line.unwrap()[..] {
"get_state" => println!("{}", board),
_ => break Err(()),
},
}
}
ui::Ui::new().run()
}

View File

@@ -1,5 +1,6 @@
use crate::board;
use crate::board::GridAxis;
use std::io::BufRead;
impl std::fmt::Display for board::Color {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
@@ -52,3 +53,30 @@ impl std::fmt::Display for board::Board {
Ok(())
}
}
pub struct Ui {
board: board::Board,
}
impl Ui {
pub fn new() -> Self {
let mut board = board::Board::new();
board.reset();
Ui {
board,
}
}
pub fn run(&mut self) -> Result<(), ()> {
let stdin = std::io::stdin();
let mut input_lines = stdin.lock().lines();
loop {
match input_lines.next() {
None => break Ok(()),
Some(line) => match &line.unwrap()[..] {
"get_state" => println!("{}", self.board),
_ => break Err(()),
},
}
}
}
}