From 439a67adc631c24bfacb280e860aa67737f0c455 Mon Sep 17 00:00:00 2001 From: h7x4 Date: Wed, 20 Oct 2021 12:22:11 +0200 Subject: [PATCH] Make several changes --- src/board.rs | 24 --------------- src/main.rs | 45 ++++++++++++++-------------- src/models.rs | 0 src/models/board.rs | 59 +++++++++++++++++++++++++++++++++++-- src/models/mod.rs | 8 +++++ src/models/piece.rs | 70 ++++++++++++++++++++++++++++++++++++-------- src/models/player.rs | 15 ++++++++++ 7 files changed, 159 insertions(+), 62 deletions(-) delete mode 100644 src/board.rs delete mode 100644 src/models.rs create mode 100644 src/models/mod.rs create mode 100644 src/models/player.rs diff --git a/src/board.rs b/src/board.rs deleted file mode 100644 index c236eed..0000000 --- a/src/board.rs +++ /dev/null @@ -1,24 +0,0 @@ -// bored -use crate::models::piece::Piece; - - -fn print_board(board: Board) { - println!("{}", board.iter().map(|row| row.iter().map(|p| p.to_string())..join(" ")).join("\n")); -} - -fn main() { - // let board = vec![ - // vec![Piece::Pharaoh], - // vec![], - // vec![], - // vec![], - // ]; - - // print_board(board); -} - - -fn is_legal_move() -> bool { - return true; -} - diff --git a/src/main.rs b/src/main.rs index 91daa5e..4b5bb1f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,9 +1,10 @@ use std::env; -mod models { - pub mod board; - pub mod piece; - pub mod networking; -} + +// mod models { +// pub mod board; +// pub mod piece; +// pub mod networking; +// } fn print_help() { println!("Ey fam, hmu with dat '--serve' or '--join
'") @@ -38,26 +39,26 @@ fn main() { } - println!("{}", serialize_move(models::networking::Action::Rotate { - from: (2, 2), - rot: models::networking::RotationDirection::Positive - })); + // println!("{}", &serialize_move(models::networking::Action::Rotate { + // from: (2, 2), + // rot: models::networking::RotationDirection::Positive + // })); } -fn serialize_move(action: models::networking::Action) -> str { - let serialized = serde_json::to_string(&action); - match serialized { - Ok(s) => { - println!("OK: {}", s); - return &s; - }, - Err(_e) => { - println!("Couldn't serialize..."); - return (""); - } - } - } \ No newline at end of file +// fn serialize_move(action: models::networking::Action) -> str { +// let serialized = serde_json::to_string(&action); +// match serialized { +// Ok(s) => { +// println!("OK: {}", s); +// return &s; +// }, +// Err(_e) => { +// println!("Couldn't serialize..."); +// return (""); +// } +// } +// } \ No newline at end of file diff --git a/src/models.rs b/src/models.rs deleted file mode 100644 index e69de29..0000000 diff --git a/src/models/board.rs b/src/models/board.rs index ddf029b..07997e4 100644 --- a/src/models/board.rs +++ b/src/models/board.rs @@ -1,5 +1,58 @@ +use std::fmt; -// use crate::models::piece::Piece; +use super::piece::Piece; -// #[derive(Display)] -pub type Board = Vec>; \ No newline at end of file +pub struct Board(Vec>>); + +impl fmt::Display for Board { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + let str = self + .0 + .to_vec() + .into_iter() + .map(|row| { + row + .into_iter() + .map(|op| match op { + Some(p) => p.to_string(), + None => ".".to_string(), + }) + .collect::>() + .join(" ") + .to_string() + }) + .collect::>() + .join("\n"); + + return write!(f, "{}", str); + } +} + +impl Board { + pub fn from_string(s: &str) -> Board { + let board = s + .split("\n") + .map(|row| { + row + .chars() + .map(|l| Piece::from_string(&l).ok()) + .collect::>() + }) + .collect::>(); + Board(board) + } + + pub fn default() -> Board { + let board = vec![ + "....OpO2..", + "..3.......", + "...4......", + "1.3.\\/.2.4", + "2.4./\\.1.3", + "......2...", + ".......1..", + "..4OpO....", + ]; + Board::from_string(&board.join("\n")) + } +} \ No newline at end of file diff --git a/src/models/mod.rs b/src/models/mod.rs new file mode 100644 index 0000000..df2f8b7 --- /dev/null +++ b/src/models/mod.rs @@ -0,0 +1,8 @@ +pub mod board; +// pub mod networking; +pub mod piece; +pub mod player; + +fn main() { + println!("{}", board::Board::default()) +} \ No newline at end of file diff --git a/src/models/piece.rs b/src/models/piece.rs index ebf52e5..9a76fd1 100644 --- a/src/models/piece.rs +++ b/src/models/piece.rs @@ -1,37 +1,81 @@ use std::fmt; -enum DjedDirection { +use super::player::Player; + +#[derive(Clone)] +pub enum DjedDirection { NorthWest, NorthEast, } -enum PyramidDirection { - NorthWest, +#[derive(Clone)] +pub enum PyramidDirection { NorthEast, SouthEast, SouthWest, + NorthWest, } -enum Stack { +#[derive(Clone)] +pub enum ObeliskStack { Single, Double, } -// #[derive(Display)] -pub enum Piece { +#[derive(Clone)] +pub enum PieceType { Pharaoh, Djed(DjedDirection), Pyramid(PyramidDirection), - Obelisk(Stack) + Obelisk(ObeliskStack), +} + +#[derive(Clone)] +pub struct Piece { + player: Player, + typ: PieceType, } impl fmt::Display for Piece { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - match self { - Piece::Pharaoh => write!(f, "Pharaoh"), - Piece::Djed(_) => write!(f, "Djed"), - Piece::Pyramid(_) => write!(f, "Pyramid"), - Piece::Obelisk(_) => write!(f, "Obelisk"), + match &self.typ { + PieceType::Pharaoh => write!(f, "P"), + PieceType::Djed(d) => match d { + DjedDirection::NorthWest => write!(f, "⟋"), + DjedDirection::NorthEast => write!(f, "⟍"), + }, + PieceType::Pyramid(d) => match d { + PyramidDirection::NorthEast => write!(f, "\u{25E3}"), + PyramidDirection::SouthEast => write!(f, "\u{25E4}"), + PyramidDirection::SouthWest => write!(f, "\u{25E5}"), + PyramidDirection::NorthWest => write!(f, "\u{25E2}"), + }, + PieceType::Obelisk(s) => match s { + ObeliskStack::Single => write!(f, "\u{25C7}"), + ObeliskStack::Double => write!(f, "\u{25C8}"), + }, } } -} \ No newline at end of file +} + +impl Piece { + pub fn from_string(s: &char) -> Result { + let typ = match s { + 'p' => Some(PieceType::Pharaoh), + '/' => Some(PieceType::Djed(DjedDirection::NorthWest)), + '\\' => Some(PieceType::Djed(DjedDirection::NorthEast)), + '1' => Some(PieceType::Pyramid(PyramidDirection::NorthEast)), + '2' => Some(PieceType::Pyramid(PyramidDirection::SouthEast)), + '3' => Some(PieceType::Pyramid(PyramidDirection::SouthWest)), + '4' => Some(PieceType::Pyramid(PyramidDirection::NorthWest)), + 'o' => Some(PieceType::Obelisk(ObeliskStack::Single)), + 'O' => Some(PieceType::Obelisk(ObeliskStack::Double)), + _ => None, + }; + + match typ { + Some(p) => Ok(Piece {typ: p, player: Player::player1("test-user")}), + None => Err("No such piece"), + } + } +} diff --git a/src/models/player.rs b/src/models/player.rs new file mode 100644 index 0000000..267d056 --- /dev/null +++ b/src/models/player.rs @@ -0,0 +1,15 @@ +#[derive(Clone)] +pub struct Player { + name: String, + // TODO: Make color its own type + color: String, +} + +impl Player { + pub fn player1(name: &str) -> Player { + Player { + name: name.to_string(), + color: "".to_string(), + } + } +} \ No newline at end of file