/// <summary> /// Load an opening book into memory. /// </summary> /// <param name="player"> /// The player to play. /// </param> public static void LoadOpeningBook(Player player) { XmlDocument xmldoc = new XmlDocument(); // xmldoc.Load(@"d:\ob6.xml"); xmldoc.Load(@"d:\OpeningBook.xml"); // xmldoc.Load(@"d:\OpeningBook_16plys_146027.xml"); int intScanMove = ScanPly(player, (XmlElement)xmldoc.SelectSingleNode("OpeningBook")); if (intScanMove != 0) { RecordHash(Board.HashCodeA, Board.HashCodeB, (byte)(intScanMove >> 8 & 0xff), (byte)(intScanMove & 0xff), Move.MoveNames.Standard, player.Colour); } }
public static object RenderPiece(SizeF SquareSize, Player.PlayerColourNames player, Piece.PieceNames name) { var size = SquareSize; var scale_w = size.Width / (float) PieceRenderer.Size.Width; var scale_h = size.Height / (float) PieceRenderer.Size.Height; UIGraphics.BeginImageContextWithOptions (size, false, 0); try { using (var graphics = UIGraphics.GetCurrentContext ()) { graphics.ScaleCTM (scale_w, scale_h); PieceRenderer.Render (graphics, player, name); return UIGraphics.GetImageFromCurrentImageContext (); } } finally { UIGraphics.EndImageContext (); } }
public static void Render (RenderTarget c, Player.PlayerColourNames player, Piece.PieceNames name) { target = c; var color = player == Player.PlayerColourNames.White ? White : Black; var main = color; var alternate = player != Player.PlayerColourNames.White ? White : Black; var white = White; var black = Black; switch (name) { case Piece.PieceNames.Bishop: RenderBishop (c, main, alternate, white, black); break; case Piece.PieceNames.King: if (player == Player.PlayerColourNames.White) RenderWhiteKing (c, main, alternate, white, black); else RenderBlackKing (c, main, alternate, white, black); break; case Piece.PieceNames.Knight: RenderKnight (c, player, main, alternate, white, black); break; case Piece.PieceNames.Pawn: RenderPawn (c, main, alternate, white, black); break; case Piece.PieceNames.Queen: if (player == Player.PlayerColourNames.White) RenderWhiteQueen (c, white, black); else RenderBlackQueen (c); break; case Piece.PieceNames.Rook: if (player == Player.PlayerColourNames.White) RenderWhiteRook (c, white, black); else RenderBlackRook (c); break; } }
/// <summary> /// Initializes a new instance of the <see cref="Piece"/> class. /// </summary> /// <param name="name"> /// The piece name e.g. bishop, queen. /// </param> /// <param name="player"> /// The player that owns the piece. /// </param> /// <param name="file"> /// Board file that the piece starts on. /// </param> /// <param name="rank"> /// Board rank that the piece starts on. /// </param> /// <param name="identifier"> /// Piece identifier. /// </param> public Piece(PieceNames name, Player player, int file, int rank, PieceIdentifierCodes identifier) { this.LastMoveTurnNo = -1; this.IsInPlay = true; Square square = Board.GetSquare(file, rank); this.Player = player; this.StartLocation = this.Square = square; square.Piece = this; this.IdentifierCode = identifier; switch (name) { case PieceNames.Pawn: this.Top = new PiecePawn(this); break; case PieceNames.Bishop: this.Top = new PieceBishop(this); break; case PieceNames.Knight: this.Top = new PieceKnight(this); break; case PieceNames.Rook: this.Top = new PieceRook(this); break; case PieceNames.Queen: this.Top = new PieceQueen(this); break; case PieceNames.King: this.Top = new PieceKing(this); break; } }
public static void RenderKnight (RenderTarget c, Player.PlayerColourNames player, Color main, Color alternate, Color white, Color black) { SetFillColor (main); SetStrokeColor (black); MoveTo (22f, 10f); AddCurveToPoint (32.5f, 11f, 38.5f, 18f, 38f, 39f); AddLineToPoint (15f, 39f); AddCurveToPoint (15f, 30f, 25f, 32.5f, 23f, 18f); FillAndStrokePath (); SetFillColor (main); SetStrokeColor (black); MoveTo (24f, 18f); AddCurveToPoint (24.38f, 20.91f, 18.45f, 25.37f, 16f, 27f); AddCurveToPoint (13f, 29f, 13.18f, 31.34f, 11f, 31f); AddCurveToPoint (9.958f, 30.06f, 12.41f, 27.96f, 11f, 28f); AddCurveToPoint (10f, 28f, 11.19f, 29.23f, 10f, 30f); AddCurveToPoint (9f, 30f, 5.997f, 31f, 6f, 26f); AddCurveToPoint (6f, 24f, 12f, 14f, 12f, 14f); AddCurveToPoint (12f, 14f, 13.89f, 12.1f, 14f, 10.5f); AddCurveToPoint (13.27f, 9.506f, 13.5f, 8.5f, 13.5f, 7.5f); AddCurveToPoint (14.5f, 6.5f, 16.5f, 10f, 16.5f, 10f); AddLineToPoint (18.5f, 10f); AddCurveToPoint (18.5f, 10f, 19.28f, 8.008f, 21f, 7f); AddCurveToPoint (22f, 7f, 22f, 10f, 22f, 10f); FillAndStrokePath (); SetFillColor (main); SetStrokeColor (alternate); MoveTo (9.5f, 25.5f); AddCurveToPoint (9.5f, 25.77614f, 9.276142f, 26f, 9f, 26f); AddCurveToPoint (8.723858f, 26f, 8.5f, 25.77614f, 8.5f, 25.5f); AddCurveToPoint (8.5f, 25.22386f, 8.723858f, 25f, 9f, 25f); AddCurveToPoint (9.276142f, 25f, 9.5f, 25.22386f, 9.5f, 25.5f); ClosePath (); MoveTo (9.5f, 25.5f); FillAndStrokePath (); SetMatrix (0.866f, 0.5f, -0.5f, 0.866f, 9.693f, -5.173f); SetFillColor (black); SetStrokeColor (alternate); MoveTo (15f, 15.5f); AddCurveToPoint (15f, 16.32843f, 14.77614f, 17f, 14.5f, 17f); AddCurveToPoint (14.22386f, 17f, 14f, 16.32843f, 14f, 15.5f); AddCurveToPoint (14f, 14.67157f, 14.22386f, 14f, 14.5f, 14f); AddCurveToPoint (14.77614f, 14f, 15f, 14.67157f, 15f, 15.5f); ClosePath (); MoveTo (15f, 15.5f); FillAndStrokePath (); if (player == Player.PlayerColourNames.Black) { SetFillColor (white); MoveTo (24.55f, 10.4f); AddLineToPoint (24.1f, 11.85f); AddLineToPoint (24.6f, 12f); AddCurveToPoint (27.75f, 13f, 30.25f, 14.49f, 32.5f, 18.75f); AddCurveToPoint (34.75f, 23.01f, 35.75f, 29.06f, 35.25f, 39f); AddLineToPoint (35.2f, 39.5f); AddLineToPoint (37.45f, 39.5f); AddLineToPoint (37.5f, 39f); AddCurveToPoint (38f, 28.94f, 36.62f, 22.15f, 34.25f, 17.66f); AddCurveToPoint (31.88f, 13.17f, 28.46f, 11.02f, 25.06f, 10.5f); AddLineToPoint (24.55f, 10.4f); ClosePath (); MoveTo (24.55f, 10.4f); FillPath (); } }
/// <summary> /// The line threatened by. /// </summary> /// <param name="player"> /// The player. /// </param> /// <param name="squares"> /// The squares. /// </param> /// <param name="squareStart"> /// The square start. /// </param> /// <param name="offset"> /// The offset. /// </param> public static void LineThreatenedBy(Player player, Squares squares, Square squareStart, int offset) { int intOrdinal = squareStart.Ordinal; Square square; intOrdinal += offset; while ((square = GetSquare(intOrdinal)) != null) { if (square.Piece == null) { squares.Add(square); } else if (square.Piece.Player.Colour != player.Colour && square.Piece.IsCapturable) { squares.Add(square); break; } else { break; } intOrdinal += offset; } }
/// <summary> /// The append piece path. /// </summary> /// <param name="moves"> /// The moves. /// </param> /// <param name="piece"> /// The piece. /// </param> /// <param name="player"> /// The player. /// </param> /// <param name="offset"> /// The offset. /// </param> /// <param name="movesType"> /// The moves type. /// </param> public static void AppendPiecePath( Moves moves, Piece piece, Player player, int offset, Moves.MoveListNames movesType) { int intOrdinal = piece.Square.Ordinal; Square square; intOrdinal += offset; while ((square = GetSquare(intOrdinal)) != null) { if (square.Piece == null) { if (movesType == Moves.MoveListNames.All) { moves.Add(0, 0, Move.MoveNames.Standard, piece, piece.Square, square, null, 0, 0); } } else if (square.Piece.Player.Colour != player.Colour && square.Piece.IsCapturable) { moves.Add(0, 0, Move.MoveNames.Standard, piece, piece.Square, square, square.Piece, 0, 0); break; } else { break; } intOrdinal += offset; } }
/// <summary> /// Gets the cheapest piece defending this square. /// </summary> /// <param name="player"> /// Defending player who pieces should be listed. /// </param> /// <returns> /// List of pieces. /// </returns> public Piece CheapestPieceDefendingThisSquare(Player player) { Piece piece; Piece pieceBest = null; // Pawn piece = Board.GetPiece(this.Ordinal - player.PawnAttackLeftOffset); if (piece != null && piece.Name == Piece.PieceNames.Pawn && piece.Player.Colour == player.Colour) { return piece; } piece = Board.GetPiece(this.Ordinal - player.PawnAttackRightOffset); if (piece != null && piece.Name == Piece.PieceNames.Pawn && piece.Player.Colour == player.Colour) { return piece; } // Knight piece = Board.GetPiece(this.Ordinal + 33); if (piece != null && piece.Name == Piece.PieceNames.Knight && piece.Player.Colour == player.Colour) { return piece; } piece = Board.GetPiece(this.Ordinal + 18); if (piece != null && piece.Name == Piece.PieceNames.Knight && piece.Player.Colour == player.Colour) { return piece; } piece = Board.GetPiece(this.Ordinal - 14); if (piece != null && piece.Name == Piece.PieceNames.Knight && piece.Player.Colour == player.Colour) { return piece; } piece = Board.GetPiece(this.Ordinal - 31); if (piece != null && piece.Name == Piece.PieceNames.Knight && piece.Player.Colour == player.Colour) { return piece; } piece = Board.GetPiece(this.Ordinal - 33); if (piece != null && piece.Name == Piece.PieceNames.Knight && piece.Player.Colour == player.Colour) { return piece; } piece = Board.GetPiece(this.Ordinal - 18); if (piece != null && piece.Name == Piece.PieceNames.Knight && piece.Player.Colour == player.Colour) { return piece; } piece = Board.GetPiece(this.Ordinal + 14); if (piece != null && piece.Name == Piece.PieceNames.Knight && piece.Player.Colour == player.Colour) { return piece; } piece = Board.GetPiece(this.Ordinal + 31); if (piece != null && piece.Name == Piece.PieceNames.Knight && piece.Player.Colour == player.Colour) { return piece; } // Bishop & Queen piece = Board.LinesFirstPiece(player.Colour, Piece.PieceNames.Bishop, this, 15); if (piece != null) { switch (piece.Name) { case Piece.PieceNames.Bishop: return piece; case Piece.PieceNames.Queen: pieceBest = piece; break; } } piece = Board.LinesFirstPiece(player.Colour, Piece.PieceNames.Bishop, this, 17); if (piece != null) { switch (piece.Name) { case Piece.PieceNames.Bishop: return piece; case Piece.PieceNames.Queen: pieceBest = piece; break; } } piece = Board.LinesFirstPiece(player.Colour, Piece.PieceNames.Bishop, this, -15); if (piece != null) { switch (piece.Name) { case Piece.PieceNames.Bishop: return piece; case Piece.PieceNames.Queen: pieceBest = piece; break; } } piece = Board.LinesFirstPiece(player.Colour, Piece.PieceNames.Bishop, this, -17); if (piece != null) { switch (piece.Name) { case Piece.PieceNames.Bishop: return piece; case Piece.PieceNames.Queen: pieceBest = piece; break; } } // Rook & Queen piece = Board.LinesFirstPiece(player.Colour, Piece.PieceNames.Rook, this, 1); if (piece != null) { switch (piece.Name) { case Piece.PieceNames.Rook: return piece; case Piece.PieceNames.Queen: pieceBest = piece; break; } } piece = Board.LinesFirstPiece(player.Colour, Piece.PieceNames.Rook, this, -1); if (piece != null) { switch (piece.Name) { case Piece.PieceNames.Rook: return piece; case Piece.PieceNames.Queen: pieceBest = piece; break; } } piece = Board.LinesFirstPiece(player.Colour, Piece.PieceNames.Rook, this, 16); if (piece != null) { switch (piece.Name) { case Piece.PieceNames.Rook: return piece; case Piece.PieceNames.Queen: pieceBest = piece; break; } } piece = Board.LinesFirstPiece(player.Colour, Piece.PieceNames.Rook, this, -16); if (piece != null) { switch (piece.Name) { case Piece.PieceNames.Rook: return piece; case Piece.PieceNames.Queen: pieceBest = piece; break; } } if (pieceBest != null) { return pieceBest; // This means a queen was found, but not a Bishop or Rook } // King! piece = Board.GetPiece(this.Ordinal + 16); if (piece != null && piece.Name == Piece.PieceNames.King && piece.Player.Colour == player.Colour) { return piece; } piece = Board.GetPiece(this.Ordinal + 17); if (piece != null && piece.Name == Piece.PieceNames.King && piece.Player.Colour == player.Colour) { return piece; } piece = Board.GetPiece(this.Ordinal + 1); if (piece != null && piece.Name == Piece.PieceNames.King && piece.Player.Colour == player.Colour) { return piece; } piece = Board.GetPiece(this.Ordinal - 15); if (piece != null && piece.Name == Piece.PieceNames.King && piece.Player.Colour == player.Colour) { return piece; } piece = Board.GetPiece(this.Ordinal - 16); if (piece != null && piece.Name == Piece.PieceNames.King && piece.Player.Colour == player.Colour) { return piece; } piece = Board.GetPiece(this.Ordinal - 17); if (piece != null && piece.Name == Piece.PieceNames.King && piece.Player.Colour == player.Colour) { return piece; } piece = Board.GetPiece(this.Ordinal - 1); if (piece != null && piece.Name == Piece.PieceNames.King && piece.Player.Colour == player.Colour) { return piece; } piece = Board.GetPiece(this.Ordinal + 15); if (piece != null && piece.Name == Piece.PieceNames.King && piece.Player.Colour == player.Colour) { return piece; } return null; }
/// <summary> /// Returns a list of player's pieces attacking this square. /// </summary> /// <param name="player"> /// Player who owns the attacking pieces that you want to find. /// </param> /// <returns> /// List of pieces. /// </returns> public Pieces PlayersPiecesAttackingThisSquare(Player player) { Piece piece; Pieces pieces = new Pieces(); // Pawn piece = Board.GetPiece(this.Ordinal - player.PawnAttackLeftOffset); if (piece != null && piece.Name == Piece.PieceNames.Pawn && piece.Player.Colour == player.Colour) { pieces.Add(piece); } piece = Board.GetPiece(this.Ordinal - player.PawnAttackRightOffset); if (piece != null && piece.Name == Piece.PieceNames.Pawn && piece.Player.Colour == player.Colour) { pieces.Add(piece); } // Knight piece = Board.GetPiece(this.Ordinal + 33); if (piece != null && piece.Name == Piece.PieceNames.Knight && piece.Player.Colour == player.Colour) { pieces.Add(piece); } piece = Board.GetPiece(this.Ordinal + 18); if (piece != null && piece.Name == Piece.PieceNames.Knight && piece.Player.Colour == player.Colour) { pieces.Add(piece); } piece = Board.GetPiece(this.Ordinal - 14); if (piece != null && piece.Name == Piece.PieceNames.Knight && piece.Player.Colour == player.Colour) { pieces.Add(piece); } piece = Board.GetPiece(this.Ordinal - 31); if (piece != null && piece.Name == Piece.PieceNames.Knight && piece.Player.Colour == player.Colour) { pieces.Add(piece); } piece = Board.GetPiece(this.Ordinal - 33); if (piece != null && piece.Name == Piece.PieceNames.Knight && piece.Player.Colour == player.Colour) { pieces.Add(piece); } piece = Board.GetPiece(this.Ordinal - 18); if (piece != null && piece.Name == Piece.PieceNames.Knight && piece.Player.Colour == player.Colour) { pieces.Add(piece); } piece = Board.GetPiece(this.Ordinal + 14); if (piece != null && piece.Name == Piece.PieceNames.Knight && piece.Player.Colour == player.Colour) { pieces.Add(piece); } piece = Board.GetPiece(this.Ordinal + 31); if (piece != null && piece.Name == Piece.PieceNames.Knight && piece.Player.Colour == player.Colour) { pieces.Add(piece); } // Bishop & Queen if ((piece = Board.LinesFirstPiece(player.Colour, Piece.PieceNames.Bishop, this, 15)) != null) { pieces.Add(piece); } if ((piece = Board.LinesFirstPiece(player.Colour, Piece.PieceNames.Bishop, this, 17)) != null) { pieces.Add(piece); } if ((piece = Board.LinesFirstPiece(player.Colour, Piece.PieceNames.Bishop, this, -15)) != null) { pieces.Add(piece); } if ((piece = Board.LinesFirstPiece(player.Colour, Piece.PieceNames.Bishop, this, -17)) != null) { pieces.Add(piece); } // Rook & Queen if ((piece = Board.LinesFirstPiece(player.Colour, Piece.PieceNames.Rook, this, 1)) != null) { pieces.Add(piece); } if ((piece = Board.LinesFirstPiece(player.Colour, Piece.PieceNames.Rook, this, -1)) != null) { pieces.Add(piece); } if ((piece = Board.LinesFirstPiece(player.Colour, Piece.PieceNames.Rook, this, 16)) != null) { pieces.Add(piece); } if ((piece = Board.LinesFirstPiece(player.Colour, Piece.PieceNames.Rook, this, -16)) != null) { pieces.Add(piece); } // King! piece = Board.GetPiece(this.Ordinal + 16); if (piece != null && piece.Name == Piece.PieceNames.King && piece.Player.Colour == player.Colour) { pieces.Add(piece); } piece = Board.GetPiece(this.Ordinal + 17); if (piece != null && piece.Name == Piece.PieceNames.King && piece.Player.Colour == player.Colour) { pieces.Add(piece); } piece = Board.GetPiece(this.Ordinal + 1); if (piece != null && piece.Name == Piece.PieceNames.King && piece.Player.Colour == player.Colour) { pieces.Add(piece); } piece = Board.GetPiece(this.Ordinal - 15); if (piece != null && piece.Name == Piece.PieceNames.King && piece.Player.Colour == player.Colour) { pieces.Add(piece); } piece = Board.GetPiece(this.Ordinal - 16); if (piece != null && piece.Name == Piece.PieceNames.King && piece.Player.Colour == player.Colour) { pieces.Add(piece); } piece = Board.GetPiece(this.Ordinal - 17); if (piece != null && piece.Name == Piece.PieceNames.King && piece.Player.Colour == player.Colour) { pieces.Add(piece); } piece = Board.GetPiece(this.Ordinal - 1); if (piece != null && piece.Name == Piece.PieceNames.King && piece.Player.Colour == player.Colour) { pieces.Add(piece); } piece = Board.GetPiece(this.Ordinal + 15); if (piece != null && piece.Name == Piece.PieceNames.King && piece.Player.Colour == player.Colour) { pieces.Add(piece); } return pieces; }
/// <summary> /// Record a hash new hash entry in the hash table. /// </summary> /// <param name="hashCodeA"> /// Hash Code for Board position A /// </param> /// <param name="hashCodeB"> /// Hash Code for Board position B /// </param> /// <param name="from"> /// From square ordinal. /// </param> /// <param name="to"> /// To square ordinal. /// </param> /// <param name="moveName"> /// The move name. /// </param> /// <param name="colour"> /// The player colour. /// </param> private static unsafe void RecordHash(ulong hashCodeA, ulong hashCodeB, byte from, byte to, Move.MoveNames moveName, Player.PlayerColourNames colour) { if (colour == Player.PlayerColourNames.Black) { hashCodeA |= 0x1; hashCodeB |= 0x1; } else { hashCodeA &= 0xFFFFFFFFFFFFFFFE; hashCodeB &= 0xFFFFFFFFFFFFFFFE; } fixed (HashEntry* phashBase = &HashTableEntries[0]) { HashEntry* phashEntry = phashBase; phashEntry += (uint)(hashCodeA % HashTableSize); phashEntry->HashCodeA = hashCodeA; phashEntry->HashCodeB = hashCodeB; phashEntry->From = from; phashEntry->To = to; phashEntry->MoveName = moveName; } }
/// <summary> /// The probe opening book (hash table) for best move for the specied board position (hash code). /// </summary> /// <param name="hashCodeA"> /// The hash code for board position A. /// </param> /// <param name="hashCodeB"> /// The hash code for board position B. /// </param> /// <param name="colour"> /// The player colour. /// </param> /// <returns> /// The best move in the opening book (hash table) or null if there is no opening book entry for the specified board position. /// </returns> private static unsafe Move ProbeForBestMove(ulong hashCodeA, ulong hashCodeB, Player.PlayerColourNames colour) { if (colour == Player.PlayerColourNames.Black) { hashCodeA |= 0x1; hashCodeB |= 0x1; } else { hashCodeA &= 0xFFFFFFFFFFFFFFFE; hashCodeB &= 0xFFFFFFFFFFFFFFFE; } fixed (HashEntry* phashBase = &HashTableEntries[0]) { HashEntry* phashEntry = phashBase; phashEntry += (uint)(hashCodeA % HashTableSize); if (phashEntry->HashCodeA == hashCodeA && phashEntry->HashCodeB == hashCodeB) { return new Move(0, 0, phashEntry->MoveName, Board.GetPiece(phashEntry->From), Board.GetSquare(phashEntry->From), Board.GetSquare(phashEntry->To), null, 0, 0); } } return NotFoundInHashTable; }
/// <summary> /// The search for best move in opening book. /// </summary> /// <param name="boardHashCodeA"> /// The board hash code a. /// </param> /// <param name="boardHashCodeB"> /// The board hash code b. /// </param> /// <param name="colour"> /// The colour. /// </param> /// <returns> /// The best move from the opening book. /// </returns> public static Move SearchForGoodMove(ulong boardHashCodeA, ulong boardHashCodeB, Player.PlayerColourNames colour) { return ProbeForBestMove(boardHashCodeA, boardHashCodeB, colour); }
static public bool DoesPieceAttackSquare(Square square, Player player, out Piece attackingPiece) { attackingPiece = null; Piece piece; piece = Board.GetPiece(square.Ordinal - player.PawnAttackLeftOffset); if (piece != null && piece.Name == _pieceType && piece.Player.Colour == player.Colour) { attackingPiece = piece; return true; } piece = Board.GetPiece(square.Ordinal - player.PawnAttackRightOffset); if (piece != null && piece.Name == _pieceType && piece.Player.Colour == player.Colour) { attackingPiece = piece; return true; } return false; }
public static bool DoesPieceAttackSquare(Square square, Player player, out Piece attackingPiece) { return Piece.DoesLeaperPieceTypeAttackSquare(square, player, _pieceType, moveVectors, out attackingPiece); }
public static bool CanPlayerPieceNameAttackSquare(Square square, Player player, Piece.PieceNames PieceName, out Piece attackingPiece) { attackingPiece = null; switch (PieceName) { case PieceNames.Bishop: return PieceBishop.DoesPieceAttackSquare(square, player,out attackingPiece); case PieceNames.King: return PieceKing.DoesPieceAttackSquare(square, player, out attackingPiece); case PieceNames.Knight: return PieceKnight.DoesPieceAttackSquare(square, player, out attackingPiece); case PieceNames.Pawn: return PiecePawn.DoesPieceAttackSquare(square, player, out attackingPiece); case PieceNames.Queen: return PieceQueen.DoesPieceAttackSquare(square, player, out attackingPiece); case PieceNames.Rook: return PieceRook.DoesPieceAttackSquare(square, player, out attackingPiece); } return false; }
/// <summary> /// Load opening book (hash table ) /// </summary> /// <param name="player"> /// The player. /// </param> /// <param name="xmlnodeParent"> /// The xmlnode parent. /// </param> /// <returns> /// The move score. /// </returns> private static int ScanPly(Player player, XmlElement xmlnodeParent) { int intReturnScore = 0; int intReturnMove = 0; foreach (XmlElement xmlnodeMove in xmlnodeParent.ChildNodes) { Move.MoveNames movename = xmlnodeMove.GetAttribute("N") == null ? Move.MoveNames.Standard : Move.MoveNameFromString(xmlnodeMove.GetAttribute("N")); Square from = Board.GetSquare(xmlnodeMove.GetAttribute("F")); Square to = Board.GetSquare(xmlnodeMove.GetAttribute("T")); Piece piece = from.Piece; int intScore = Convert.ToInt32(xmlnodeMove.GetAttribute(player.Colour == Player.PlayerColourNames.White ? "W" : "B")); if (intScore > intReturnScore) { intReturnScore = intScore; intReturnMove = from.Ordinal << 8 | to.Ordinal; } Move moveUndo = piece.Move(movename, to); int intScanMove = ScanPly(player.OpposingPlayer, xmlnodeMove); if (intScanMove != 0) { RecordHash(Board.HashCodeA, Board.HashCodeB, (byte)(intScanMove >> 8 & 0xff), (byte)(intScanMove & 0xff), movename, player.OpposingPlayer.Colour); } Move.Undo(moveUndo); } return intReturnMove; }
public static bool DoesLeaperPieceTypeAttackSquare(Square square, Player player, PieceNames pieceName, int[] vector, out Piece attackingPiece) { Piece piece; attackingPiece = null; for (int i = 0; i < vector.Length; i++) { piece = Board.GetPiece(square.Ordinal + vector[i]); if (piece != null && piece.Name == pieceName && piece.Player.Colour == player.Colour) { attackingPiece = piece; return true; } } return false; }
/// <summary> /// Appends a list of moves of all the pieces that are attacking this square. /// </summary> /// <param name="moves"> /// Moves of pieces that are attacking this square. /// </param> /// <param name="player"> /// Player whose turn it is /// </param> public void AttackersMoveList(Moves moves, Player player) { foreach (Piece p in player.Pieces) { if (p.CanAttackSquare(this)) moves.Add(0, 0, Move.MoveNames.Standard, p, p.Square, this, this.Piece, 0, 0); } }
/// <summary> /// Determines whether the specified player can attack this square. /// </summary> /// <param name="player"> /// The player being tested. /// </param> /// <returns> /// True if player can move a piece to this square. /// </returns> public bool PlayerCanAttackSquare(Player player) { Piece piece; // Pawn piece = Board.GetPiece(this.Ordinal - player.PawnAttackLeftOffset); if (piece != null && piece.Name == Piece.PieceNames.Pawn && piece.Player.Colour == player.Colour) { return true; } piece = Board.GetPiece(this.Ordinal - player.PawnAttackRightOffset); if (piece != null && piece.Name == Piece.PieceNames.Pawn && piece.Player.Colour == player.Colour) { return true; } // Knight piece = Board.GetPiece(this.Ordinal + 33); if (piece != null && piece.Name == Piece.PieceNames.Knight && piece.Player.Colour == player.Colour) { return true; } piece = Board.GetPiece(this.Ordinal + 18); if (piece != null && piece.Name == Piece.PieceNames.Knight && piece.Player.Colour == player.Colour) { return true; } piece = Board.GetPiece(this.Ordinal - 14); if (piece != null && piece.Name == Piece.PieceNames.Knight && piece.Player.Colour == player.Colour) { return true; } piece = Board.GetPiece(this.Ordinal - 31); if (piece != null && piece.Name == Piece.PieceNames.Knight && piece.Player.Colour == player.Colour) { return true; } piece = Board.GetPiece(this.Ordinal - 33); if (piece != null && piece.Name == Piece.PieceNames.Knight && piece.Player.Colour == player.Colour) { return true; } piece = Board.GetPiece(this.Ordinal - 18); if (piece != null && piece.Name == Piece.PieceNames.Knight && piece.Player.Colour == player.Colour) { return true; } piece = Board.GetPiece(this.Ordinal + 14); if (piece != null && piece.Name == Piece.PieceNames.Knight && piece.Player.Colour == player.Colour) { return true; } piece = Board.GetPiece(this.Ordinal + 31); if (piece != null && piece.Name == Piece.PieceNames.Knight && piece.Player.Colour == player.Colour) { return true; } // Bishop & Queen if (Board.LinesFirstPiece(player.Colour, Piece.PieceNames.Bishop, this, 15) != null) { return true; } if (Board.LinesFirstPiece(player.Colour, Piece.PieceNames.Bishop, this, 17) != null) { return true; } if (Board.LinesFirstPiece(player.Colour, Piece.PieceNames.Bishop, this, -15) != null) { return true; } if (Board.LinesFirstPiece(player.Colour, Piece.PieceNames.Bishop, this, -17) != null) { return true; } // Rook & Queen if (Board.LinesFirstPiece(player.Colour, Piece.PieceNames.Rook, this, 1) != null) { return true; } if (Board.LinesFirstPiece(player.Colour, Piece.PieceNames.Rook, this, -1) != null) { return true; } if (Board.LinesFirstPiece(player.Colour, Piece.PieceNames.Rook, this, 16) != null) { return true; } if (Board.LinesFirstPiece(player.Colour, Piece.PieceNames.Rook, this, -16) != null) { return true; } // King! piece = Board.GetPiece(this.Ordinal + 16); if (piece != null && piece.Name == Piece.PieceNames.King && piece.Player.Colour == player.Colour) { return true; } piece = Board.GetPiece(this.Ordinal + 17); if (piece != null && piece.Name == Piece.PieceNames.King && piece.Player.Colour == player.Colour) { return true; } piece = Board.GetPiece(this.Ordinal + 1); if (piece != null && piece.Name == Piece.PieceNames.King && piece.Player.Colour == player.Colour) { return true; } piece = Board.GetPiece(this.Ordinal - 15); if (piece != null && piece.Name == Piece.PieceNames.King && piece.Player.Colour == player.Colour) { return true; } piece = Board.GetPiece(this.Ordinal - 16); if (piece != null && piece.Name == Piece.PieceNames.King && piece.Player.Colour == player.Colour) { return true; } piece = Board.GetPiece(this.Ordinal - 17); if (piece != null && piece.Name == Piece.PieceNames.King && piece.Player.Colour == player.Colour) { return true; } piece = Board.GetPiece(this.Ordinal - 1); if (piece != null && piece.Name == Piece.PieceNames.King && piece.Player.Colour == player.Colour) { return true; } piece = Board.GetPiece(this.Ordinal + 15); if (piece != null && piece.Name == Piece.PieceNames.King && piece.Player.Colour == player.Colour) { return true; } return false; }
/// <summary> /// Returns a list of player's pieces attacking this square. /// </summary> /// <param name="player"> /// Player who owns the attacking pieces that you want to find. /// </param> /// <returns> /// List of pieces. /// </returns> public Pieces PlayersPiecesAttackingThisSquare(Player player) { Pieces pieces = new Pieces(); foreach (Piece p in player.Pieces) { if (p.CanAttackSquare(this)) pieces.Add(p); } return pieces; }
/// <summary> /// Appends a list of moves of all the pieces that are attacking this square. /// </summary> /// <param name="moves"> /// Moves of pieces that are attacking this square. /// </param> /// <param name="player"> /// Player whose turn it is /// </param> public void AttackersMoveList(Moves moves, Player player) { Piece piece; // Pawn piece = Board.GetPiece(this.Ordinal - player.PawnAttackLeftOffset); if (piece != null && piece.Name == Piece.PieceNames.Pawn && piece.Player.Colour == player.Colour) { moves.Add( 0, 0, Move.MoveNames.Standard, Board.GetPiece(this.Ordinal - player.PawnAttackLeftOffset), Board.GetSquare(this.Ordinal - player.PawnAttackLeftOffset), this, this.Piece, 0, 0); } piece = Board.GetPiece(this.Ordinal - player.PawnAttackRightOffset); if (piece != null && piece.Name == Piece.PieceNames.Pawn && piece.Player.Colour == player.Colour) { moves.Add( 0, 0, Move.MoveNames.Standard, Board.GetPiece(this.Ordinal - player.PawnAttackRightOffset), Board.GetSquare(this.Ordinal - player.PawnAttackRightOffset), this, this.Piece, 0, 0); } // Knight piece = Board.GetPiece(this.Ordinal + 33); if (piece != null && piece.Name == Piece.PieceNames.Knight && piece.Player.Colour == player.Colour) { moves.Add(0, 0, Move.MoveNames.Standard, piece, piece.Square, this, this.Piece, 0, 0); } piece = Board.GetPiece(this.Ordinal + 18); if (piece != null && piece.Name == Piece.PieceNames.Knight && piece.Player.Colour == player.Colour) { moves.Add(0, 0, Move.MoveNames.Standard, piece, piece.Square, this, this.Piece, 0, 0); } piece = Board.GetPiece(this.Ordinal - 14); if (piece != null && piece.Name == Piece.PieceNames.Knight && piece.Player.Colour == player.Colour) { moves.Add(0, 0, Move.MoveNames.Standard, piece, piece.Square, this, this.Piece, 0, 0); } piece = Board.GetPiece(this.Ordinal - 31); if (piece != null && piece.Name == Piece.PieceNames.Knight && piece.Player.Colour == player.Colour) { moves.Add(0, 0, Move.MoveNames.Standard, piece, piece.Square, this, this.Piece, 0, 0); } piece = Board.GetPiece(this.Ordinal - 33); if (piece != null && piece.Name == Piece.PieceNames.Knight && piece.Player.Colour == player.Colour) { moves.Add(0, 0, Move.MoveNames.Standard, piece, piece.Square, this, this.Piece, 0, 0); } piece = Board.GetPiece(this.Ordinal - 18); if (piece != null && piece.Name == Piece.PieceNames.Knight && piece.Player.Colour == player.Colour) { moves.Add(0, 0, Move.MoveNames.Standard, piece, piece.Square, this, this.Piece, 0, 0); } piece = Board.GetPiece(this.Ordinal + 14); if (piece != null && piece.Name == Piece.PieceNames.Knight && piece.Player.Colour == player.Colour) { moves.Add(0, 0, Move.MoveNames.Standard, piece, piece.Square, this, this.Piece, 0, 0); } piece = Board.GetPiece(this.Ordinal + 31); if (piece != null && piece.Name == Piece.PieceNames.Knight && piece.Player.Colour == player.Colour) { moves.Add(0, 0, Move.MoveNames.Standard, piece, piece.Square, this, this.Piece, 0, 0); } // Bishop & Queen if ((piece = Board.LinesFirstPiece(player.Colour, Piece.PieceNames.Bishop, this, 15)) != null) { moves.Add(0, 0, Move.MoveNames.Standard, piece, piece.Square, this, this.Piece, 0, 0); } if ((piece = Board.LinesFirstPiece(player.Colour, Piece.PieceNames.Bishop, this, 17)) != null) { moves.Add(0, 0, Move.MoveNames.Standard, piece, piece.Square, this, this.Piece, 0, 0); } if ((piece = Board.LinesFirstPiece(player.Colour, Piece.PieceNames.Bishop, this, -15)) != null) { moves.Add(0, 0, Move.MoveNames.Standard, piece, piece.Square, this, this.Piece, 0, 0); } if ((piece = Board.LinesFirstPiece(player.Colour, Piece.PieceNames.Bishop, this, -17)) != null) { moves.Add(0, 0, Move.MoveNames.Standard, piece, piece.Square, this, this.Piece, 0, 0); } // Rook & Queen if ((piece = Board.LinesFirstPiece(player.Colour, Piece.PieceNames.Rook, this, 1)) != null) { moves.Add(0, 0, Move.MoveNames.Standard, piece, piece.Square, this, this.Piece, 0, 0); } if ((piece = Board.LinesFirstPiece(player.Colour, Piece.PieceNames.Rook, this, -1)) != null) { moves.Add(0, 0, Move.MoveNames.Standard, piece, piece.Square, this, this.Piece, 0, 0); } if ((piece = Board.LinesFirstPiece(player.Colour, Piece.PieceNames.Rook, this, 16)) != null) { moves.Add(0, 0, Move.MoveNames.Standard, piece, piece.Square, this, this.Piece, 0, 0); } if ((piece = Board.LinesFirstPiece(player.Colour, Piece.PieceNames.Rook, this, -16)) != null) { moves.Add(0, 0, Move.MoveNames.Standard, piece, piece.Square, this, this.Piece, 0, 0); } // King! piece = Board.GetPiece(this.Ordinal + 16); if (piece != null && piece.Name == Piece.PieceNames.King && piece.Player.Colour == player.Colour) { moves.Add(0, 0, Move.MoveNames.Standard, piece, piece.Square, this, this.Piece, 0, 0); } piece = Board.GetPiece(this.Ordinal + 17); if (piece != null && piece.Name == Piece.PieceNames.King && piece.Player.Colour == player.Colour) { moves.Add(0, 0, Move.MoveNames.Standard, piece, piece.Square, this, this.Piece, 0, 0); } piece = Board.GetPiece(this.Ordinal + 1); if (piece != null && piece.Name == Piece.PieceNames.King && piece.Player.Colour == player.Colour) { moves.Add(0, 0, Move.MoveNames.Standard, piece, piece.Square, this, this.Piece, 0, 0); } piece = Board.GetPiece(this.Ordinal - 15); if (piece != null && piece.Name == Piece.PieceNames.King && piece.Player.Colour == player.Colour) { moves.Add(0, 0, Move.MoveNames.Standard, piece, piece.Square, this, this.Piece, 0, 0); } piece = Board.GetPiece(this.Ordinal - 16); if (piece != null && piece.Name == Piece.PieceNames.King && piece.Player.Colour == player.Colour) { moves.Add(0, 0, Move.MoveNames.Standard, piece, piece.Square, this, this.Piece, 0, 0); } piece = Board.GetPiece(this.Ordinal - 17); if (piece != null && piece.Name == Piece.PieceNames.King && piece.Player.Colour == player.Colour) { moves.Add(0, 0, Move.MoveNames.Standard, piece, piece.Square, this, this.Piece, 0, 0); } piece = Board.GetPiece(this.Ordinal - 1); if (piece != null && piece.Name == Piece.PieceNames.King && piece.Player.Colour == player.Colour) { moves.Add(0, 0, Move.MoveNames.Standard, piece, piece.Square, this, this.Piece, 0, 0); } piece = Board.GetPiece(this.Ordinal + 15); if (piece != null && piece.Name == Piece.PieceNames.King && piece.Player.Colour == player.Colour) { moves.Add(0, 0, Move.MoveNames.Standard, piece, piece.Square, this, this.Piece, 0, 0); } }
/// <summary> /// Determines whether the specified player can attack this square. /// </summary> /// <param name="player"> /// The player being tested. ( /// </param> /// <returns> /// True if player can move a piece to this square. /// </returns> public bool PlayerCanAttackSquare(Player player) { foreach (Piece.PieceNames pieceName in player.PieceTypes()) { if (Piece.CanPlayerPieceNameAttackSquare(this, player, pieceName)) return true; } return false; }
/// <summary> /// Calculates defense points for the player on this square. Returns the value of the cheapest piece defending the square. /// If no pieces are defending, then returns a high value (15,000). /// </summary> /// <param name="player"> /// The defending player. /// </param> /// <returns> /// Defense points. /// </returns> public int DefencePointsForPlayer(Player player) { Piece piece; int value = 0; int bestValue = 0; // Pawn piece = Board.GetPiece(this.Ordinal - player.PawnAttackLeftOffset); if (piece != null && piece.Name == Piece.PieceNames.Pawn && piece.Player.Colour == player.Colour) { return piece.Value; } piece = Board.GetPiece(this.Ordinal - player.PawnAttackRightOffset); if (piece != null && piece.Name == Piece.PieceNames.Pawn && piece.Player.Colour == player.Colour) { return piece.Value; } // Knight piece = Board.GetPiece(this.Ordinal + 33); if (piece != null && piece.Name == Piece.PieceNames.Knight && piece.Player.Colour == player.Colour) { return piece.Value; } piece = Board.GetPiece(this.Ordinal + 18); if (piece != null && piece.Name == Piece.PieceNames.Knight && piece.Player.Colour == player.Colour) { return piece.Value; } piece = Board.GetPiece(this.Ordinal - 14); if (piece != null && piece.Name == Piece.PieceNames.Knight && piece.Player.Colour == player.Colour) { return piece.Value; } piece = Board.GetPiece(this.Ordinal - 31); if (piece != null && piece.Name == Piece.PieceNames.Knight && piece.Player.Colour == player.Colour) { return piece.Value; } piece = Board.GetPiece(this.Ordinal - 33); if (piece != null && piece.Name == Piece.PieceNames.Knight && piece.Player.Colour == player.Colour) { return piece.Value; } piece = Board.GetPiece(this.Ordinal - 18); if (piece != null && piece.Name == Piece.PieceNames.Knight && piece.Player.Colour == player.Colour) { return piece.Value; } piece = Board.GetPiece(this.Ordinal + 14); if (piece != null && piece.Name == Piece.PieceNames.Knight && piece.Player.Colour == player.Colour) { return piece.Value; } piece = Board.GetPiece(this.Ordinal + 31); if (piece != null && piece.Name == Piece.PieceNames.Knight && piece.Player.Colour == player.Colour) { return piece.Value; } // Bishop & Queen piece = Board.LinesFirstPiece(player.Colour, Piece.PieceNames.Bishop, this, 15); value = piece != null ? piece.Value : 0; if (value > 0 && value < 9000) { return value; } if (value > 0) { bestValue = value; } piece = Board.LinesFirstPiece(player.Colour, Piece.PieceNames.Bishop, this, 17); value = piece != null ? piece.Value : 0; if (value > 0 && value < 9000) { return value; } if (value > 0) { bestValue = value; } piece = Board.LinesFirstPiece(player.Colour, Piece.PieceNames.Bishop, this, -15); value = piece != null ? piece.Value : 0; if (value > 0 && value < 9000) { return value; } if (value > 0) { bestValue = value; } piece = Board.LinesFirstPiece(player.Colour, Piece.PieceNames.Bishop, this, -17); value = piece != null ? piece.Value : 0; if (value > 0 && value < 9000) { return value; } if (value > 0) { bestValue = value; } // Rook & Queen piece = Board.LinesFirstPiece(player.Colour, Piece.PieceNames.Rook, this, 1); value = piece != null ? piece.Value : 0; if (value > 0 && value < 9000) { return value; } if (value > 0) { bestValue = value; } piece = Board.LinesFirstPiece(player.Colour, Piece.PieceNames.Rook, this, -1); value = piece != null ? piece.Value : 0; if (value > 0 && value < 9000) { return value; } if (value > 0) { bestValue = value; } piece = Board.LinesFirstPiece(player.Colour, Piece.PieceNames.Rook, this, 16); value = piece != null ? piece.Value : 0; if (value > 0 && value < 9000) { return value; } if (value > 0) { bestValue = value; } piece = Board.LinesFirstPiece(player.Colour, Piece.PieceNames.Rook, this, -16); value = piece != null ? piece.Value : 0; if (value > 0 && value < 9000) { return value; } if (value > 0) { bestValue = value; } if (bestValue > 0) { return bestValue; // This means a queen was found, but not a Bishop or Rook } // King! piece = Board.GetPiece(this.Ordinal + 16); if (piece != null && piece.Name == Piece.PieceNames.King && piece.Player.Colour == player.Colour) { return piece.Value; } piece = Board.GetPiece(this.Ordinal + 17); if (piece != null && piece.Name == Piece.PieceNames.King && piece.Player.Colour == player.Colour) { return piece.Value; } piece = Board.GetPiece(this.Ordinal + 1); if (piece != null && piece.Name == Piece.PieceNames.King && piece.Player.Colour == player.Colour) { return piece.Value; } piece = Board.GetPiece(this.Ordinal - 15); if (piece != null && piece.Name == Piece.PieceNames.King && piece.Player.Colour == player.Colour) { return piece.Value; } piece = Board.GetPiece(this.Ordinal - 16); if (piece != null && piece.Name == Piece.PieceNames.King && piece.Player.Colour == player.Colour) { return piece.Value; } piece = Board.GetPiece(this.Ordinal - 17); if (piece != null && piece.Name == Piece.PieceNames.King && piece.Player.Colour == player.Colour) { return piece.Value; } piece = Board.GetPiece(this.Ordinal - 1); if (piece != null && piece.Name == Piece.PieceNames.King && piece.Player.Colour == player.Colour) { return piece.Value; } piece = Board.GetPiece(this.Ordinal + 15); if (piece != null && piece.Name == Piece.PieceNames.King && piece.Player.Colour == player.Colour) { return piece.Value; } return 15000; }
/// <summary> /// Calculates defense points for the player on this square. Returns the value of the cheapest piece defending the square. /// If no pieces are defending, then returns a high value (15,000). /// </summary> /// <param name="player"> /// The defending player. /// </param> /// <returns> /// Defense points. /// </returns> public int DefencePointsForPlayer(Player player) { Piece piece; foreach (Piece.PieceNames pieceName in player.PieceTypes()) { if (Piece.CanPlayerPieceNameAttackSquare(this, player, pieceName, out piece)) return piece.Value; } return 15000; }
/// <summary> /// The lines first piece. /// </summary> /// <param name="colour"> /// The colour. /// </param> /// <param name="pieceName"> /// The piece name. /// </param> /// <param name="squareStart"> /// The square start. /// </param> /// <param name="offset"> /// The offset. /// </param> /// <returns> /// The first piece on the line, or null. /// </returns> public static Piece LinesFirstPiece( Player.PlayerColourNames colour, Piece.PieceNames pieceName, Square squareStart, int offset) { int intOrdinal = squareStart.Ordinal; Square square; intOrdinal += offset; while ((square = GetSquare(intOrdinal)) != null) { if (square.Piece == null) { } else if (square.Piece.Player.Colour != colour) { return null; } else if (square.Piece.Name == pieceName || square.Piece.Name == Piece.PieceNames.Queen) { return square.Piece; } else { return null; } intOrdinal += offset; } return null; }
/// <summary> /// Gets the cheapest piece defending this square. /// </summary> /// <param name="player"> /// Defending player who pieces should be listed. /// </param> /// <returns> /// List of pieces. /// </returns> public Piece CheapestPieceDefendingThisSquare(Player player) { Piece piece; foreach (Piece.PieceNames pieceName in player.PieceTypes()) { if (Piece.CanPlayerPieceNameAttackSquare(this, player, pieceName, out piece)) return piece; } return null; }
/// <summary> /// Calculates a positional penalty score for a single open line to a square (usually the king square), in a specified direction. /// </summary> /// <param name="colour"> /// The player's colour. /// </param> /// <param name="squareStart"> /// The square piece (king) is on. /// </param> /// <param name="directionOffset"> /// The direction offset. /// </param> /// <returns> /// The open line penalty. /// </returns> public static int OpenLinePenalty(Player.PlayerColourNames colour, Square squareStart, int directionOffset) { int intOrdinal = squareStart.Ordinal; int intSquareCount = 0; int intPenalty = 0; Square square; intOrdinal += directionOffset; while (intSquareCount <= 2 && ((square = GetSquare(intOrdinal)) != null && (square.Piece == null || (square.Piece.Name != Piece.PieceNames.Pawn && square.Piece.Name != Piece.PieceNames.Rook) || square.Piece.Player.Colour != colour))) { intPenalty += 75; intSquareCount++; intOrdinal += directionOffset; } return intPenalty; }
/// <summary> /// static method to determine if a square is attacked by this piece /// </summary> /// <param name="square"></param> /// <param name="player"></param> /// <returns></returns> /// static public bool DoesPieceAttackSquare(Square square, Player player) { return Piece.DoesLeaperPieceTypeAttackSquare(square, player, _pieceType, moveVectors); }