Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: ec70b585d6
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 271 lines (213 sloc) 6.777 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
#include "weak.h"

Set
NewBlackSet()
{
  Set ret;

  ret.Occupancy = InitBlackOccupancy;
  ret.EmptySquares = ~InitBlackOccupancy;

  ret.Boards[Pawn] = InitBlackPawns;
  ret.Boards[Rook] = InitBlackRooks;
  ret.Boards[Knight] = InitBlackKnights;
  ret.Boards[Bishop] = InitBlackBishops;
  ret.Boards[Queen] = InitBlackQueens;
  ret.Boards[King] = InitBlackKing;

  return ret;
}

ChessSet
NewChessSet()
{
  int i;
  ChessSet ret;
  Piece piece;
  Position pos;
  Side side;

  ret.Sets[White] = NewWhiteSet();
  ret.Sets[Black] = NewBlackSet();
  ret.Occupancy = InitOccupancy;
  ret.EmptySquares = ~InitOccupancy;

  for(pos = A1; pos <= H8; pos++) {
    ret.Squares[pos] = MissingPiece;
    // TODO: This shouldn't matter.
    ret.PiecePositionIndexes[pos] = 0;
  }

  // Clear down piece locations.
  for(side = White; side <= Black; side++) {
    // We ignore king here. King locations stored in CheckStats.
    for(piece = Pawn; piece < King; piece++) {
      for(i = 0; i < MAX_PIECE_LOCATION; i++) {
        ret.PiecePositions[side][piece][i] = EmptyPosition;
      }
    }
  }

  for(side = White; side <= Black; side++) {
    ret.PieceCounts[side][Pawn] = 8;
    ret.PieceCounts[side][Rook] = 2;
    ret.PieceCounts[side][Knight] = 2;
    ret.PieceCounts[side][Bishop] = 2;
    ret.PieceCounts[side][Queen] = 1;
    ret.PieceCounts[side][King] = 1;
  }

  for(pos = A3; pos <= H6; pos++) {
    ret.Squares[pos] = MissingPiece;
    ret.PiecePositionIndexes[pos] = 0;
  }

  for(pos = A2, i=0; pos <= H2; pos++, i++) {
    ret.PiecePositions[White][Pawn][i] = pos;
    ret.PiecePositionIndexes[pos] = i;
    ret.Squares[pos] = Pawn;
  }
  for(pos = A7, i=0; pos <= H7; pos++, i++) {
    ret.PiecePositions[Black][Pawn][i] = pos;
    ret.PiecePositionIndexes[pos] = i;
    ret.Squares[pos] = Pawn;
  }

  ret.Squares[A1] = Rook;
  ret.PiecePositions[White][Rook][0] = A1;
  ret.PiecePositionIndexes[A1] = 0;

  ret.Squares[H1] = Rook;
  ret.PiecePositions[White][Rook][1] = H1;
  ret.PiecePositionIndexes[H1] = 1;

  ret.Squares[A8] = Rook;
  ret.PiecePositions[Black][Rook][0] = A8;
  ret.PiecePositionIndexes[A8] = 0;

  ret.Squares[H8] = Rook;
  ret.PiecePositions[Black][Rook][1] = H8;
  ret.PiecePositionIndexes[H8] = 1;

  ret.Squares[B1] = Knight;
  ret.PiecePositions[White][Knight][0] = B1;
  ret.PiecePositionIndexes[B1] = 0;

  ret.Squares[G1] = Knight;
  ret.PiecePositions[White][Knight][1] = G1;
  ret.PiecePositionIndexes[G1] = 1;

  ret.Squares[B8] = Knight;
  ret.PiecePositions[Black][Knight][0] = B8;
  ret.PiecePositionIndexes[B8] = 0;

  ret.Squares[G8] = Knight;
  ret.PiecePositions[Black][Knight][1] = G8;
  ret.PiecePositionIndexes[G8] = 1;

  ret.Squares[C1] = Bishop;
  ret.PiecePositions[White][Bishop][0] = C1;
  ret.PiecePositionIndexes[C1] = 0;

  ret.Squares[F1] = Bishop;
  ret.PiecePositions[White][Bishop][1] = F1;
  ret.PiecePositionIndexes[F1] = 1;

  ret.Squares[C8] = Bishop;
  ret.PiecePositions[Black][Bishop][0] = C8;
  ret.PiecePositionIndexes[C8] = 0;

  ret.Squares[F8] = Bishop;
  ret.PiecePositions[Black][Bishop][1] = F8;
  ret.PiecePositionIndexes[F8] = 1;

  ret.Squares[D1] = Queen;
  ret.PiecePositions[White][Queen][0] = D1;
  ret.PiecePositionIndexes[D1] = 0;

  ret.Squares[D8] = Queen;
  ret.PiecePositions[Black][Queen][0] = D8;
  ret.PiecePositionIndexes[D8] = 0;

  ret.Squares[E1] = King;
  ret.PiecePositions[White][King][0] = E1;
  ret.PiecePositionIndexes[E1] = 0;

  ret.Squares[E8] = King;
  ret.PiecePositions[White][King][0] = E8;
  ret.PiecePositionIndexes[E8] = 0;

  UpdateOccupancies(&ret);

  return ret;
}

