mirror of
https://github.com/szymon-jozef/checkers.git
synced 2026-10-27 19:20:44 +01:00
feat: add displaying board_view as string
This commit is contained in:
@@ -85,6 +85,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
"Got new identity: {} and board_view. Game is about to start!",
|
"Got new identity: {} and board_view. Game is about to start!",
|
||||||
identity
|
identity
|
||||||
);
|
);
|
||||||
|
println!("{}", board_view.to_string(identity.id));
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(checkers::network::client::ClientData::AvailableCaptures(capture_paths)) => {
|
Ok(checkers::network::client::ClientData::AvailableCaptures(capture_paths)) => {
|
||||||
|
|||||||
@@ -1,4 +1,7 @@
|
|||||||
|
use std::ops::Index;
|
||||||
|
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
use crate::logic::board::{board::Board, field::Field, pawn::Pawn};
|
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")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -7,7 +7,6 @@ use crate::logic::{math::position::Position, player::Player};
|
|||||||
pub enum PawnState {
|
pub enum PawnState {
|
||||||
Man,
|
Man,
|
||||||
Dame,
|
Dame,
|
||||||
Captured,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Basic pawn
|
/// Basic pawn
|
||||||
|
|||||||
Reference in New Issue
Block a user