use cetkaik_fundamental::{Color, Profession};
use cetkaik_traits::{IsBoard, IsField, IsPieceWithSide};
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
pub enum Side {
Upward,
Downward,
}
impl std::ops::Not for Side {
type Output = Side;
fn not(self) -> Self::Output {
match self {
Side::Upward => Side::Downward,
Side::Downward => Side::Upward,
}
}
}
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
pub struct NonTam2PieceDownward {
pub color: Color,
pub prof: Profession,
}
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
pub struct NonTam2PieceUpward {
pub color: Color,
pub prof: Profession,
}
impl From<NonTam2PieceUpward> for Piece {
fn from(from: NonTam2PieceUpward) -> Piece {
Piece::NonTam2Piece {
color: from.color,
prof: from.prof,
side: Side::Upward,
}
}
}
impl From<NonTam2PieceDownward> for Piece {
fn from(from: NonTam2PieceDownward) -> Piece {
Piece::NonTam2Piece {
color: from.color,
prof: from.prof,
side: Side::Downward,
}
}
}
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
pub enum Piece {
Tam2,
NonTam2Piece {
color: Color,
prof: Profession,
side: Side,
},
}
impl Piece {
#[must_use]
pub const fn is_tam2(self) -> bool {
match self {
Piece::Tam2 => true,
Piece::NonTam2Piece { .. } => false,
}
}
#[must_use]
pub fn has_color(self, clr: Color) -> bool {
match self {
Piece::Tam2 => false,
Piece::NonTam2Piece { color, .. } => color == clr,
}
}
#[must_use]
pub fn has_prof(self, prf: Profession) -> bool {
match self {
Piece::Tam2 => false,
Piece::NonTam2Piece { prof, .. } => prof == prf,
}
}
#[must_use]
pub fn has_side(self, sid: Side) -> bool {
match self {
Piece::Tam2 => false,
Piece::NonTam2Piece { side, .. } => side == sid,
}
}
}
#[must_use]
fn rotate_piece_or_null(p: Option<Piece>) -> Option<Piece> {
let p = p?;
match p {
Piece::Tam2 => Some(p),
Piece::NonTam2Piece { prof, color, side } => Some(Piece::NonTam2Piece {
prof,
color,
side: !side,
}),
}
}
pub type Coord = [usize; 2];
#[must_use]
pub fn serialize_coord(coord: Coord) -> String {
format!("[{},{}]", coord[0], coord[1])
}
#[must_use]
pub const fn rotate_coord(c: Coord) -> Coord {
[(8 - c[0]), (8 - c[1])]
}
#[must_use]
#[allow(clippy::nonminimal_bool)]
pub const fn is_water([row, col]: Coord) -> bool {
(row == 4 && col == 2)
|| (row == 4 && col == 3)
|| (row == 4 && col == 4)
|| (row == 4 && col == 5)
|| (row == 4 && col == 6)
|| (row == 2 && col == 4)
|| (row == 3 && col == 4)
|| (row == 5 && col == 4)
|| (row == 6 && col == 4)
}
const fn serialize_side(side: Side) -> &'static str {
match side {
Side::Upward => "↑",
Side::Downward => "↓",
}
}
#[must_use]
pub fn serialize_piece(p: Piece) -> String {
match p {
Piece::Tam2 => "皇".to_string(),
Piece::NonTam2Piece { prof, color, side } => format!(
"{}{}{}",
cetkaik_fundamental::serialize_color(color),
cetkaik_fundamental::serialize_prof(prof),
serialize_side(side)
),
}
}
#[derive(Clone, Debug, Eq, PartialEq, Hash, Copy)]
pub struct Board(pub [SingleRow; 9]);
pub type SingleRow = [Option<Piece>; 9];
#[derive(Debug, Clone, Hash)]
pub struct Field {
pub current_board: Board,
pub hop1zuo1of_upward: Vec<NonTam2PieceUpward>,
pub hop1zuo1of_downward: Vec<NonTam2PieceDownward>,
}
#[must_use]
pub fn yhuap_initial_board_where_red_king_points_upward() -> Board {
rotate_board(&yhuap_initial_board_where_black_king_points_upward())
}
#[must_use]
#[allow(clippy::too_many_lines)]
pub const fn yhuap_initial_board_where_black_king_points_upward() -> Board {
Board([
[
Some(Piece::NonTam2Piece {
color: Color::Huok2,
prof: Profession::Kua2,
side: Side::Downward,
}),
Some(Piece::NonTam2Piece {
color: Color::Huok2,
prof: Profession::Maun1,
side: Side::Downward,
}),
Some(Piece::NonTam2Piece {
color: Color::Huok2,
prof: Profession::Kaun1,
side: Side::Downward,
}),
Some(Piece::NonTam2Piece {
color: Color::Huok2,
prof: Profession::Uai1,
side: Side::Downward,
}),
Some(Piece::NonTam2Piece {
color: Color::Kok1,
prof: Profession::Io,
side: Side::Downward,
}),
Some(Piece::NonTam2Piece {
color: Color::Kok1,
prof: Profession::Uai1,
side: Side::Downward,
}),
Some(Piece::NonTam2Piece {
color: Color::Kok1,
prof: Profession::Kaun1,
side: Side::Downward,
}),
Some(Piece::NonTam2Piece {
color: Color::Kok1,
prof: Profession::Maun1,
side: Side::Downward,
}),
Some(Piece::NonTam2Piece {
color: Color::Kok1,
prof: Profession::Kua2,
side: Side::Downward,
}),
],
[
Some(Piece::NonTam2Piece {
color: Color::Kok1,
prof: Profession::Tuk2,
side: Side::Downward,
}),
Some(Piece::NonTam2Piece {
color: Color::Kok1,
prof: Profession::Gua2,
side: Side::Downward,
}),
None,
Some(Piece::NonTam2Piece {
color: Color::Kok1,
prof: Profession::Dau2,
side: Side::Downward,
}),
None,
Some(Piece::NonTam2Piece {
color: Color::Huok2,
prof: Profession::Dau2,
side: Side::Downward,
}),
None,
Some(Piece::NonTam2Piece {
color: Color::Huok2,
prof: Profession::Gua2,
side: Side::Downward,
}),
Some(Piece::NonTam2Piece {
color: Color::Huok2,
prof: Profession::Tuk2,
side: Side::Downward,
}),
],
[
Some(Piece::NonTam2Piece {
color: Color::Huok2,
prof: Profession::Kauk2,
side: Side::Downward,
}),
Some(Piece::NonTam2Piece {
color: Color::Kok1,
prof: Profession::Kauk2,
side: Side::Downward,
}),
Some(Piece::NonTam2Piece {
color: Color::Huok2,
prof: Profession::Kauk2,
side: Side::Downward,
}),
Some(Piece::NonTam2Piece {
color: Color::Kok1,
prof: Profession::Kauk2,
side: Side::Downward,
}),
Some(Piece::NonTam2Piece {
color: Color::Kok1,
prof: Profession::Nuak1,
side: Side::Downward,
}),
Some(Piece::NonTam2Piece {
color: Color::Kok1,
prof: Profession::Kauk2,
side: Side::Downward,
}),
Some(Piece::NonTam2Piece {
color: Color::Huok2,
prof: Profession::Kauk2,
side: Side::Downward,
}),
Some(Piece::NonTam2Piece {
color: Color::Kok1,
prof: Profession::Kauk2,
side: Side::Downward,
}),
Some(Piece::NonTam2Piece {
color: Color::Huok2,
prof: Profession::Kauk2,
side: Side::Downward,
}),
],
[None, None, None, None, None, None, None, None, None],
[
None,
None,
None,
None,
Some(Piece::Tam2),
None,
None,
None,
None,
],
[None, None, None, None, None, None, None, None, None],
[
Some(Piece::NonTam2Piece {
color: Color::Huok2,
prof: Profession::Kauk2,
side: Side::Upward,
}),
Some(Piece::NonTam2Piece {
color: Color::Kok1,
prof: Profession::Kauk2,
side: Side::Upward,
}),
Some(Piece::NonTam2Piece {
color: Color::Huok2,
prof: Profession::Kauk2,
side: Side::Upward,
}),
Some(Piece::NonTam2Piece {
color: Color::Kok1,
prof: Profession::Kauk2,
side: Side::Upward,
}),
Some(Piece::NonTam2Piece {
color: Color::Huok2,
prof: Profession::Nuak1,
side: Side::Upward,
}),
Some(Piece::NonTam2Piece {
color: Color::Kok1,
prof: Profession::Kauk2,
side: Side::Upward,
}),
Some(Piece::NonTam2Piece {
color: Color::Huok2,
prof: Profession::Kauk2,
side: Side::Upward,
}),
Some(Piece::NonTam2Piece {
color: Color::Kok1,
prof: Profession::Kauk2,
side: Side::Upward,
}),
Some(Piece::NonTam2Piece {
color: Color::Huok2,
prof: Profession::Kauk2,
side: Side::Upward,
}),
],
[
Some(Piece::NonTam2Piece {
color: Color::Huok2,
prof: Profession::Tuk2,
side: Side::Upward,
}),
Some(Piece::NonTam2Piece {
color: Color::Huok2,
prof: Profession::Gua2,
side: Side::Upward,
}),
None,
Some(Piece::NonTam2Piece {
color: Color::Huok2,
prof: Profession::Dau2,
side: Side::Upward,
}),
None,
Some(Piece::NonTam2Piece {
color: Color::Kok1,
prof: Profession::Dau2,
side: Side::Upward,
}),
None,
Some(Piece::NonTam2Piece {
color: Color::Kok1,
prof: Profession::Gua2,
side: Side::Upward,
}),
Some(Piece::NonTam2Piece {
color: Color::Kok1,
prof: Profession::Tuk2,
side: Side::Upward,
}),
],
[
Some(Piece::NonTam2Piece {
color: Color::Kok1,
prof: Profession::Kua2,
side: Side::Upward,
}),
Some(Piece::NonTam2Piece {
color: Color::Kok1,
prof: Profession::Maun1,
side: Side::Upward,
}),
Some(Piece::NonTam2Piece {
color: Color::Kok1,
prof: Profession::Kaun1,
side: Side::Upward,
}),
Some(Piece::NonTam2Piece {
color: Color::Kok1,
prof: Profession::Uai1,
side: Side::Upward,
}),
Some(Piece::NonTam2Piece {
color: Color::Huok2,
prof: Profession::Io,
side: Side::Upward,
}),
Some(Piece::NonTam2Piece {
color: Color::Huok2,
prof: Profession::Uai1,
side: Side::Upward,
}),
Some(Piece::NonTam2Piece {
color: Color::Huok2,
prof: Profession::Kaun1,
side: Side::Upward,
}),
Some(Piece::NonTam2Piece {
color: Color::Huok2,
prof: Profession::Maun1,
side: Side::Upward,
}),
Some(Piece::NonTam2Piece {
color: Color::Huok2,
prof: Profession::Kua2,
side: Side::Upward,
}),
],
])
}
impl Field {
pub fn insert_nontam_piece_into_hop1zuo1(
&mut self,
color: Color,
prof: Profession,
side: Side,
) {
match side {
Side::Upward => self
.hop1zuo1of_upward
.push(NonTam2PieceUpward { color, prof }),
Side::Downward => self
.hop1zuo1of_downward
.push(NonTam2PieceDownward { color, prof }),
}
}
}
#[must_use]
pub fn rotate_board(b: &Board) -> Board {
let mut ans: Board = Board([
[None, None, None, None, None, None, None, None, None],
[None, None, None, None, None, None, None, None, None],
[None, None, None, None, None, None, None, None, None],
[None, None, None, None, None, None, None, None, None],
[None, None, None, None, None, None, None, None, None],
[None, None, None, None, None, None, None, None, None],
[None, None, None, None, None, None, None, None, None],
[None, None, None, None, None, None, None, None, None],
[None, None, None, None, None, None, None, None, None],
]);
for i in 0..9 {
for j in 0..9 {
ans.0[i][j] = rotate_piece_or_null(b.0[8 - i][8 - j]);
}
}
ans
}
#[must_use]
pub fn distance(a: Coord, b: Coord) -> i32 {
let [x1, y1] = a;
let [x2, y2] = b;
let x_distance = (i32::try_from(x1).unwrap() - i32::try_from(x2).unwrap()).abs();
let y_distance = (i32::try_from(y1).unwrap() - i32::try_from(y2).unwrap()).abs();
x_distance.max(y_distance)
}
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
pub enum PureMove {
NonTamMoveSrcDst {
src: Coord,
dest: Coord,
is_water_entry_ciurl: bool,
},
NonTamMoveSrcStepDstFinite {
src: Coord,
step: Coord,
dest: Coord,
is_water_entry_ciurl: bool,
},
InfAfterStep {
src: Coord,
step: Coord,
planned_direction: Coord,
},
NonTamMoveFromHopZuo {
color: Color,
prof: Profession,
dest: Coord,
},
TamMoveNoStep {
src: Coord,
first_dest: Coord,
second_dest: Coord,
},
TamMoveStepsDuringFormer {
src: Coord,
step: Coord,
first_dest: Coord,
second_dest: Coord,
},
TamMoveStepsDuringLatter {
src: Coord,
step: Coord,
first_dest: Coord,
second_dest: Coord,
},
}
impl PureMove {
#[must_use]
pub fn serialize(self) -> String {
match self {
PureMove::InfAfterStep {
src,
step,
planned_direction,
} => format!(
"{}片{}心{}",
serialize_coord(src),
serialize_coord(step),
serialize_coord(planned_direction)
),
PureMove::NonTamMoveFromHopZuo { color, prof, dest } => format!(
"{}{}{}",
cetkaik_fundamental::serialize_color(color),
cetkaik_fundamental::serialize_prof(prof),
serialize_coord(dest)
),
PureMove::NonTamMoveSrcDst {
src,
dest,
is_water_entry_ciurl,
} => format!(
"{}片{}{}",
serialize_coord(src),
serialize_coord(dest),
if is_water_entry_ciurl { "水" } else { "" }
),
PureMove::NonTamMoveSrcStepDstFinite {
src,
dest,
is_water_entry_ciurl,
step,
} => format!(
"{}片{}{}{}",
serialize_coord(src),
serialize_coord(step),
serialize_coord(dest),
if is_water_entry_ciurl { "水" } else { "" }
),
PureMove::TamMoveNoStep {
src,
first_dest,
second_dest,
} => format!(
"{}皇[{}]{}",
serialize_coord(src),
serialize_coord(first_dest),
serialize_coord(second_dest)
),
PureMove::TamMoveStepsDuringFormer {
src,
first_dest,
second_dest,
step,
} => format!(
"{}皇{}[{}]{}",
serialize_coord(src),
serialize_coord(step),
serialize_coord(first_dest),
serialize_coord(second_dest)
),
PureMove::TamMoveStepsDuringLatter {
src,
first_dest,
second_dest,
step,
} => format!(
"{}皇[{}]{}{}",
serialize_coord(src),
serialize_coord(first_dest),
serialize_coord(step),
serialize_coord(second_dest)
),
}
}
}
impl IsBoard for Board {
type PieceWithSide = Piece;
type Coord = Coord;
fn peek(&self, c: Self::Coord) -> Option<Self::PieceWithSide> {
self.0[c[0]][c[1]]
}
fn pop(&mut self, c: Self::Coord) -> Option<Self::PieceWithSide> {
let o = self.peek(c);
self.0[c[0]][c[1]] = None;
o
}
fn put(&mut self, c: Self::Coord, p: Option<Self::PieceWithSide>) {
self.0[c[0]][c[1]] = p;
}
fn assert_empty(&self, c: Self::Coord) {
assert!(self.peek(c).is_none());
}
fn assert_occupied(&self, c: Self::Coord) {
assert!(self.peek(c).is_some());
}
type EmptySquaresIter = std::vec::IntoIter<[usize; 2]>;
fn empty_squares(&self) -> Self::EmptySquaresIter {
let mut ans = vec![];
for row in 0..9 {
for column in 0..9 {
let coord = [row, column];
if self.peek(coord).is_none() {
ans.push(coord);
}
}
}
ans.into_iter()
}
}
impl IsField for Field {
type Board = Board;
type Coord = Coord;
type PieceWithSide = Piece;
type Side = Side;
fn move_nontam_piece_from_src_to_dest_while_taking_opponent_piece_if_needed(
&self,
src: Self::Coord,
dest: Self::Coord,
whose_turn: Self::Side,
) -> Result<Self, &'static str>
where
Self: std::marker::Sized,
{
let mut new_self = self.clone();
let src_piece =
new_self.current_board.0[src[0]][src[1]].ok_or("src does not contain a piece")?;
let Piece::NonTam2Piece { color: _color, prof: _prof, side } = src_piece
else {
return Err("Expected a NonTam2Piece to be present at the src, but found a Tam2")
};
if whose_turn != side {
return Err("Found the opponent piece at the src");
}
let maybe_captured_piece = new_self.current_board.0[dest[0]][dest[1]];
new_self.current_board.0[dest[0]][dest[1]] = Some(src_piece);
if let Some(captured_piece) = maybe_captured_piece {
match captured_piece {
Piece::Tam2 => return Err("Tried to capture a Tam2"),
Piece::NonTam2Piece {
color: captured_piece_color,
prof: captured_piece_prof,
side: captured_piece_side,
} => {
if captured_piece_side == whose_turn {
return Err("Tried to capture an ally");
}
match whose_turn {
Side::Downward => new_self.hop1zuo1of_downward.push(NonTam2PieceDownward {
color: captured_piece_color,
prof: captured_piece_prof,
}),
Side::Upward => new_self.hop1zuo1of_upward.push(NonTam2PieceUpward {
color: captured_piece_color,
prof: captured_piece_prof,
}),
}
}
}
}
Ok(new_self)
}
fn as_board(&self) -> &Self::Board {
&self.current_board
}
fn as_board_mut(&mut self) -> &mut Self::Board {
&mut self.current_board
}
#[must_use]
fn search_from_hop1zuo1_and_parachute_at(
&self,
color: Color,
prof: Profession,
side: Side,
to: Coord,
) -> Option<Self> {
match side {
Side::Upward => {
let mut new_self = self.clone();
let index = new_self
.hop1zuo1of_upward
.iter()
.position(|x| *x == NonTam2PieceUpward { color, prof })?;
new_self.hop1zuo1of_upward.remove(index);
if self.current_board.0[to[0]][to[1]].is_some() {
return None;
}
new_self.current_board.0[to[0]][to[1]] =
Some(Piece::NonTam2Piece { color, prof, side });
Some(new_self)
}
Side::Downward => {
let mut new_self = self.clone();
let index = new_self
.hop1zuo1of_downward
.iter()
.position(|x| *x == NonTam2PieceDownward { color, prof })?;
new_self.hop1zuo1of_downward.remove(index);
if self.current_board.0[to[0]][to[1]].is_some() {
return None;
}
new_self.current_board.0[to[0]][to[1]] =
Some(Piece::NonTam2Piece { color, prof, side });
Some(new_self)
}
}
}
}
impl IsPieceWithSide for Piece {
type Side = Side;
fn match_on_piece_and_apply<U>(
self,
f_tam: &dyn Fn() -> U,
f_piece: &dyn Fn(Color, Profession, Self::Side) -> U,
) -> U {
match self {
Piece::Tam2 => f_tam(),
Piece::NonTam2Piece { color, prof, side } => f_piece(color, prof, side),
}
}
}