Make several changes

This commit is contained in:
Oystein Kristoffer Tveit 2021-10-20 12:22:11 +02:00
parent 655b7eacd1
commit 439a67adc6
7 changed files with 159 additions and 62 deletions

View File

@ -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;
}

View File

@ -1,9 +1,10 @@
use std::env; use std::env;
mod models {
pub mod board; // mod models {
pub mod piece; // pub mod board;
pub mod networking; // pub mod piece;
} // pub mod networking;
// }
fn print_help() { fn print_help() {
println!("Ey fam, hmu with dat '--serve' or '--join <address>'") println!("Ey fam, hmu with dat '--serve' or '--join <address>'")
@ -38,26 +39,26 @@ fn main() {
} }
println!("{}", serialize_move(models::networking::Action::Rotate { // println!("{}", &serialize_move(models::networking::Action::Rotate {
from: (2, 2), // from: (2, 2),
rot: models::networking::RotationDirection::Positive // rot: models::networking::RotationDirection::Positive
})); // }));
} }
fn serialize_move(action: models::networking::Action) -> str { // fn serialize_move(action: models::networking::Action) -> str {
let serialized = serde_json::to_string(&action); // let serialized = serde_json::to_string(&action);
match serialized { // match serialized {
Ok(s) => { // Ok(s) => {
println!("OK: {}", s); // println!("OK: {}", s);
return &s; // return &s;
}, // },
Err(_e) => { // Err(_e) => {
println!("Couldn't serialize..."); // println!("Couldn't serialize...");
return (""); // return ("");
} // }
} // }
} // }

View File

View File

@ -1,5 +1,58 @@
use std::fmt;
// use crate::models::piece::Piece; use super::piece::Piece;
// #[derive(Display)] pub struct Board(Vec<Vec<Option<Piece>>>);
pub type Board = Vec<Vec<crate::models::piece::Piece>>;
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::<Vec<_>>()
.join(" ")
.to_string()
})
.collect::<Vec<_>>()
.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::<Vec<_>>()
})
.collect::<Vec<_>>();
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"))
}
}

8
src/models/mod.rs Normal file
View File

@ -0,0 +1,8 @@
pub mod board;
// pub mod networking;
pub mod piece;
pub mod player;
fn main() {
println!("{}", board::Board::default())
}

View File

@ -1,37 +1,81 @@
use std::fmt; use std::fmt;
enum DjedDirection { use super::player::Player;
#[derive(Clone)]
pub enum DjedDirection {
NorthWest, NorthWest,
NorthEast, NorthEast,
} }
enum PyramidDirection { #[derive(Clone)]
NorthWest, pub enum PyramidDirection {
NorthEast, NorthEast,
SouthEast, SouthEast,
SouthWest, SouthWest,
NorthWest,
} }
enum Stack { #[derive(Clone)]
pub enum ObeliskStack {
Single, Single,
Double, Double,
} }
// #[derive(Display)] #[derive(Clone)]
pub enum Piece { pub enum PieceType {
Pharaoh, Pharaoh,
Djed(DjedDirection), Djed(DjedDirection),
Pyramid(PyramidDirection), Pyramid(PyramidDirection),
Obelisk(Stack) Obelisk(ObeliskStack),
}
#[derive(Clone)]
pub struct Piece {
player: Player,
typ: PieceType,
} }
impl fmt::Display for Piece { impl fmt::Display for Piece {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self { match &self.typ {
Piece::Pharaoh => write!(f, "Pharaoh"), PieceType::Pharaoh => write!(f, "P"),
Piece::Djed(_) => write!(f, "Djed"), PieceType::Djed(d) => match d {
Piece::Pyramid(_) => write!(f, "Pyramid"), DjedDirection::NorthWest => write!(f, ""),
Piece::Obelisk(_) => write!(f, "Obelisk"), 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}"),
},
}
}
}
impl Piece {
pub fn from_string(s: &char) -> Result<Piece, &str> {
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"),
} }
} }
} }

15
src/models/player.rs Normal file
View File

@ -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(),
}
}
}