Skip to content

Commit

Permalink
Refactor iterators (#13)
Browse files Browse the repository at this point in the history
* Do not serialize private game data

* Put movement logic in chusst-gen::game::play

* Allow to backup GameState mobility data

* Allow chaining iterators more ergonomically

* Fix iterators

* Fix take_while_empty_or_enemy()

* Fix take_while_empty_until_enemy()
  • Loading branch information
oriolarcas authored Jan 19, 2024
1 parent 64dda02 commit 40086a2
Show file tree
Hide file tree
Showing 18 changed files with 1,239 additions and 704 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions chusst-gen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ edition = "2021"
anyhow = "1.0.79"
atty = "0.2.14"
colored = "2.1.0"
lazy_static = "1.4.0"
rand = "0.8.5"
serde = { version = "1.0.195", features = ["derive"] }

[dev-dependencies]
Expand Down
37 changes: 36 additions & 1 deletion chusst-gen/src/board.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
mod iter;

// Board representations
#[cfg(feature = "bitboards")]
mod bitboards;
Expand All @@ -13,6 +15,10 @@ pub(crate) use bitboards::PlayerBitboards;
pub use compact::CompactBoard;
pub use simple::SimpleBoard;

pub use self::iter::{BoardIter, Direction, PositionIter, PositionIterator};

use self::iter::{try_move, DirectionIter};

use atty;
use colored::Colorize;
use serde::{ser::SerializeMap, ser::SerializeSeq, Serialize};
Expand Down Expand Up @@ -408,14 +414,43 @@ macro_rules! pos {
pub type Files<T> = [T; 8];
pub type Ranks<T> = [Files<T>; 8];

pub trait ModifiableBoard<K, V> {
pub trait ModifiableBoard<K, V>
where
Self: Sized,
{
fn at(&self, pos: &K) -> V;
fn update(&mut self, pos: &K, value: V);
fn move_piece(&mut self, source: &K, target: &K);
}

pub trait IterableBoard
where
Self: Sized,
{
fn try_move(&self, position: &Position, direction: &Direction) -> PositionIter<Self> {
PositionIter::new(self, try_move(position, direction))
}

fn position_iter(&self, position: &Position) -> PositionIter<Self> {
PositionIter::new(self, Some(*position))
}

fn board_iter(&self) -> BoardIter<Self> {
BoardIter::new(self)
}

fn direction_iterator(
&self,
position: &Position,
direction: &Direction,
) -> DirectionIter<Self> {
DirectionIter::new(self, Some(*position), *direction)
}
}

pub trait Board:
ModifiableBoard<Position, Option<Piece>>
+ IterableBoard
+ Clone
+ Default
+ fmt::Debug
Expand Down
5 changes: 4 additions & 1 deletion chusst-gen/src/board/bitboards.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ mod in_between;
use serde::Serialize;

use super::{
format_board, serialize_board, Board, ModifiableBoard, Piece, PieceType, Player, Position,
format_board, serialize_board, Board, IterableBoard, ModifiableBoard, Piece, PieceType, Player,
Position,
};
use std::fmt;

Expand Down Expand Up @@ -378,6 +379,8 @@ impl ModifiableBoard<Position, Option<Piece>> for Bitboards {
}
}

impl IterableBoard for Bitboards {}

impl Board for Bitboards {
const NEW_BOARD: Self = Bitboards::new();
}
Expand Down
6 changes: 4 additions & 2 deletions chusst-gen/src/board/compact.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ use std::fmt;
use serde::Serialize;

use super::{
format_board, serialize_board, Board, ModifiableBoard, Piece, PieceType, Player, Position,
Ranks,
format_board, serialize_board, Board, IterableBoard, ModifiableBoard, Piece, PieceType, Player,
Position, Ranks,
};
use crate::p;

Expand Down Expand Up @@ -128,6 +128,8 @@ impl ModifiableBoard<Position, Option<Piece>> for CompactBoard {
}
}

impl IterableBoard for CompactBoard {}

impl Board for CompactBoard {
const NEW_BOARD: Self = CompactBoard::new();
}
Expand Down
Loading

0 comments on commit 40086a2

Please sign in to comment.