示例#1
0
 private function SetupAttackBitboardsOLD()
 {
     $n64One = new BitBoard('0x1');
     // Generate masks for every tile. Helps to reduce the number of bit shifting operations required.
     $this->Tiles = array();
     for ($nTile = 0; $nTile < 64; $nTile++) {
         $this->Tiles[$nTile] = $n64One->shift_l_($nTile);
     }
     // Start from index 0=A1 and work up to index 63=H8
     $this->KingAttacks = array();
     // The king can move 1 square in any direction. Of course it cannot move off the board :P
     for ($nTile = 0; $nTile < 64; $nTile++) {
         $this->KingAttacks[$nTile] = new BitBoard();
         if ($nTile % 8 < 7) {
             $this->KingAttacks[$nTile]->_OR($this->Tiles[$nTile + 1]);
             // Move right
             if ($this->floor[$nTile] < 7) {
                 $this->KingAttacks[$nTile]->_OR($this->Tiles[$nTile + 9]);
             }
             // Move up
             if ($this->floor[$nTile] > 0) {
                 $this->KingAttacks[$nTile]->_OR($this->Tiles[$nTile - 7]);
             }
             // Move down
         }
         if ($nTile % 8 > 0) {
             $this->KingAttacks[$nTile]->_OR($this->Tiles[$nTile - 1]);
             // Move left.
             if ($this->floor[$nTile] < 7) {
                 $this->KingAttacks[$nTile]->_OR($this->Tiles[$nTile + 7]);
             }
             // Move up
             if ($this->floor[$nTile] > 0) {
                 $this->KingAttacks[$nTile]->_OR($this->Tiles[$nTile - 9]);
             }
             // Move down
         }
         if ($this->floor[$nTile] < 7) {
             $this->KingAttacks[$nTile]->_OR($this->Tiles[$nTile + 8]);
         }
         //Move up
         if ($this->floor[$nTile] > 0) {
             $this->KingAttacks[$nTile]->_OR($this->Tiles[$nTile - 8]);
         }
         //Move down
     }
     // Knight moves.
     $this->KnightAttacks = array();
     for ($nTile = 0; $nTile < 64; $nTile++) {
         $this->KnightAttacks[$nTile] = new BitBoard();
         // Moving up 2 tiles, need 2 tile buffer
         if ($this->floor[$nTile] < 6) {
             if ($nTile % 8 < 7) {
                 $this->KnightAttacks[$nTile]->_OR($this->Tiles[$nTile + 17]);
             }
             // Moved right
             if ($nTile % 8 > 0) {
                 $this->KnightAttacks[$nTile]->_OR($this->Tiles[$nTile + 15]);
             }
             // Moved left
         }
         // Moving up 1 tile, need 1 tile buffer
         if ($this->floor[$nTile] < 7) {
             if ($nTile % 8 < 6) {
                 $this->KnightAttacks[$nTile]->_OR($this->Tiles[$nTile + 10]);
             }
             // Moved right
             if ($nTile % 8 > 1) {
                 $this->KnightAttacks[$nTile]->_OR($this->Tiles[$nTile + 6]);
             }
             // Moved left
         }
         // Moving down 2 tiles, need 2 tile buffer
         if ($this->floor[$nTile] > 1) {
             if ($nTile % 8 < 7) {
                 $this->KnightAttacks[$nTile]->_OR($this->Tiles[$nTile - 15]);
             }
             // Moved right
             if ($nTile % 8 > 0) {
                 $this->KnightAttacks[$nTile]->_OR($this->Tiles[$nTile - 17]);
             }
             // Moved left
         }
         // Moving down 1 tile, need 1 tile buffer
         if ($this->floor[$nTile] > 0) {
             if ($nTile % 8 < 6) {
                 $this->KnightAttacks[$nTile]->_OR($this->Tiles[$nTile - 6]);
             }
             // Moved right
             if ($nTile % 8 > 1) {
                 $this->KnightAttacks[$nTile]->_OR($this->Tiles[$nTile - 10]);
             }
             // Moved left
         }
     }
     // Moves/capture for white pawns. Starting on rank 1 and ending on rank 6
     $this->WPawnAttacks = array();
     $this->WPawnCaptures = array();
     for ($nTile = 0; $nTile < 64; $nTile++) {
         $this->WPawnAttacks[$nTile] = new BitBoard();
         $this->WPawnCaptures[$nTile] = new BitBoard();
         if ($this->floor[$nTile] < 1 || $this->floor[$nTile] > 6) {
             continue;
         }
         if ($this->floor[$nTile] == 1) {
             // Can move forward two tiles
             $this->WPawnAttacks[$nTile]->_OR($this->Tiles[$nTile + 16]);
         }
         $this->WPawnAttacks[$nTile]->_OR($this->Tiles[$nTile + 8]);
         // Move forward one tile.
         if ($nTile % 8 > 0) {
             $this->WPawnCaptures[$nTile]->_OR($this->Tiles[$nTile + 7]);
         }
         // Take piece to the left.
         if ($nTile % 8 < 7) {
             $this->WPawnCaptures[$nTile]->_OR($this->Tiles[$nTile + 9]);
         }
         // Take piece to the right.
     }
     // Moves/captures for black pawns. Going from rank 1 to rank 6
     $this->BPawnAttacks = array();
     $this->BPawnCaptures = array();
     for ($nTile = 0; $nTile < 64; $nTile++) {
         $this->BPawnAttacks[$nTile] = new BitBoard();
         $this->BPawnCaptures[$nTile] = new BitBoard();
         if ($this->floor[$nTile] < 1 || $this->floor[$nTile] > 6) {
             continue;
         }
         if ($this->floor[$nTile] == 6) {
             // Can move backward two tiles
             $this->BPawnAttacks[$nTile]->_OR($this->Tiles[$nTile - 16]);
         }
         $this->BPawnAttacks[$nTile]->_OR($this->Tiles[$nTile - 8]);
         // Move forward one tile.
         if ($nTile % 8 > 0) {
             $this->BPawnCaptures[$nTile]->_OR($this->Tiles[$nTile - 9]);
         }
         // Take piece to the left.
         if ($nTile % 8 < 7) {
             $this->BPawnCaptures[$nTile]->_OR($this->Tiles[$nTile - 7]);
         }
         // Take piece to the right.
     }
     // Moves in straigh lines (vertical, horizontal and diagonal)
     $this->Plus1 = array();
     $this->Plus8 = array();
     $this->Plus9 = array();
     $this->Plus7 = array();
     $this->Minus1 = array();
     $this->Minus7 = array();
     $this->Minus8 = array();
     $this->Minus9 = array();
     for ($nTile = 0; $nTile < 64; $nTile++) {
         $this->Plus1[$nTile] = new BitBoard();
         $this->Plus8[$nTile] = new BitBoard();
         $this->Plus9[$nTile] = new BitBoard();
         $this->Plus7[$nTile] = new BitBoard();
         $this->Minus1[$nTile] = new BitBoard();
         $this->Minus8[$nTile] = new BitBoard();
         // going right
         for ($file = $nTile % 8 + 1; $file < 8; $file++) {
             $this->Plus1[$nTile]->_OR($this->Tiles[$this->floor[$nTile] * 8 + $file]);
         }
         // going left
         for ($file = 0; $file < $nTile % 8; $file++) {
             $this->Minus1[$nTile]->_OR($this->Tiles[$this->floor[$nTile] * 8 + $file]);
         }
         // going up
         for ($rank = $this->floor[$nTile] + 1; $rank < 8; $rank++) {
             $this->Plus8[$nTile]->_OR($this->Tiles[$rank * 8 + $nTile % 8]);
         }
         // going down
         for ($rank = 0; $rank < $this->floor[$nTile]; $rank++) {
             $this->Minus8[$nTile]->_OR($this->Tiles[$rank * 8 + $nTile % 8]);
         }
         // going up right
         for ($rank = $this->floor[$nTile] + 1, $file = $nTile % 8 + 1; $rank < 8 && $file < 8; $rank++, $file++) {
             $this->Plus9[$nTile]->_OR($this->Tiles[$rank * 8 + $file]);
         }
         // going up left
         for ($rank = $this->floor[$nTile] + 1, $file = $nTile % 8 - 1; $rank < 8 && $file > -1; $rank++, $file--) {
             $this->Plus7[$nTile]->_OR($this->Tiles[$rank * 8 + $file]);
         }
     }
     // This time going backwards because it was just easier to come up with a solution.
     for ($i = 0; $i < 64; $i++) {
         $this->Minus7[$i] = new BitBoard();
         $this->Minus9[$i] = new BitBoard();
     }
     for ($nTile = 63; $nTile > -1; $nTile--) {
         $this->Minus7[$nTile] = new BitBoard();
         $this->Minus9[$nTile] = new BitBoard();
         // going down left
         for ($rank = $this->floor[$nTile] - 1, $file = $nTile % 8 - 1; $rank > -1 && $file > -1; $rank--, $file--) {
             $this->Minus9[$nTile]->_OR($this->Tiles[$rank * 8 + $file]);
         }
         // going down right
         for ($rank = $this->floor[$nTile] - 1, $file = $nTile % 8 + 1; $rank > -1 && $file < 8; $rank--, $file++) {
             $this->Minus7[$nTile]->_OR($this->Tiles[$rank * 8 + $file]);
         }
     }
     // Setup other boards.
     // Setup casteling bits. These indicate which tiles need to be free to be able to castle.
     $this->n64WhiteCastleQFree = new BitBoard('0xC');
     // 4 + 8
     $this->n64WhiteCastleKFree = new BitBoard('0x60');
     // 32 + 64
     $this->n64BlackCastleQFree = $this->n64WhiteCastleQFree->shift_l_(56);
     $this->n64BlackCastleKFree = $this->n64WhiteCastleKFree->shift_l_(56);
     // Set bits to mask ranks.
     $this->Ranks = array();
     $mask = new BitBoard('0xFF');
     // Masks a whole rank
     $this->Ranks[0] = $mask;
     $this->Ranks[1] = $mask->shift_l_(8);
     $this->Ranks[2] = $mask->shift_l_(16);
     $this->Ranks[3] = $mask->shift_l_(24);
     $this->Ranks[4] = $mask->shift_l_(32);
     $this->Ranks[5] = $mask->shift_l_(40);
     $this->Ranks[6] = $mask->shift_l_(48);
     $this->Ranks[7] = $mask->shift_l_(56);
     // Will mask the en passant tile.
     $this->n64Enpassant = new BitBoard();
 }