cozy_chess_types/file.rs
1use crate::*;
2
3crate::helpers::simple_enum! {
4 /// A file on a chessboard.
5 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
6 pub enum File {
7 /// The A file.
8 A,
9 /// The B file.
10 B,
11 /// The C file.
12 C,
13 /// The D file.
14 D,
15 /// The E file.
16 E,
17 /// The F file.
18 F,
19 /// The G file.
20 G,
21 /// The H file.
22 H
23 }
24}
25
26crate::helpers::enum_char_conv! {
27 File, FileParseError {
28 A = 'a',
29 B = 'b',
30 C = 'c',
31 D = 'd',
32 E = 'e',
33 F = 'f',
34 G = 'g',
35 H = 'h'
36 }
37}
38
39impl File {
40 /// Flip the file.
41 /// # Examples
42 /// ```
43 /// # use cozy_chess_types::*;
44 /// assert_eq!(File::A.flip(), File::H);
45 /// ```
46 #[inline(always)]
47 pub const fn flip(self) -> Self {
48 Self::index_const(Self::H as usize - self as usize)
49 }
50
51 /// Get a bitboard with all squares on this file set.
52 /// # Examples
53 /// ```
54 /// # use cozy_chess_types::*;
55 /// assert_eq!(File::B.bitboard(), bitboard! {
56 /// . X . . . . . .
57 /// . X . . . . . .
58 /// . X . . . . . .
59 /// . X . . . . . .
60 /// . X . . . . . .
61 /// . X . . . . . .
62 /// . X . . . . . .
63 /// . X . . . . . .
64 /// });
65 /// ```
66 #[inline(always)]
67 pub const fn bitboard(self) -> BitBoard {
68 BitBoard(u64::from_ne_bytes([
69 0b00000001,
70 0b00000001,
71 0b00000001,
72 0b00000001,
73 0b00000001,
74 0b00000001,
75 0b00000001,
76 0b00000001
77 ]) << self as u8)
78 }
79
80 /// Get a bitboard with all squares on adjacent files set.
81 /// # Examples
82 /// ```
83 /// # use cozy_chess_types::*;
84 /// assert_eq!(File::C.adjacent(), bitboard! {
85 /// . X . X . . . .
86 /// . X . X . . . .
87 /// . X . X . . . .
88 /// . X . X . . . .
89 /// . X . X . . . .
90 /// . X . X . . . .
91 /// . X . X . . . .
92 /// . X . X . . . .
93 /// });
94 /// assert_eq!(File::A.adjacent(), bitboard! {
95 /// . X . . . . . .
96 /// . X . . . . . .
97 /// . X . . . . . .
98 /// . X . . . . . .
99 /// . X . . . . . .
100 /// . X . . . . . .
101 /// . X . . . . . .
102 /// . X . . . . . .
103 /// });
104 /// assert_eq!(File::H.adjacent(), bitboard! {
105 /// . . . . . . X .
106 /// . . . . . . X .
107 /// . . . . . . X .
108 /// . . . . . . X .
109 /// . . . . . . X .
110 /// . . . . . . X .
111 /// . . . . . . X .
112 /// . . . . . . X .
113 /// });
114 /// ```
115 #[inline(always)]
116 pub const fn adjacent(self) -> BitBoard {
117 const TABLE: [BitBoard; File::NUM] = {
118 let mut table = [BitBoard::EMPTY; File::NUM];
119 let mut i = 0;
120 while i < table.len() {
121 if i > 0 {
122 table[i].0 |= File::index_const(i - 1)
123 .bitboard().0;
124 }
125 if i < (table.len() - 1) {
126 table[i].0 |= File::index_const(i + 1)
127 .bitboard().0;
128 }
129 i += 1;
130 }
131 table
132 };
133 TABLE[self as usize]
134 }
135}