ChessSet
NewEmptyChessSet()
{
  ChessSet ret;
  int i;
  Piece piece;
  Position pos;
  Side side;

  ret.Sets[White] = NewEmptySet();
  ret.Sets[Black] = NewEmptySet();
  ret.Occupancy = EmptyBoard;
  ret.EmptySquares = FullyOccupied;

  for(pos = A1; pos <= H8; pos++) {
    ret.Squares[pos] = MissingPiece;
    // TODO: This shouldn't matter.
    ret.PiecePositionIndexes[pos] = 0;
  }

  for(side = White; side <= Black; side++) {
    // We ignore king here. King locations stored in CheckStats.
    for(piece = Pawn; piece <= King; piece++) {
      ret.PieceCounts[side][piece] = 0;
      for(i = 0; i < MAX_PIECE_LOCATION; i++) {
        ret.PiecePositions[side][piece][i] = EmptyPosition;
      }
    }
  }

  UpdateOccupancies(&ret);

  return ret;
}

Set
NewEmptySet()
{
  Piece piece;
  Set ret;

  for(piece = Pawn; piece <= King; piece++) {
    ret.Boards[piece] = EmptyBoard;
  }

  ret.Occupancy = EmptyBoard;
  ret.EmptySquares = FullyOccupied;

  return ret;
}

Set
NewWhiteSet()
{
  Set ret;

  ret.Occupancy = InitWhiteOccupancy;
  ret.EmptySquares = ~InitWhiteOccupancy;

  ret.Boards[Pawn] = InitWhitePawns;
  ret.Boards[Rook] = InitWhiteRooks;
  ret.Boards[Knight] = InitWhiteKnights;
  ret.Boards[Bishop] = InitWhiteBishops;
  ret.Boards[Queen] = InitWhiteQueens;
  ret.Boards[King] = InitWhiteKing;

  return ret;
}

BitBoard
PinnedPieces(ChessSet *chessSet, Side side, Position king, bool pinned)
{
  BitBoard attackers, bitBoard;
  BitBoard ret = EmptyBoard;
  Side opposite = OPPOSITE(side);
  Side pinner = pinned ? opposite : side;
  Position attacker;

  assert(chessSet->Sets[side].Boards[King] != EmptyBoard);

  // If we consider opponents attacks *from* the king's square, this is equivalent to
  // positions in which the pieces in question can attack *to* the king.

  attackers = (chessSet->Sets[pinner].Boards[Rook] |
               chessSet->Sets[pinner].Boards[Queen]) &
    EmptyAttacks[Rook][king];

  attackers |= (chessSet->Sets[pinner].Boards[Bishop] |
               chessSet->Sets[pinner].Boards[Queen]) &
    EmptyAttacks[Bishop][king];

  while(attackers != EmptyBoard) {
    attacker = PopForward(&attackers);

    bitBoard = Between[king][attacker] & chessSet->Occupancy;

    if(bitBoard != EmptyBoard && SingleBit(bitBoard) &&
       (bitBoard & chessSet->Sets[side].Occupancy) != EmptyBoard) {
      ret |= bitBoard;
    }
  }

  return ret;
}

void
UpdateOccupancies(ChessSet *chessSet)
{
  Piece piece;

  chessSet->Sets[White].Occupancy = EmptyBoard;
  chessSet->Sets[Black].Occupancy = EmptyBoard;
  for(piece = Pawn; piece <= King; piece++) {
    chessSet->Sets[White].Occupancy |= chessSet->Sets[White].Boards[piece];
    chessSet->Sets[Black].Occupancy |= chessSet->Sets[Black].Boards[piece];

    chessSet->PieceOccupancy[piece] = chessSet->Sets[White].Boards[piece] |
      chessSet->Sets[Black].Boards[piece];
  }

  chessSet->Sets[White].EmptySquares = ~chessSet->Sets[White].Occupancy;
  chessSet->Sets[Black].EmptySquares = ~chessSet->Sets[Black].Occupancy;

  chessSet->Occupancy = chessSet->Sets[White].Occupancy | chessSet->Sets[Black].Occupancy;
  chessSet->EmptySquares = ~chessSet->Occupancy;
}
Something went wrong with that request. Please try again.