Parsing board from ascii

Mostly useful for testing!
main
Elijah C. Voigt 2 years ago
parent df1a2a3bf7
commit b86aea043e

@ -816,7 +816,7 @@ fn set_valid_move_model(
fn pick_up( fn pick_up(
mut events: Query< mut events: Query<
(Entity, &game::Piece, &game::Side), (Entity, &game::Piece),
(With<game::Piece>, With<Display3d>, Added<game::Selected>), (With<game::Piece>, With<Display3d>, Added<game::Selected>),
>, >,
gltfs: Res<Assets<Gltf>>, gltfs: Res<Assets<Gltf>>,
@ -829,7 +829,7 @@ fn pick_up(
let tweak = tweaks let tweak = tweaks
.get(tweaks_file.handle.clone()) .get(tweaks_file.handle.clone())
.expect("Load tweakfile"); .expect("Load tweakfile");
events.iter_mut().for_each(|(entity, piece, side)| { events.iter_mut().for_each(|(entity, piece)| {
info!("Picking up piece"); info!("Picking up piece");
let assets_handle = tweak let assets_handle = tweak
@ -891,7 +891,7 @@ fn pick_up(
fn put_down( fn put_down(
mut events: RemovedComponents<game::Selected>, mut events: RemovedComponents<game::Selected>,
mut query: Query< mut query: Query<
(&game::Piece, &game::Side), Entity,
(With<game::Piece>, With<game::Selectable>, With<Display3d>), (With<game::Piece>, With<game::Selectable>, With<Display3d>),
>, >,
gltfs: Res<Assets<Gltf>>, gltfs: Res<Assets<Gltf>>,
@ -905,7 +905,7 @@ fn put_down(
.get(&tweaks_file.handle.clone()) .get(&tweaks_file.handle.clone())
.expect("Load tweakfile"); .expect("Load tweakfile");
events.read().for_each(|entity| { events.read().for_each(|entity| {
if let Ok((piece, side)) = query.get_mut(entity) { if let Ok(_) = query.get_mut(entity) {
info!("Putting down piece"); info!("Putting down piece");
let assets_handle = tweak let assets_handle = tweak

@ -301,15 +301,6 @@ pub(crate) enum Side {
B, B,
} }
impl Side {
pub(crate) fn color_str(&self) -> &'static str {
match self {
Side::A => "red",
Side::B => "blue",
}
}
}
impl std::ops::Not for Side { impl std::ops::Not for Side {
type Output = Self; type Output = Self;
@ -327,55 +318,47 @@ impl Board {
self.inner[y][x].as_ref() self.inner[y][x].as_ref()
} }
fn new() -> Board { fn from_ascii(art: &str) -> Board {
use Piece::*; use Piece::*;
let mut inner: Vec<Vec<Option<Piece>>> = vec![Vec::with_capacity(8), Vec::with_capacity(8), Vec::with_capacity(8), Vec::with_capacity(8)];
let mut index = BoardIndex { x: 0, y: 3 };
art.chars().for_each(|c| {
match c {
'q' => {
inner[index.y].push(Some(Queen));
}
'd' => {
inner[index.y].push(Some(Drone));
},
'p' => {
inner[index.y].push(Some(Pawn));
},
'.' => {
inner[index.y].push(None);
},
'\n' => {
assert_eq!(inner[index.y].len(), 8, "Each row must be 8 characters long!");
index.y -= 1;
}
_ => {
// Ignore all other characters
}
}
});
Board { Board {
inner,
moves: vec![], moves: vec![],
inner: vec![
vec![
Some(Queen),
Some(Queen),
Some(Drone),
None,
None,
None,
None,
None,
],
vec![
Some(Queen),
Some(Drone),
Some(Pawn),
None,
None,
Some(Pawn),
Some(Pawn),
Some(Drone),
],
vec![
Some(Drone),
Some(Pawn),
Some(Pawn),
None,
None,
Some(Pawn),
Some(Drone),
Some(Queen),
],
vec![
None,
None,
None,
None,
None,
Some(Drone),
Some(Queen),
Some(Queen),
],
],
} }
} }
fn new() -> Board {
Board::from_ascii(
r#".....dqq
dpp..pdq
qdp..ppd
qqd....."#)
}
/// Show all pieces on one side of the board /// Show all pieces on one side of the board
pub(crate) fn on(&self, side: Side) -> Vec<(&Piece, BoardIndex)> { pub(crate) fn on(&self, side: Side) -> Vec<(&Piece, BoardIndex)> {
match side { match side {
@ -600,7 +583,6 @@ impl Board {
} }
/// Returns the possible moves the piece at this tile can make. /// Returns the possible moves the piece at this tile can make.
/// TODO: Implement "no jumping" over pieces
pub(crate) fn valid_moves(&self, current_board_index: BoardIndex) -> HashSet<BoardIndex> { pub(crate) fn valid_moves(&self, current_board_index: BoardIndex) -> HashSet<BoardIndex> {
tiles() tiles()
.filter_map(|(board_index, _)| { .filter_map(|(board_index, _)| {
@ -620,6 +602,21 @@ impl Board {
} }
} }
mod test {
use super::*;
#[test]
fn test_01() {
let mut board = Board::new();
print!("{}", board);
println!("{:?}", board.at(BoardIndex { x: 0, y: 0 }));
println!("{:?}", board.at(BoardIndex { x: 1, y: 0 }));
println!("{:?}", board.at(BoardIndex { x: 2, y: 0 }));
println!("{:?}", board.at(BoardIndex { x: 3, y: 0 }));
todo!()
}
}
impl std::fmt::Display for Board { impl std::fmt::Display for Board {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
self.inner.iter().rev().for_each(|row| { self.inner.iter().rev().for_each(|row| {

Loading…
Cancel
Save