aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authoreug-vs <eugene@eug-vs.xyz>2023-01-20 23:52:27 +0300
committereug-vs <eugene@eug-vs.xyz>2023-01-20 23:52:27 +0300
commit203abeafb94b977236bd6a9a2d0f33173f6f0d1b (patch)
treebb8e7410b791cd40596f171a819e25009e178eac /src
parent1b33595091334bb17db78298cad4eefa7b448c22 (diff)
downloadchessnost-203abeafb94b977236bd6a9a2d0f33173f6f0d1b.tar.gz
feat: implement Bitboard.pop_count()
Diffstat (limited to 'src')
-rw-r--r--src/bitboard.rs42
-rw-r--r--src/main.rs20
2 files changed, 46 insertions, 16 deletions
diff --git a/src/bitboard.rs b/src/bitboard.rs
new file mode 100644
index 0000000..a2d34cb
--- /dev/null
+++ b/src/bitboard.rs
@@ -0,0 +1,42 @@
+use std::fmt;
+
+pub struct Bitboard(pub u64);
+
+impl fmt::Display for Bitboard {
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+ for index in 0..64 {
+ f.write_str(if &self.0 >> index & 1 == 1 { "1" } else { "." });
+ if (index + 1) % 8 == 0 {
+ f.write_str("\n");
+ }
+ }
+ return write!(f, "\n")
+ }
+}
+
+impl Bitboard {
+ pub fn pop_count(&self) -> i32 {
+ if self.0 == 0 {
+ return 0;
+ }
+ return Bitboard::pop_count(&Bitboard(self.0 >> 1)) + (self.0 & 1) as i32;
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn test_print() {
+ const bb: Bitboard = Bitboard(127);
+ println!("{}", bb);
+ }
+
+ #[test]
+ fn test_pop_count() {
+ const bb: Bitboard = Bitboard(127);
+ assert_eq!(bb.pop_count(), 7);
+ }
+}
+
diff --git a/src/main.rs b/src/main.rs
index 822f773..e0d9912 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,22 +1,10 @@
-use std::fmt;
-
-struct Bitboard(u64);
-
-impl fmt::Display for Bitboard {
- fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
- for index in (0..64) {
- f.write_str(if (&self.0 >> index & 1 == 1) { "1" } else { "." });
- if ((index + 1) % 8 == 0) {
- f.write_str("\n");
- }
- }
- return write!(f, "\n")
- }
-}
+mod bitboard;
+use bitboard::*;
fn main() {
- const bb:Bitboard = Bitboard(33);
+ const bb: Bitboard = Bitboard(127);
println!("Hello, world!");
println!("{}", bb);
println!("{}", bb.0);
+ println!("{}", bb.pop_count());
}