### Doc example for promotion zone Source: https://docs.rs/haitaka-types/0.1.3/src/haitaka_types/rank.rs.html?search= A documentation example showing how to use the prom_zone function. The example is incomplete in the source, but it is included as displayed. ```rust 147/// Returns a [`BitBoard`] of all squares where the piece _must_ promote. 148/// 149/// This is equivalent to the ranks in the promotion zone where a piece 150/// can not be dropped. 151/// 152/// # Examples 153/// 154/// use haitaka_types::*; ``` -------------------------------- ### Run rs_haitaka examples Source: https://docs.rs/haitaka Commands to execute the find_magics and perft examples from the repository. ```bash cargo run --release --example find_magics -- --verbose cargo run --release --example perft -- 3 ``` -------------------------------- ### Run perft example Source: https://docs.rs/haitaka Execute the perft example with a depth of 5 using cargo. ```bash cargo run --release --example perft -- 5 ``` -------------------------------- ### Promotion Zone Example Source: https://docs.rs/haitaka-types/0.1.3/src/haitaka_types/rank.rs.html?search=Option%3CT%3E%2C+%28T+-%3E+U%29+-%3E+Option%3CU%3E Example usage for calculating the promotion zone. ```rust use haitaka_types::*; ``` -------------------------------- ### colored_pieces example with bitboard assertions Source: https://docs.rs/haitaka/latest/src/haitaka/board/mod.rs.html?search=Option%3CT%3E%2C+%28T+-%3E+U%29+-%3E+Option%3CU%3E Shows how to use Board::colored_pieces to get a bitboard of pieces of a specific color and type. The example uses Board::startpos() and asserts the result for white pawns, black pawns, and white lances. ```rust /// # Examples /// ``` /// # use haitaka::*; /// let board = Board::startpos(); /// let white_pawns = board.colored_pieces(Color::White, Piece::Pawn); /// assert_eq!(white_pawns, bitboard! { /// . . . . . . . . . /// . . . . . . . . . /// X X X X X X X X X /// . . . . . . . . . /// . . . . . . . . . /// . . . . . . . . . /// . . . . . . . . . /// . . . . . . . . . /// . . . . . . . . . ``` -------------------------------- ### cargo run --release --example perft -- 3 Source: https://docs.rs/haitaka/latest/haitaka/index.html?search=Option%3CT%3E%2C+%28T+-%3E+U%29+-%3E+Option%3CU%3E Runs the perft example, a perft implementation for Shogi. This is a command-line tool example. ```APIDOC ## cargo run --release --example perft -- 3 ### Description Runs the perft example, a perft implementation for Shogi. This is a command-line tool example. ### Method cargo run ### Endpoint cargo run --release --example perft -- 3 ### Parameters #### Command Line Arguments - **depth** (integer) - Required - The depth for the perft calculation. ``` -------------------------------- ### line_ray usage example Source: https://docs.rs/haitaka-types/0.1.3/src/haitaka_types/sliders/common.rs.html?search=std%3A%3Avec Shows how to use line_ray to get the full ray passing through two squares. The example asserts that the returned bitboard matches the expected pattern and that equivalent rays produce the same result. ```rust /// Get a ray on the board that passes through both squares, if it exists. /// /// These rays include the `from` and `to` square. /// /// # Examples /// ``` /// # use haitaka_types::*; /// let rays = line_ray(Square::B1, Square::I8); /// assert_eq!(rays, bitboard! { /// . . . . . . . . . /// . . . . . . . . X /// . . . . . . . X . /// . . . . . . X . . /// . . . . . X . . . /// . . . . X . . . . /// . . . X . . . . . /// . . X . . . . . . /// . X . . . . . . . /// }); /// assert_eq!(line_ray(Square::F5, Square::E4), line_ray(Square::B1, Square::I8)); /// assert_eq!(line_ray(Square::A3, Square::I3), line_ray(Square::C3, Square::F3)); /// ``` ``` -------------------------------- ### Piece::must_promote usage examples Source: https://docs.rs/haitaka-types/0.1.3/src/haitaka_types/piece.rs.html Examples demonstrating the promotion requirements for different pieces based on color and square. ```rust assert!(Piece::Knight.must_promote(Color::White, Square::I3)); assert!(!Piece::Pawn.must_promote(Color::Black, Square::B3)); assert!(!Piece::Lance.must_promote(Color::Black, Square::B3)); assert!(Piece::Knight.must_promote(Color::Black, Square::B3)); assert!(!Piece::Pawn.must_promote(Color::White, Square::H3)); assert!(!Piece::Lance.must_promote(Color::White, Square::H3)); assert!(Piece::Knight.must_promote(Color::White, Square::H3)); ``` -------------------------------- ### BitBoard shl method example Source: https://docs.rs/haitaka-types/0.1.3/src/haitaka_types/bitboard.rs.html?search= Example showing how to use the shl method to shift a bitboard left (south) along files. The example creates two bitboards and asserts that shifting bb1 by 1 results in bb2. ```rust /// # use haitaka_types::*; /// let bb1 = bitboard! { /// . . . . . . X X X /// . . . . . . . . . /// . . . . . . . . . /// . . . . . . . . . /// X X X . . . . . . /// X . X . . . . . . /// X X X . . . . . . /// . . . . . . . . . /// . . . . . . . . . /// }; /// let bb2 = bitboard! { /// . . . . . . X X X /// . . . . . . . . . /// . . . . . . . . . /// . . . . . . . . . /// . . . . . . . . . /// X X X . . . . . . /// X . X . . . . . . /// X X X . . . . . . /// . . . . . . . . . /// }; /// assert_eq!(bb1.shl(1), bb2); ``` -------------------------------- ### Doctest example for Board::play_unchecked Source: https://docs.rs/haitaka/latest/src/haitaka/board/mod.rs.html?search= Shows how to create a board from the start position, play four moves using play_unchecked, and assert the resulting board string. The expected string is 'lnsgkgsnl/1r5b1/p1ppppppp/9/1p5P1/9/PPPPPPP1P/1B5R1/LNSGKGSNL b - 5'. ```rust /// # Examples /// ``` /// # use haitaka::*; /// let mut board = Board::startpos(); /// board.play_unchecked("2g2f".parse().unwrap()); /// board.play_unchecked("8c8d".parse().unwrap()); /// board.play_unchecked("2f2e".parse().unwrap()); /// board.play_unchecked("8d8e".parse().unwrap()); /// let expected: &str = "lnsgkgsnl/1r5b1/p1ppppppp/9/1p5P1/9/PPPPPPP1P/1B5R1/LNSGKGSNL b - 5"; /// assert_eq!(format!("{}", board), expected); /// ``` ``` -------------------------------- ### Lance Pseudo-Legal Moves Example Source: https://docs.rs/haitaka-types/0.1.3/x86_64-unknown-linux-gnu/src/haitaka_types/sliders/common.rs.html Example showing the initialization of a bitboard for lance pseudo-legal moves. ```rust use haitaka_types::*; let occ = bitboard! { . . . . . X X X X . . . . . . . X . ``` -------------------------------- ### cargo run --release --example find_magics -- --verbose Source: https://docs.rs/haitaka/latest/haitaka/index.html?search=Option%3CT%3E%2C+%28T+-%3E+U%29+-%3E+Option%3CU%3E Runs the find_magics example, which generates magic numbers for slider moves. This is a command-line tool example. ```APIDOC ## cargo run --release --example find_magics -- --verbose ### Description Runs the find_magics example, which generates magic numbers for slider moves. This is a command-line tool example. ### Method cargo run ### Endpoint cargo run --release --example find_magics -- --verbose ### Parameters #### Command Line Arguments - **--verbose** (flag) - Optional - Enables verbose output. ``` -------------------------------- ### is_empty method example Source: https://docs.rs/haitaka-types/0.1.3/haitaka_types/bitboard/struct.BitBoard.html?search=std%3A%3Avec Checks if the bitboard is empty. The example asserts that an empty bitboard is empty and a non-empty one is not. ```rust assert!(BitBoard::EMPTY.is_empty()); let bb = bitboard! { . . . . . . . . . . . . . . . . . . . . X X X . . . . . . X . X X . . . . . X X X X . . . . . X . X . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . }; assert!(!bb.is_empty()); assert!(BitBoard::new(0).is_empty()); ``` -------------------------------- ### Initialize a board with the default start position Source: https://docs.rs/haitaka/latest/src/haitaka/board/mod.rs.html Demonstrates how to create a Board instance using the default start position and verify it against an SFEN string. ```rust # use haitaka::*; let sfen: &str = "lnsgkgsnl/1r5b1/ppppppppp/9/9/9/PPPPPPPPP/1B5R1/LNSGKGSNL b - 1"; assert_eq!(Board::startpos(), sfen.parse().unwrap()); ``` -------------------------------- ### king example Source: https://docs.rs/haitaka/latest/src/haitaka/board/mod.rs.html Shows how to get the king square of a given side. Panics if the color has no King. The example checks the starting position for both colors. ```rust /// # use haitaka::*; /// let board = Board::startpos(); /// assert_eq!(board.king(Color::White), Square::A5); /// assert_eq!(board.king(Color::Black), Square::I5); /// ``` ``` -------------------------------- ### color_on example Source: https://docs.rs/haitaka/latest/src/haitaka/board/mod.rs.html Shows how to get the Color of the piece on a square. Returns None if the square is empty. The example checks specific squares on the starting position. ```rust /// # use haitaka::*; /// let board = Board::startpos(); /// assert_eq!(board.color_on(Square::E5), None); /// assert_eq!(board.color_on(Square::A5), Some(Color::White)); /// assert_eq!(board.color_on(Square::I5), Some(Color::Black)); /// ``` ``` -------------------------------- ### Square::up_diagonal example Source: https://docs.rs/haitaka-types/0.1.3/src/haitaka_types/square.rs.html Gets the bitboard with the up-slanting diagonal for a square. The example asserts that Square::E5.up_diagonal() produces the diagonal from top-right to bottom-left. ```rust use haitaka_types::*; assert_eq!(Square::E5.up_diagonal(), bitboard! { . . . . . . . . X . . . . . . . X . . . . . . . X . . . . . . . X . . . . . . . X . . . . . . . X . . . . . . . X . . . . . . . X . . . . . . . X . . . . . . . . }); ``` -------------------------------- ### colored_piece_on example Source: https://docs.rs/haitaka/latest/src/haitaka/board/mod.rs.html Shows how to get the ColoredPiece on a square. This is a convenience function mainly useful in parsing SFEN strings. The example constructs a ColoredPiece and checks it against the board. ```rust /// # use haitaka::*; /// let board = Board::startpos(); /// let piece = ColoredPiece { piece: Piece::King, color: Color::Black }; /// assert_eq!(board.color_on(Square::I5), Some(Color::Black)); /// assert_eq!(board.piece_on(Square::I5), Some(Piece::King)); /// assert_eq!(board.colored_piece_on(Square::I5), Some(piece)); /// ``` ``` -------------------------------- ### startpos Source: https://docs.rs/haitaka/latest/haitaka/board/struct.Board.html?search=u32+-%3E+bool Creates a Board with the standard Shogi start position. ```APIDOC ## startpos ### Description Returns a Board configured with the default start position. ### Signature `pub fn startpos() -> Self` ### Return Value - `Self` - A new Board instance with the start position. ### Example ```rust let sfen: &str = "lnsgkgsnl/1r5b1/ppppppppp/9/9/9/PPPPPPPPP/1B5R1/LNSGKGSNL b - 1"; assert_eq!(Board::startpos(), sfen.parse().unwrap()); ``` ``` -------------------------------- ### pawn_drop_ok example Source: https://docs.rs/haitaka/latest/src/haitaka/board/mod.rs.html Shows how to check if a Pawn drop is allowed on a given square. Returns true if there is no Pawn of the same color on the file. The example tests all squares on a default board and a starting position. ```rust /// use haitaka::*; /// let board = Board::default(); /// for &square in Square::ALL.iter() { /// let bok = board.pawn_drop_ok(Color::Black, square); /// let wok = board.pawn_drop_ok(Color::White, square); /// if square.rank() as usize != 0 { /// assert!(bok); /// } /// if square.rank() as usize != 8 { /// assert!(wok); /// } /// } /// let board = Board::startpos(); /// for &square in Square::ALL.iter() { /// assert!(!board.pawn_drop_ok(Color::Black, square)); /// } /// ``` ``` -------------------------------- ### Generate all legal moves Source: https://docs.rs/haitaka/latest/haitaka/board/struct.Board.html?search=Option%3CT%3E%2C+%28T+-%3E+U%29+-%3E+Option%3CU%3E Example of using generate_moves to count all legal moves from the starting position. ```rust let board = Board::startpos(); let mut total_moves = 0; board.generate_moves(|moves| { // Done this way for demonstration. // Actual counting is best done in bulk with moves.len(). for _mv in moves { total_moves += 1; } false }); assert_eq!(total_moves, 30); ``` -------------------------------- ### Display Board Example Source: https://docs.rs/haitaka/latest/haitaka/board/struct.Board.html?search=Option%3CT%3E%2C+%28T+-%3E+U%29+-%3E+Option%3CU%3E Demonstrates how to format and display a Board instance using the Display trait. ```rust let mut board: Board = SFEN_6PIECE_HANDICAP.parse().unwrap(); assert_eq!(format!("{}", board), SFEN_6PIECE_HANDICAP); board = SFEN_4PIECE_HANDICAP.parse().unwrap(); assert_eq!(format!("{}", board), SFEN_4PIECE_HANDICAP); board = SFEN_2PIECE_HANDICAP.parse().unwrap(); assert_eq!(format!("{}", board), SFEN_2PIECE_HANDICAP); ``` -------------------------------- ### Examples: Using POS_DIA with bitboard! macro Source: https://docs.rs/haitaka-types/0.1.3/haitaka_types/square/constant.POS_DIA.html?search=std%3A%3Avec Shows how to use POS_DIA to get bitboards for specific diagonals. The examples assert that POS_DIA[8], POS_DIA[2], and POS_DIA[16] match the expected bitboards. ```rust use haitaka_types::*; assert_eq!(POS_DIA[8], bitboard! { X . . . . . . . . . X . . . . . . . . . X . . . . . . . . . X . . . . . . . . . X . . . . . . . . . X . . . . . . . . . X . . . . . . . . . X . . . . . . . . . X }); assert_eq!(POS_DIA[2], bitboard! { . . . . . . X . . . . . . . . . X . . . . . . . . . X . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . }); assert_eq!(POS_DIA[16], bitboard! { . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . X . . . . . . . . }); ``` -------------------------------- ### BitBoard::flip_files example Source: https://docs.rs/haitaka-types/0.1.3/haitaka_types/bitboard/struct.BitBoard.html?search=std%3A%3Avec Shows how to flip the bitboard's files (mirror vertically in file 5). The example defines a bitboard and asserts the result of calling flip_files. ```rust let bb = bitboard! { . . . . . . . . . . . . . . . . . . . . X X X . . . . . . X . X X . . . . . X X X X . . . . . X . X . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . }; assert_eq!(bb.flip_files(), bitboard! { . . . . . . . . . . . . . . . . . . . . . . . X X X . . . . X X . X . . . . . X X X X . . . . . . X . X . . . . . . . . . . . . . . . . . . . . . . . . . . . . . }); ``` -------------------------------- ### Example: Piece::can_promote Source: https://docs.rs/haitaka-types/0.1.3/haitaka_types/piece/enum.Piece.html?search=Option%3CT%3E%2C+%28T+-%3E+U%29+-%3E+Option%3CU%3E Shows how to use Piece::can_promote to check if a piece can promote on a given square for a given color. The example uses assertions to verify expected results. ```rust use haitaka_types::*; assert!(Piece::Pawn.can_promote(Color::Black, Square::C1)); assert!(Piece::Pawn.can_promote(Color::White, Square::G1)); assert!(! Piece::Pawn.can_promote(Color::Black, Square::H7)); assert!(! Piece::Pawn.can_promote(Color::White, Square::C3)); ``` -------------------------------- ### Generate all moves with generate_moves Source: https://docs.rs/haitaka/latest/src/haitaka/board/movegen/mod.rs.html?search=u32+-%3E+bool Example of using Board::generate_moves with a listener callback to count total moves. The listener is called multiple times and can return true to stop early. The example asserts 30 total moves from the start position. ```rust /// # Examples /// /// ``` /// # use haitaka::*; /// let board = Board::startpos(); /// let mut total_moves = 0; /// board.generate_moves(|moves| { /// // Done this way for demonstration. /// // Actual counting is best done in bulk with moves.len(). /// for _mv in moves { /// total_moves += 1; /// } /// false /// }); /// assert_eq!(total_moves, 30); /// ``` ``` -------------------------------- ### BitBoard::new Source: https://docs.rs/haitaka-types/0.1.3/src/haitaka_types/bitboard.rs.html?search= Documentation for the BitBoard::new constructor, which creates a bitboard with proper masking. ```APIDOC ## BitBoard::new ### Description Creates a new `BitBoard` from a `u128` value, masking out the unused high bits to ensure only the first 81 bits are used. This is different from the tuple constructor `BitBoard(value)`, which does not mask the high bits. ### Method `pub const fn new(value: u128) -> Self` ### Parameters - **value** (`u128`) - Required - The raw bit value to create the bitboard from. ### Returns - **Self** - A new `BitBoard` with the high bits masked out. ``` -------------------------------- ### relative_to example: assert_eq!(Square::A1.relative_to(Color::White), Square::I9) and more Source: https://docs.rs/haitaka-types/0.1.3/haitaka_types/square/enum.Square.html?search=Option%3CT%3E%2C+%28T+-%3E+U%29+-%3E+Option%3CU%3E Gets a square relative to a color, effectively rotating the board for White's perspective. The examples assert that A1 relative to White is I9, E5 relative to White is E5, and A1 relative to Black is A1. ```rust assert_eq!(Square::A1.relative_to(Color::White), Square::I9); assert_eq!(Square::E5.relative_to(Color::White), Square::E5); assert_eq!(Square::A1.relative_to(Color::Black), Square::A1); ``` -------------------------------- ### Board::startpos Source: https://docs.rs/haitaka/latest/haitaka/board/struct.Board.html?search=Option%3CT%3E%2C+%28T+-%3E+U%29+-%3E+Option%3CU%3E Creates a Board with the default start position. ```APIDOC ## Rust Function `Board::startpos` ### Description Get a board with the default start position. ### Signature `pub fn startpos() -> Self` ### Returns - `Self` - A `Board` with the default start position. ### Example ```rust let sfen: &str = "lnsgkgsnl/1r5b1/ppppppppp/9/9/9/PPPPPPPPP/1B5R1/LNSGKGSNL b - 1"; assert_eq!(Board::startpos(), sfen.parse().unwrap()); ``` ``` -------------------------------- ### Define SFEN_6PIECE_HANDICAP constant Source: https://docs.rs/haitaka/latest/haitaka/board/constant.SFEN_6PIECE_HANDICAP.html?search=Option%3CT%3E%2C+%28T+-%3E+U%29+-%3E+Option%3CU%3E Public constant in haitaka::board that holds the SFEN string for a 6-piece handicap starting position. Use it when you need the standard SFEN representation for this handicap setup. ```rust pub const SFEN_6PIECE_HANDICAP: &str = "2sgkgs2/9/ppppppppp/9/9/9/PPPPPPPPP/1B5R1/LNSGKGSNL w - 2"; ``` -------------------------------- ### Initialize default board with Board::startpos Source: https://docs.rs/haitaka/latest/haitaka/board/struct.Board.html?search=Option%3CT%3E%2C+%28T+-%3E+U%29+-%3E+Option%3CU%3E Returns a Board instance initialized to the default Shogi starting position. ```rust let sfen: &str = "lnsgkgsnl/1r5b1/ppppppppp/9/9/9/PPPPPPPPP/1B5R1/LNSGKGSNL b - 1"; assert_eq!(Board::startpos(), sfen.parse().unwrap()); ``` -------------------------------- ### rank.rs: Rank enum, bitboard constants, and zone functions Source: https://docs.rs/haitaka-types/0.1.3/src/haitaka_types/rank.rs.html The complete source of rank.rs defines the Rank enum, its character conversion, rank bitboards, and zone helper functions. It uses macros simple_enum and enum_char_conv, and constants for north/south zones. The file ends with a doc comment that starts an example block but the example code is not shown on this page. ```rust 1//! The [`Rank`] enum represents the ranks (rows) on a Shogi board 2//! 3//! Shogi ranks are indicated by the letters abdefghi or by Kanji numerals 一二三四五六七八九. 4//! Here we use capital letters to indicate the ranks: Rank::A .. Rank::I. 5//! 6 7use crate::*; 8 9crate::helpers::simple_enum! { 10 /// A rank (row) on a shogi board. 11 /// 12 /// Ranks are indicated by letters or by Kanji numerals. 13 /// Rank 'a' ("一") is the top-most rank in board diagrams which are 14 /// always shown from the perspective of the Black (Sente) player. 15 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)] 16 pub enum Rank { 17 /// The first rank. 18 A, 19 /// The second rank. 20 B, 21 /// The third rank. 22 C, 23 /// The fourth rank. 24 D, 25 /// The fifth rank. 26 E, 27 /// The sixth rank. 28 F, 29 /// The seventh rank. 30 G, 31 /// The eighth rank. 32 H, 33 /// The ninth rank. 34 I 35 } 36} 37 38crate::helpers::enum_char_conv! { 39 Rank, RankParseError { 40 A = 'a', 41 B = 'b', 42 C = 'c', 43 D = 'd', 44 E = 'e', 45 F = 'f', 46 G = 'g', 47 H = 'h', 48 I = 'i' 49 } 50} 51 52// MASK corresponds to all set bits in Rank::A. 53// Remember that the board is oriented so that File 1 corresponds with 54// the LSB bits in a bitboard. This makes it a little less convenient 55// to handle ranks. 56 57const MASK: u128 = 0x1008040201008040201; 58const RANK_A: BitBoard = BitBoard(MASK); 59const RANK_B: BitBoard = BitBoard(MASK << 1); 60const RANK_C: BitBoard = BitBoard(MASK << 2); 61const RANK_D: BitBoard = BitBoard(MASK << 3); 62const RANK_E: BitBoard = BitBoard(MASK << 4); 63const RANK_F: BitBoard = BitBoard(MASK << 5); 64const RANK_G: BitBoard = BitBoard(MASK << 6); 65const RANK_H: BitBoard = BitBoard(MASK << 7); 66const RANK_I: BitBoard = BitBoard(MASK << 8); 67 68// north and south, given the usual board diagrams 69 70const NORTH_A: BitBoard = BitBoard::EMPTY; 71const NORTH_B: BitBoard = RANK_A; 72const NORTH_C: BitBoard = NORTH_B.bitor(RANK_B); 73const NORTH_D: BitBoard = NORTH_C.bitor(RANK_C); 74const NORTH_E: BitBoard = NORTH_D.bitor(RANK_D); 75const NORTH_F: BitBoard = NORTH_E.bitor(RANK_E); 76const NORTH_G: BitBoard = NORTH_F.bitor(RANK_F); 77const NORTH_H: BitBoard = NORTH_G.bitor(RANK_G); 78const NORTH_I: BitBoard = NORTH_H.bitor(RANK_H); 79 80const SOUTH_I: BitBoard = BitBoard::EMPTY; 81const SOUTH_H: BitBoard = RANK_I; 82const SOUTH_G: BitBoard = SOUTH_H.bitor(RANK_H); 83const SOUTH_F: BitBoard = SOUTH_G.bitor(RANK_G); 84const SOUTH_E: BitBoard = SOUTH_F.bitor(RANK_F); 85const SOUTH_D: BitBoard = SOUTH_E.bitor(RANK_E); 86const SOUTH_C: BitBoard = SOUTH_D.bitor(RANK_D); 87const SOUTH_B: BitBoard = SOUTH_C.bitor(RANK_C); 88const SOUTH_A: BitBoard = SOUTH_B.bitor(RANK_B); 89 90/// Get the no-fly-zones for a piece. 91/// 92/// Returns a BitBoard where a piece may _not_ be dropped. 93/// 94#[inline(always)] 95pub const fn no_fly_zone(color: Color, piece: Piece) -> BitBoard { 96 match piece { 97 Piece::Pawn | Piece::Lance => { 98 if color as usize == Color::White as usize { 99 RANK_I 100 } else { 101 RANK_A 102 } 103 } 104 Piece::Knight => { 105 if color as usize == Color::White as usize { 106 RANK_I.bitor(RANK_H) 107 } else { 108 RANK_A.bitor(RANK_B) 109 } 110 } 111 _ => BitBoard::EMPTY, 112 } 113} 114 115/// Returns a BitBoard representing all squares where a piece may 116/// be dropped. This is the inverse of `no_fly_zone`. 117#[inline(always)] 118pub const fn drop_zone(color: Color, piece: Piece) -> BitBoard { 119 match piece { 120 Piece::Pawn | Piece::Lance => { 121 if color as usize == Color::White as usize { 122 NORTH_I 123 } else { 124 SOUTH_A 125 } 126 } 127 Piece::Knight => { 128 if color as usize == Color::White as usize { 129 NORTH_H 130 } else { 131 SOUTH_B 132 } 133 } 134 _ => BitBoard::FULL, 135 } 136} 137 138/// Returns a [`BitBoard`] representing the promotion zone for the color. 139#[inline(always)] 140pub const fn prom_zone(color: Color) -> BitBoard { 141 match color { 142 Color::White => SOUTH_F, 143 Color::Black => NORTH_D, 144 } 145} 146 147/// Returns a [`BitBoard`] of all squares where the piece _must_ promote. 148/// 149/// This is equivalent to the ranks in the promotion zone where a piece 150/// can not be dropped. 151/// 152/// # Examples 153/// ``` ``` -------------------------------- ### Using the bitboard! macro Source: https://docs.rs/haitaka-types/0.1.3/src/haitaka_types/bitboard.rs.html?search=u32+-%3E+bool Shows how to define a BitBoard using the visual macro syntax and verify it against rank and file bitboards. ```rust # use haitaka_types::*; let bb = bitboard! { . . . X . . . . . . . . X . . . . . . . . X . . . . . . . . X . . . . . . . . X . . . . . X X X . X X X X X . . . X . . . . . . . . X . . . . . . . . X . . . . . }; assert_eq!(bb, File::Six.bitboard() ^ Rank::F.bitboard()); ``` -------------------------------- ### Example usage of get_rook_file_moves Source: https://docs.rs/haitaka-types/0.1.3/haitaka_types/sliders/fn.get_rook_file_moves.html This example defines an occupancy bitboard and the expected moves for a rook on e5. It uses the `bitboard!` macro from `haitaka_types`. ```rust use haitaka_types::*; let occ = bitboard! { . . . . . . . . . . . . . X . . X . . . X . . . . . . . . . . . . . . . X X . . X . . X . . . . . . . . . . . . . . . . X . . . X . . X . . . . . . . . . . . . . }; let mov_e5 = bitboard! { . . . . . . . . . . . . . X . . . . . . . . X . . . . . . . . X . . . . . . . . * . . . . . . . . X . . . . . . . . X . . . . . . . . X . . . . . . . . . . . . . }; ``` -------------------------------- ### Rust Default implementation for Board in board/mod.rs Source: https://docs.rs/haitaka/latest/src/haitaka/board/mod.rs.html?search=Option%3CT%3E%2C+%28T+-%3E+U%29+-%3E+Option%3CU%3E Provides `Default` for `Board` that initializes an empty board with no pieces, useful for Tsume Shogi setup or debugging; use `Board::startpos()` for the standard start position. ```rust 64/// Default only initializes an empty board. 65/// 66/// This may be useful for setting up Tsume Shogi positions and for debugging. 67/// Use [`Board::startpos`] to initialize the default start position. 68impl Default for Board { 69 fn default() -> Self { 70 Self { 71 inner: ZobristBoard::empty(), 72 pinned: BitBoard::EMPTY, 73 checkers: BitBoard::EMPTY, 74 no_pawn_on_file: [BitBoard::FULL; Color::NUM], 75 move_number: 0, 76 } 77 } 78} ``` -------------------------------- ### Get Move Number with `move_number()` in Rust Source: https://docs.rs/haitaka/latest/src/haitaka/board/mod.rs.html?search=std%3A%3Avec Shows that `board.move_number()` returns 1 for the start position and increments by 1 after each move, as shown via assertions after playing "2g2f" and "8c8d". ```Rust /// Get the [move number]. /// /// In Shogi, other than in International Chess, moves are always numbered /// by their "half-move number". /// /// # Examples /// /// ``` /// # use haitaka::*; /// let mut board = Board::startpos(); /// assert_eq!(board.move_number(), 1); /// board.play("2g2f".parse().unwrap()); /// assert_eq!(board.move_number(), 2); /// board.play("8c8d".parse().unwrap()); /// assert_eq!(board.move_number(), 3); /// ``` ``` -------------------------------- ### Board methods: startpos, hand, has_in_hand, unchecked_set_hand, and unchecked_put (haitaka::board::mod.rs) Source: https://docs.rs/haitaka/latest/src/haitaka/board/mod.rs.html?search= Part of the Board impl block: includes a doc-test example for Board::startpos(), plus methods for hand access and unchecked, inline operations. The doc-test uses an SFEN string to assert the parsed board equals the start position. ```rust 80impl Board { 81 /// Get a board with the default start position. 82 /// 83 /// # Examples 84 /// ``` 85 /// # use haitaka::*; 86 /// let sfen: &str = "lnsgkgsnl/1r5b1/ppppppppp/9/9/9/PPPPPPPPP/1B5R1/LNSGKGSNL b - 1"; 87 /// assert_eq!(Board::startpos(), sfen.parse().unwrap()); 88 /// ``` 89 pub fn startpos() -> Self { 90 Self::from_sfen(SFEN_STARTPOS).unwrap() 91 } 92 93 /// Return a reference to the hand for color. 94 pub fn hand(&self, color: Color) -> &[u8; Piece::NUM] { 95 self.inner.hand(color) 96 } /// Does color have no pieces in hand? /// #[inline(always)] pub fn is_hand_empty(&self, color: Color) -> bool { self.inner.is_hand_empty(color) } /// Does color have the given piece in hand? /// #[inline(always)] pub fn has_in_hand(&self, color: Color, piece: Piece) -> bool { self.inner.hand(color)[piece as usize] > 0 } /// Return the number of pieces of the given piece type that color has in hand. /// #[inline(always)] pub fn num_in_hand(&self, color: Color, piece: Piece) -> u8 { self.inner.num_in_hand(color, piece) } /// Set the count of a piece in hand for color. /// /// This function performs no checks in the validity of count! /// /// #[inline(always)] pub fn unchecked_set_hand(&mut self, color: Color, piece: Piece, count: u8) { self.inner.unchecked_set_hand(color, piece, count); } #[inline(always)] pub fn take_in_hand(&mut self, color: Color, piece: Piece) { self.inner.take_in_hand(color, piece); } #[inline(always)] pub fn unchecked_put(&mut self, color: Color, piece: Piece, square: Square) { self.inner.xor_square(piece, color, square); if piece == Piece::Pawn { self.no_pawn_on_file[color as usize] &= !square.file().bitboard(); } } } ``` -------------------------------- ### Example: assert_eq! with bitboard! for Square::E5 and Square::A9 Source: https://docs.rs/haitaka-types/0.1.3/haitaka_types/sliders/fn.rook_pseudo_attacks.html Shows usage of `rook_pseudo_attacks` with `assert_eq!` and the `bitboard!` macro for two squares. The example imports `haitaka_types::*` and verifies the expected bitboards. ```rust use haitaka_types::*; assert_eq!(rook_pseudo_attacks(Square::E5), bitboard! { . . . . X . . . . . . . . X . . . . . . . . X . . . . . . . . X . . . . X X X X . X X X X . . . . X . . . . . . . . X . . . . . . . . X . . . . . . . . X . . . . }); assert_eq!(rook_pseudo_attacks(Square::A9), bitboard! { . X X X X X X X X X . . . . . . . . X . . . . . . . . X . . . . . . . . X . . . . . . . . X . . . . . . . . X . . . . . . . . ``` -------------------------------- ### File::bitboard example Source: https://docs.rs/haitaka-types/0.1.3/haitaka_types/file/enum.File.html Example for File::bitboard, which returns a bitboard with all squares on this file set. File 1 is the east-most file in board diagrams. ```rust assert_eq!(File::Eight.bitboard(), bitboard! { . X . . . . . . . . X . . . . . . . . X . . . . . . . . X . . . . . . . . X . . . . . . . . X . . . . . . . . X . . . . . . . . X . . . . . . . . X . . . . . . . }); ``` -------------------------------- ### File::west example Source: https://docs.rs/haitaka-types/0.1.3/haitaka_types/file/enum.File.html Example for File::west, which returns a bitboard with all squares to the West of this file set. It imports haitaka_types::* and shows edge cases and a full bitboard. ```rust use haitaka_types::*; assert_eq!(File::Nine.west(), BitBoard::EMPTY); assert_eq!(File::Eight.west(), File::Nine.bitboard()); assert_eq!(File::Two.west(), bitboard!{ X X X X X X X . . X X X X X X X . . X X X X X X X . . X X X X X X X . . X X X X X X X . . X X X X X X X . . X X X X X X X . . X X X X X X X . . X X X X X X X . . }); ``` -------------------------------- ### lance_pseudo_attacks usage example Source: https://docs.rs/haitaka-types/0.1.3/src/haitaka_types/sliders/common.rs.html?search=u32+-%3E+bool Example of using lance_pseudo_attacks with an occupancy bitboard. ```rust /// use haitaka_types::*; /// let occ = bitboard! { /// . . . . . X X X X /// . . . . . . . X . /// . . . . . X . X X ```