[board] Implement BitAnd and BitOr on BitBoard and make it's u64 private
This commit is contained in:
		
							parent
							
								
									aa7e901241
								
							
						
					
					
						commit
						6af64171a2
					
				
					 2 changed files with 69 additions and 28 deletions
				
			
		|  | @ -2,11 +2,20 @@ | |||
| 
 | ||||
| use crate::square::Square; | ||||
| use std::arch::asm; | ||||
| use std::ops::{BitAnd, BitOr}; | ||||
| 
 | ||||
| #[derive(Clone, Copy, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)] | ||||
| pub struct BitBoard(pub u64); | ||||
| pub struct BitBoard(u64); | ||||
| 
 | ||||
| impl BitBoard { | ||||
|     pub fn empty() -> BitBoard { | ||||
|         BitBoard(0) | ||||
|     } | ||||
| 
 | ||||
|     pub fn from_bit_field(bits: u64) -> BitBoard { | ||||
|         BitBoard(bits) | ||||
|     } | ||||
| 
 | ||||
|     fn is_empty(&self) -> bool { | ||||
|         self.0 == 0 | ||||
|     } | ||||
|  | @ -38,6 +47,38 @@ impl BitBoard { | |||
|     } | ||||
| } | ||||
| 
 | ||||
| impl BitAnd for BitBoard { | ||||
|     type Output = BitBoard; | ||||
| 
 | ||||
|     fn bitand(self, rhs: Self) -> Self { | ||||
|         BitBoard(self.0 & rhs.0) | ||||
|     } | ||||
| } | ||||
| 
 | ||||
| impl BitAnd<&BitBoard> for BitBoard { | ||||
|     type Output = BitBoard; | ||||
| 
 | ||||
|     fn bitand(self, rhs: &Self) -> Self { | ||||
|         BitBoard(self.0 & rhs.0) | ||||
|     } | ||||
| } | ||||
| 
 | ||||
| impl BitOr for BitBoard { | ||||
|     type Output = BitBoard; | ||||
| 
 | ||||
|     fn bitor(self, rhs: Self) -> Self { | ||||
|         BitBoard(self.0 | rhs.0) | ||||
|     } | ||||
| } | ||||
| 
 | ||||
| impl BitOr<&BitBoard> for BitBoard { | ||||
|     type Output = BitBoard; | ||||
| 
 | ||||
|     fn bitor(self, rhs: &Self) -> Self { | ||||
|         BitBoard(self.0 | rhs.0) | ||||
|     } | ||||
| } | ||||
| 
 | ||||
| #[cfg(test)] | ||||
| mod tests { | ||||
|     use super::*; | ||||
|  |  | |||
|  | @ -29,23 +29,23 @@ pub struct Position { | |||
| impl Position { | ||||
|     fn empty() -> Position { | ||||
|         Position { | ||||
|             pieces_per_color: [BitBoard(0), BitBoard(0)], | ||||
|             pieces_per_color: [BitBoard::empty(), BitBoard::empty()], | ||||
|             pieces_per_type: [ | ||||
|                 [ | ||||
|                     BitBoard(0), | ||||
|                     BitBoard(0), | ||||
|                     BitBoard(0), | ||||
|                     BitBoard(0), | ||||
|                     BitBoard(0), | ||||
|                     BitBoard(0), | ||||
|                     BitBoard::empty(), | ||||
|                     BitBoard::empty(), | ||||
|                     BitBoard::empty(), | ||||
|                     BitBoard::empty(), | ||||
|                     BitBoard::empty(), | ||||
|                     BitBoard::empty(), | ||||
|                 ], | ||||
|                 [ | ||||
|                     BitBoard(0), | ||||
|                     BitBoard(0), | ||||
|                     BitBoard(0), | ||||
|                     BitBoard(0), | ||||
|                     BitBoard(0), | ||||
|                     BitBoard(0), | ||||
|                     BitBoard::empty(), | ||||
|                     BitBoard::empty(), | ||||
|                     BitBoard::empty(), | ||||
|                     BitBoard::empty(), | ||||
|                     BitBoard::empty(), | ||||
|                     BitBoard::empty(), | ||||
|                 ], | ||||
|             ], | ||||
|         } | ||||
|  | @ -54,27 +54,27 @@ impl Position { | |||
|     /// Return a starting position.
 | ||||
|     fn starting() -> Position { | ||||
|         let white_pieces = [ | ||||
|             BitBoard(0x00FF000000000000), | ||||
|             BitBoard(0x4200000000000000), | ||||
|             BitBoard(0x2400000000000000), | ||||
|             BitBoard(0x8100000000000000), | ||||
|             BitBoard(0x1000000000000000), | ||||
|             BitBoard(0x8000000000000000), | ||||
|             BitBoard::from_bit_field(0x00FF000000000000), | ||||
|             BitBoard::from_bit_field(0x4200000000000000), | ||||
|             BitBoard::from_bit_field(0x2400000000000000), | ||||
|             BitBoard::from_bit_field(0x8100000000000000), | ||||
|             BitBoard::from_bit_field(0x1000000000000000), | ||||
|             BitBoard::from_bit_field(0x8000000000000000), | ||||
|         ]; | ||||
| 
 | ||||
|         let black_pieces = [ | ||||
|             BitBoard(0xFF00), | ||||
|             BitBoard(0x0042), | ||||
|             BitBoard(0x0024), | ||||
|             BitBoard(0x0081), | ||||
|             BitBoard(0x0010), | ||||
|             BitBoard(0x0080), | ||||
|             BitBoard::from_bit_field(0xFF00), | ||||
|             BitBoard::from_bit_field(0x0042), | ||||
|             BitBoard::from_bit_field(0x0024), | ||||
|             BitBoard::from_bit_field(0x0081), | ||||
|             BitBoard::from_bit_field(0x0010), | ||||
|             BitBoard::from_bit_field(0x0080), | ||||
|         ]; | ||||
| 
 | ||||
|         Position { | ||||
|             pieces_per_color: [ | ||||
|                 BitBoard(white_pieces.iter().map(|bb| bb.0).fold(0, |a, b| a | b)), | ||||
|                 BitBoard(black_pieces.iter().map(|bb| bb.0).fold(0, |a, b| a | b)), | ||||
|                 white_pieces.iter().fold(BitBoard::empty(), |a, b| a | b), | ||||
|                 black_pieces.iter().fold(BitBoard::empty(), |a, b| a | b), | ||||
|             ], | ||||
|             pieces_per_type: [white_pieces, black_pieces], | ||||
|         } | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue