feat: add displaying board_view as string

This commit is contained in:
2026-08-17 17:57:28 +02:00
parent 10a874da32
commit ed6fc3583a
3 changed files with 45 additions and 1 deletions

View File

@@ -85,6 +85,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
"Got new identity: {} and board_view. Game is about to start!",
identity
);
println!("{}", board_view.to_string(identity.id));
}
Ok(checkers::network::client::ClientData::AvailableCaptures(capture_paths)) => {

View File

@@ -1,4 +1,7 @@
use std::ops::Index;
use serde::{Deserialize, Serialize};
use uuid::Uuid;
use crate::logic::board::{board::Board, field::Field, pawn::Pawn};
@@ -17,3 +20,44 @@ impl From<Board> for BoardView {
}
}
}
impl Index<usize> for BoardView {
type Output = [Field];
fn index<'a>(&'a self, i: usize) -> &'a [Field] {
let start = i * &self.size;
let end: usize = start + &self.size;
&self.board[start..end]
}
}
impl BoardView {
pub fn to_string(&self, owner: Uuid) -> String {
(0..self.size)
.map(move |row| {
(0..self.size)
.map(move |column| match &self[row][column].pawn {
Some(pawn) => match pawn.state {
super::pawn::PawnState::Man => {
if pawn.owner == owner {
'm'
} else {
'M'
}
}
super::pawn::PawnState::Dame => {
if pawn.owner == owner {
'd'
} else {
'D'
}
}
},
None => ' ',
})
.collect::<String>()
})
.collect::<Vec<String>>()
.join("\n")
}
}

View File

@@ -7,7 +7,6 @@ use crate::logic::{math::position::Position, player::Player};
pub enum PawnState {
Man,
Dame,
Captured,
}
/// Basic pawn