Code: Select all
public void QCollectMoves(List<MoveBase> moves)
{
var whitePieces = Board.WhitePieces;
var blackPieces = Board.BlackPieces;
ColorSign colorSign = (ColorSign)Board.CurPlayer;
var curPieces = colorSign == ColorSign.White ? whitePieces : blackPieces;
var opponentPieces = Board.Occupiers & ~curPieces;
var pawns = Board.Pawns;
IField Location = null;
if (colorSign == ColorSign.White)
{
if ((curPieces & pawns & ChessBoard.SEVENTH_ROW_BITMAP) != 0)
{
CPosition.fieldIterator.Reset(curPieces & pawns & ChessBoard.SEVENTH_ROW_BITMAP);
while ((Location = CPosition.fieldIterator.Next(ChessBoard)) != null)
{
Location.AddPromotionsWhitePawn(moves, this);
}
}
}
else if ( (curPieces & pawns & ChessBoard.SECOND_ROW_BITMAP) != 0)
{
CPosition.fieldIterator.Reset(curPieces & pawns & ChessBoard.SECOND_ROW_BITMAP);
while ((Location = CPosition.fieldIterator.Next(ChessBoard)) != null)
{
Location.AddPromotionsBlackPawn(moves, this);
}
}
BitBoardIndex bbIndex = BitBoardIndex.Instance;
UInt64 bits = curPieces;
while (bits != 0)
{
var bit = bits & (~bits + 1);
bits &= bits - 1;
Location = ChessBoard.GetField(bbIndex, bit);
ChessPiece.Sort PieceSort = Location.Occupier.PieceSort;
if ((Location.XRayMoves(PieceSort) & opponentPieces) != 0 )
{
int dir;
switch (PieceSort)
{
case ChessPiece.Sort.whiteKing:
dir = 8;
DirectionMoves allMovesKing = ((Field) Location).dirMoves[(int)ChessPiece.Sort.whiteKing];
while (dir-- > 0)
{
var dirMoves2 = allMovesKing[dir];
int n = dirMoves2.Count;
for (int i = 0; i < n; i++)
{
var move = dirMoves2[i];
ChessPiece capture = move.End.Occupier;
if (capture == null)
{
}
else
{
if (capture.Colour == ColorSign.Black)
{
switch (capture.PieceKind)
{
case ChessPiece.Kind.King:
moves.Clear();
moves.Add(move);
return;
case ChessPiece.Kind.Pawn:
move.Value = ChessPiece.PK_MVVA_VALUE;
break;
case ChessPiece.Kind.Knight:
move.Value = ChessPiece.NK_MVVA_VALUE;
break;
case ChessPiece.Kind.Bishop:
move.Value = ChessPiece.BK_MVVA_VALUE;
break;
case ChessPiece.Kind.Rook:
move.Value = ChessPiece.RK_MVVA_VALUE;
break;
case ChessPiece.Kind.Queen:
move.Value = ChessPiece.QK_MVVA_VALUE;
break;
default:
move.Value = ChessPiece.MVVAPoints(capture.PieceKind) - ChessPiece.KING_VALUE;
moves.Add(move);
break;
}
moves.Add(move);
}
break;
}
}
}
break;
case ChessPiece.Sort.blackKing:
dir = 8;
DirectionMoves allMovesKingB = ((Field)Location).dirMoves[(int)ChessPiece.Sort.blackKing];
while (dir-- > 0)
{
var dirMoves2 = allMovesKingB[dir];
int n = dirMoves2.Count;
for (int i = 0; i < n; i++)
{
var move = dirMoves2[i];
ChessPiece capture = move.End.Occupier;
if (capture == null)
{
}
else
{
if (capture.Colour == ColorSign.White)
{
switch (capture.PieceKind)
{
case ChessPiece.Kind.King:
moves.Clear();
moves.Add(move);
return;
case ChessPiece.Kind.Pawn:
move.Value = ChessPiece.PK_MVVA_VALUE;
break;
case ChessPiece.Kind.Knight:
move.Value = ChessPiece.NK_MVVA_VALUE;
break;
case ChessPiece.Kind.Bishop:
move.Value = ChessPiece.BK_MVVA_VALUE;
break;
case ChessPiece.Kind.Rook:
move.Value = ChessPiece.RK_MVVA_VALUE;
break;
case ChessPiece.Kind.Queen:
move.Value = ChessPiece.QK_MVVA_VALUE;
break;
}
moves.Add(move);
}
break;
}
}
}
break;
case ChessPiece.Sort.whiteKnight:
dir = 8;
DirectionMoves allMovesKnight = ((Field)Location).dirMoves[(int)ChessPiece.Sort.whiteKnight];
while (dir-- > 0)
{
var dirMoves2 = allMovesKnight[dir];
int n = dirMoves2.Count;
for (int i = 0; i < n; i++)
{
var move = dirMoves2[i];
ChessPiece capture = move.End.Occupier;
if (capture == null)
{
}
else
{
if (capture.Colour == ColorSign.Black)
{
switch (capture.PieceKind)
{
case ChessPiece.Kind.King:
moves.Clear();
moves.Add(move);
return;
case ChessPiece.Kind.Pawn:
move.Value = ChessPiece.PN_MVVA_VALUE;
break;
case ChessPiece.Kind.Knight:
move.Value = ChessPiece.NN_MVVA_VALUE;
break;
case ChessPiece.Kind.Bishop:
move.Value = ChessPiece.BN_MVVA_VALUE;
break;
case ChessPiece.Kind.Rook:
move.Value = ChessPiece.RN_MVVA_VALUE;
break;
case ChessPiece.Kind.Queen:
move.Value = ChessPiece.QN_MVVA_VALUE;
break;
}
moves.Add(move);
}
break;
}
}
}
break;
case ChessPiece.Sort.blackKnight:
dir = 8;
DirectionMoves allMovesKnightB = ((Field)Location).dirMoves[(int)ChessPiece.Sort.blackKnight];
while (dir-- > 0)
{
var dirMoves2 = allMovesKnightB[dir];
int n = dirMoves2.Count;
for (int i = 0; i < n; i++)
{
var move = dirMoves2[i];
ChessPiece capture = move.End.Occupier;
if (capture == null)
{
}
else
{
if (capture.Colour == ColorSign.White)
{
switch (capture.PieceKind)
{
case ChessPiece.Kind.King:
moves.Clear();
moves.Add(move);
return;
case ChessPiece.Kind.Pawn:
move.Value = ChessPiece.PN_MVVA_VALUE;
break;
case ChessPiece.Kind.Knight:
move.Value = ChessPiece.NN_MVVA_VALUE;
break;
case ChessPiece.Kind.Bishop:
move.Value = ChessPiece.BN_MVVA_VALUE;
break;
case ChessPiece.Kind.Rook:
move.Value = ChessPiece.RN_MVVA_VALUE;
break;
case ChessPiece.Kind.Queen:
move.Value = ChessPiece.QN_MVVA_VALUE;
break;
}
moves.Add(move);
}
break;
}
}
}
break;
case ChessPiece.Sort.whitePawn:
DirectionMoves allMoves = ((Field)Location).dirMoves[(int)ChessPiece.Sort.whitePawn];
int colNr = Location.ColNr;
if (colNr > ChessBoard.FIRSTCOLUMN)
{
MoveBase move = allMoves[(int)Pawn.Direction.LEFT][0];
Debug.Assert(!(move is EPMove));
ChessPiece capture = move.End.Occupier;
if (capture != null && capture.Colour == ColorSign.Black) // capture
{
switch (capture.PieceKind)
{
case ChessPiece.Kind.King:
moves.Clear();
moves.Add(move);
return;
case ChessPiece.Kind.Pawn:
move.Value = ChessPiece.PP_MVVA_VALUE;
break;
case ChessPiece.Kind.Knight:
move.Value = ChessPiece.NP_MVVA_VALUE;
break;
case ChessPiece.Kind.Bishop:
move.Value = ChessPiece.BP_MVVA_VALUE;
break;
case ChessPiece.Kind.Rook:
move.Value = ChessPiece.RP_MVVA_VALUE;
break;
case ChessPiece.Kind.Queen:
move.Value = ChessPiece.QP_MVVA_VALUE;
break;
}
moves.Add(move);
}
var epMoves = allMoves[(int)Pawn.Direction.EP_LEFT];
if (epMoves.Count > 0)
{
move = epMoves[0];
if (move.CanMove(colorSign, this, Location))
{
Debug.Assert((move is EPMove));
capture = (ChessPiece)move.GetCapture(this);
switch (capture.PieceKind)
{
case ChessPiece.Kind.King:
moves.Clear();
moves.Add(move);
return;
case ChessPiece.Kind.Pawn:
move.Value = ChessPiece.PP_MVVA_VALUE;
break;
case ChessPiece.Kind.Knight:
move.Value = ChessPiece.NP_MVVA_VALUE;
break;
case ChessPiece.Kind.Bishop:
move.Value = ChessPiece.BP_MVVA_VALUE;
break;
case ChessPiece.Kind.Rook:
move.Value = ChessPiece.RP_MVVA_VALUE;
break;
case ChessPiece.Kind.Queen:
move.Value = ChessPiece.QP_MVVA_VALUE;
break;
}
moves.Add(move);
}
}
}
if (colNr < ChessBoard.LASTCOLUMN)
{
MoveBase move = allMoves[(int)Pawn.Direction.RIGHT][0];
Debug.Assert(!(move is EPMove));
ChessPiece capture = move.End.Occupier;
if (capture != null && capture.Colour == ColorSign.Black) // capture
{
switch (capture.PieceKind)
{
case ChessPiece.Kind.King:
moves.Clear();
moves.Add(move);
return;
case ChessPiece.Kind.Pawn:
move.Value = ChessPiece.PP_MVVA_VALUE;
break;
case ChessPiece.Kind.Knight:
move.Value = ChessPiece.NP_MVVA_VALUE;
break;
case ChessPiece.Kind.Bishop:
move.Value = ChessPiece.BP_MVVA_VALUE;
break;
case ChessPiece.Kind.Rook:
move.Value = ChessPiece.RP_MVVA_VALUE;
break;
case ChessPiece.Kind.Queen:
move.Value = ChessPiece.QP_MVVA_VALUE;
break;
}
moves.Add(move);
}
var epMoves = allMoves[(int)Pawn.Direction.EP_RIGHT];
if (epMoves.Count > 0)
{
move = epMoves[0];
if (move.CanMove(colorSign, this, Location))
{
Debug.Assert((move is EPMove));
capture = (ChessPiece)move.GetCapture(this);
switch (capture.PieceKind)
{
case ChessPiece.Kind.King:
moves.Clear();
moves.Add(move);
return;
case ChessPiece.Kind.Pawn:
move.Value = ChessPiece.PP_MVVA_VALUE;
break;
case ChessPiece.Kind.Knight:
move.Value = ChessPiece.NP_MVVA_VALUE;
break;
case ChessPiece.Kind.Bishop:
move.Value = ChessPiece.BP_MVVA_VALUE;
break;
case ChessPiece.Kind.Rook:
move.Value = ChessPiece.RP_MVVA_VALUE;
break;
case ChessPiece.Kind.Queen:
move.Value = ChessPiece.QP_MVVA_VALUE;
break;
}
moves.Add(move);
}
}
}
break;
case ChessPiece.Sort.blackPawn:
DirectionMoves allMovesBP = ((Field)Location).dirMoves[(int)ChessPiece.Sort.blackPawn];
int colNrBP = Location.ColNr;
if (colNrBP > ChessBoard.FIRSTCOLUMN)
{
MoveBase move = allMovesBP[(int)Pawn.Direction.LEFT][0];
Debug.Assert(!(move is EPMove));
ChessPiece occupier = move.End.Occupier;
if (occupier != null && occupier.Colour == ColorSign.White) // capture
{
IPiece capture = move.End.Occupier;
switch (capture.PieceKind)
{
case ChessPiece.Kind.King:
moves.Clear();
moves.Add(move);
return;
case ChessPiece.Kind.Pawn:
move.Value = ChessPiece.PP_MVVA_VALUE;
break;
case ChessPiece.Kind.Knight:
move.Value = ChessPiece.NP_MVVA_VALUE;
break;
case ChessPiece.Kind.Bishop:
move.Value = ChessPiece.BP_MVVA_VALUE;
break;
case ChessPiece.Kind.Rook:
move.Value = ChessPiece.RP_MVVA_VALUE;
break;
case ChessPiece.Kind.Queen:
move.Value = ChessPiece.QP_MVVA_VALUE;
break;
}
moves.Add(move);
}
var epMoves = allMovesBP[(int)Pawn.Direction.EP_LEFT];
if (epMoves.Count > 0)
{
move = epMoves[0];
if (move.CanMove(colorSign, this, Location))
{
Debug.Assert((move is EPMove));
var capture = move.GetCapture(this);
switch (capture.PieceKind)
{
case ChessPiece.Kind.King:
moves.Clear();
moves.Add(move);
return;
case ChessPiece.Kind.Pawn:
move.Value = ChessPiece.PP_MVVA_VALUE;
break;
case ChessPiece.Kind.Knight:
move.Value = ChessPiece.NP_MVVA_VALUE;
break;
case ChessPiece.Kind.Bishop:
move.Value = ChessPiece.BP_MVVA_VALUE;
break;
case ChessPiece.Kind.Rook:
move.Value = ChessPiece.RP_MVVA_VALUE;
break;
case ChessPiece.Kind.Queen:
move.Value = ChessPiece.QP_MVVA_VALUE;
break;
}
moves.Add(move);
}
}
}
if (colNrBP < ChessBoard.LASTCOLUMN)
{
MoveBase move = allMovesBP[(int)Pawn.Direction.RIGHT][0];
Debug.Assert(!(move is EPMove));
ChessPiece occupier = move.End.Occupier;
if (occupier != null && occupier.Colour == ColorSign.White) // capture
{
IPiece capture = move.End.Occupier;
switch (capture.PieceKind)
{
case ChessPiece.Kind.King:
moves.Clear();
moves.Add(move);
return;
case ChessPiece.Kind.Pawn:
move.Value = ChessPiece.PP_MVVA_VALUE;
break;
case ChessPiece.Kind.Knight:
move.Value = ChessPiece.NP_MVVA_VALUE;
break;
case ChessPiece.Kind.Bishop:
move.Value = ChessPiece.BP_MVVA_VALUE;
break;
case ChessPiece.Kind.Rook:
move.Value = ChessPiece.RP_MVVA_VALUE;
break;
case ChessPiece.Kind.Queen:
move.Value = ChessPiece.QP_MVVA_VALUE;
break;
}
moves.Add(move);
}
var epMoves = allMovesBP[(int)Pawn.Direction.EP_RIGHT];
if (epMoves.Count > 0)
{
move = epMoves[0];
if (move.CanMove(colorSign, this, Location))
{
Debug.Assert((move is EPMove));
IPiece capture = move.GetCapture(this);
switch (capture.PieceKind)
{
case ChessPiece.Kind.King:
moves.Clear();
moves.Add(move);
return;
case ChessPiece.Kind.Pawn:
move.Value = ChessPiece.PP_MVVA_VALUE;
break;
case ChessPiece.Kind.Knight:
move.Value = ChessPiece.NP_MVVA_VALUE;
break;
case ChessPiece.Kind.Bishop:
move.Value = ChessPiece.BP_MVVA_VALUE;
break;
case ChessPiece.Kind.Rook:
move.Value = ChessPiece.RP_MVVA_VALUE;
break;
case ChessPiece.Kind.Queen:
move.Value = ChessPiece.QP_MVVA_VALUE;
break;
}
moves.Add(move);
}
}
}
break;
case ChessPiece.Sort.whiteQueen:
dir = 8;
var xrayMoves = ((Field)Location).dirMoves[(int)ChessPiece.Sort.whiteQueen];
var dirMoves = Location.DirMoves(ChessPiece.Sort.whiteQueen);
while (dir-- > 0)
{
if ((dirMoves[dir] & blackPieces) == 0)
{
// all moves in a direction don't hit any opponent pieces
continue;
}
var xrayDirMoves = xrayMoves[dir];
int n = xrayDirMoves.Count;
for (int i = 0; i < n;)
{
var move = xrayDirMoves[i++];
// check each move, there can only be one capture in a direction
ChessPiece capture = move.End.Occupier;
if (capture == null)
{
}
else if (capture.Colour == ColorSign.Black)
{
switch (capture.PieceKind)
{
case ChessPiece.Kind.King:
moves.Clear();
moves.Add(move);
return;
case ChessPiece.Kind.Pawn:
move.Value = ChessPiece.PQ_MVVA_VALUE;
break;
case ChessPiece.Kind.Knight:
move.Value = ChessPiece.NQ_MVVA_VALUE;
break;
case ChessPiece.Kind.Bishop:
move.Value = ChessPiece.BQ_MVVA_VALUE;
break;
case ChessPiece.Kind.Rook:
move.Value = ChessPiece.RQ_MVVA_VALUE;
break;
case ChessPiece.Kind.Queen:
move.Value = ChessPiece.QQ_MVVA_VALUE;
break;
}
moves.Add(move);
break;
}
else break;
}
}
break;
case ChessPiece.Sort.blackQueen:
dir = 8;
var xrayMovesBQ = ((Field)Location).dirMoves[(int)ChessPiece.Sort.blackQueen];
var dirMovesBQ = Location.DirMoves(ChessPiece.Sort.blackQueen);
while (dir-- > 0)
{
if ((dirMovesBQ[dir] & whitePieces) == 0)
{
// all moves in a direction don't hit any opponent pieces
continue;
}
var xrayDirMoves = xrayMovesBQ[dir];
int n = xrayDirMoves.Count;
for (int i = 0; i < n;)
{
var move = xrayDirMoves[i++];
// check each move, there can only be one capture in a direction
ChessPiece capture = move.End.Occupier;
if (capture == null)
{
}
else if (capture.Colour == ColorSign.White)
{
switch (capture.PieceKind)
{
case ChessPiece.Kind.King:
moves.Clear();
moves.Add(move);
return;
case ChessPiece.Kind.Pawn:
move.Value = ChessPiece.PQ_MVVA_VALUE;
break;
case ChessPiece.Kind.Knight:
move.Value = ChessPiece.NQ_MVVA_VALUE;
break;
case ChessPiece.Kind.Bishop:
move.Value = ChessPiece.BQ_MVVA_VALUE;
break;
case ChessPiece.Kind.Rook:
move.Value = ChessPiece.RQ_MVVA_VALUE;
break;
case ChessPiece.Kind.Queen:
move.Value = ChessPiece.QQ_MVVA_VALUE;
break;
}
moves.Add(move);
break;
}
else break;
}
}
break;
case ChessPiece.Sort.whiteRook:
dir = 4;
var xrayMovesR = ((Field)Location).dirMoves[(int)ChessPiece.Sort.whiteRook];
var dirMovesR = Location.DirMoves(ChessPiece.Sort.whiteRook);
while (dir-- > 0)
{
if ((dirMovesR[dir] & blackPieces) == 0)
{
// all moves in a direction don't hit any opponent pieces
continue;
}
var xrayDirMoves = xrayMovesR[dir];
int n = xrayDirMoves.Count;
for (int i = 0; i < n;)
{
var move = xrayDirMoves[i++];
// check each move, there can only be one capture in a direction
ChessPiece capture = move.End.Occupier;
if (capture == null)
{
}
else if (capture.Colour == ColorSign.Black)
{
switch (capture.PieceKind)
{
case ChessPiece.Kind.King:
moves.Clear();
moves.Add(move);
return;
case ChessPiece.Kind.Pawn:
move.Value = ChessPiece.PR_MVVA_VALUE;
break;
case ChessPiece.Kind.Knight:
move.Value = ChessPiece.NR_MVVA_VALUE;
break;
case ChessPiece.Kind.Bishop:
move.Value = ChessPiece.BR_MVVA_VALUE;
break;
case ChessPiece.Kind.Rook:
move.Value = ChessPiece.RR_MVVA_VALUE;
break;
case ChessPiece.Kind.Queen:
move.Value = ChessPiece.QR_MVVA_VALUE;
break;
}
moves.Add(move);
break;
}
else break;
}
}
break;
case ChessPiece.Sort.blackRook:
dir = 4;
var xrayMovesRB = ((Field)Location).dirMoves[(int)ChessPiece.Sort.blackRook];
var dirMovesRB = Location.DirMoves(ChessPiece.Sort.blackRook);
while (dir-- > 0)
{
if ((dirMovesRB[dir] & whitePieces) == 0)
{
// all moves in a direction don't hit any opponent pieces
continue;
}
var xrayDirMoves = xrayMovesRB[dir];
int n = xrayDirMoves.Count;
for (int i = 0; i < n;)
{
var move = xrayDirMoves[i++];
// check each move, there can only be one capture in a direction
ChessPiece capture = move.End.Occupier;
if (capture == null)
{
}
else if (capture.Colour == ColorSign.White)
{
switch (capture.PieceKind)
{
case ChessPiece.Kind.King:
moves.Clear();
moves.Add(move);
return;
case ChessPiece.Kind.Pawn:
move.Value = ChessPiece.PR_MVVA_VALUE;
break;
case ChessPiece.Kind.Knight:
move.Value = ChessPiece.NR_MVVA_VALUE;
break;
case ChessPiece.Kind.Bishop:
move.Value = ChessPiece.BR_MVVA_VALUE;
break;
case ChessPiece.Kind.Rook:
move.Value = ChessPiece.RR_MVVA_VALUE;
break;
case ChessPiece.Kind.Queen:
move.Value = ChessPiece.QR_MVVA_VALUE;
break;
}
moves.Add(move);
break;
}
else break;
}
}
break;
case ChessPiece.Sort.whiteBishop:
dir = 4;
var xrayMovesB = ((Field)Location).dirMoves[(int)ChessPiece.Sort.whiteBishop];
var dirMovesB = Location.DirMoves(ChessPiece.Sort.whiteBishop);
while (dir-- > 0)
{
if ((dirMovesB[dir] & blackPieces) == 0)
{
// all moves in a direction don't hit any opponent pieces
continue;
}
var xrayDirMoves = xrayMovesB[dir];
int n = xrayDirMoves.Count;
for (int i = 0; i < n;)
{
var move = xrayDirMoves[i++];
// check each move, there can only be one capture in a direction
ChessPiece capture = move.End.Occupier;
if (capture == null)
{
}
else if (capture.Colour == ColorSign.Black)
{
switch (capture.PieceKind)
{
case ChessPiece.Kind.King:
moves.Clear();
moves.Add(move);
return;
case ChessPiece.Kind.Pawn:
move.Value = ChessPiece.PB_MVVA_VALUE;
break;
case ChessPiece.Kind.Knight:
move.Value = ChessPiece.NB_MVVA_VALUE;
break;
case ChessPiece.Kind.Bishop:
move.Value = ChessPiece.BB_MVVA_VALUE;
break;
case ChessPiece.Kind.Rook:
move.Value = ChessPiece.RB_MVVA_VALUE;
break;
case ChessPiece.Kind.Queen:
move.Value = ChessPiece.QB_MVVA_VALUE;
break;
}
moves.Add(move);
break;
}
else break;
}
}
break;
case ChessPiece.Sort.blackBishop:
dir = 4;
var xrayMovesBB = ((Field)Location).dirMoves[(int)ChessPiece.Sort.blackBishop];
var dirMovesBB = Location.DirMoves(ChessPiece.Sort.blackBishop);
while (dir-- > 0)
{
if ((dirMovesBB[dir] & whitePieces) == 0)
{
// all moves in a direction don't hit any opponent pieces
continue;
}
var xrayDirMoves = xrayMovesBB[dir];
int n = xrayDirMoves.Count;
for (int i = 0; i < n;)
{
var move = xrayDirMoves[i++];
// check each move, there can only be one capture in a direction
ChessPiece capture = move.End.Occupier;
if (capture == null)
{
}
else if (capture.Colour == ColorSign.White)
{
switch (capture.PieceKind)
{
case ChessPiece.Kind.King:
moves.Clear();
moves.Add(move);
return;
case ChessPiece.Kind.Pawn:
move.Value = ChessPiece.PB_MVVA_VALUE;
break;
case ChessPiece.Kind.Knight:
move.Value = ChessPiece.NB_MVVA_VALUE;
break;
case ChessPiece.Kind.Bishop:
move.Value = ChessPiece.BB_MVVA_VALUE;
break;
case ChessPiece.Kind.Rook:
move.Value = ChessPiece.RB_MVVA_VALUE;
break;
case ChessPiece.Kind.Queen:
move.Value = ChessPiece.QB_MVVA_VALUE;
break;
default:
move.Value = ChessPiece.MVVAPoints(capture.PieceKind);
break;
}
moves.Add(move);
break;
}
else break;
}
}
break;
}
}
}
}