Example #1
0
 /// <summary>
 ///   Initializes static members of the <see cref = "Board" /> class.
 /// </summary>
 static Board()
 {
     for (int intOrdinal = 0; intOrdinal < SquareCount; intOrdinal++)
     {
         Squares[intOrdinal] = new Square(intOrdinal);
     }
 }
Example #2
0
 /// <summary>
 /// Initializes a new instance of the <see cref="Move"/> class.
 /// </summary>
 /// <param name="turnNo">
 /// The turn no.
 /// </param>
 /// <param name="lastMoveTurnNo">
 /// The last move turn no.
 /// </param>
 /// <param name="moveName">
 /// The move name.
 /// </param>
 /// <param name="piece">
 /// The piece moving.
 /// </param>
 /// <param name="from">
 /// The square the peice is moving from.
 /// </param>
 /// <param name="to">
 /// The square the peice is moving to.
 /// </param>
 /// <param name="pieceCaptured">
 /// The piece being captured.
 /// </param>
 /// <param name="pieceCapturedOrdinal">
 /// Ordinal position of the piece being captured.
 /// </param>
 /// <param name="score">
 /// The positional score.
 /// </param>
 public Move(int turnNo, int lastMoveTurnNo, MoveNames moveName, Piece piece, Square from, Square to, Piece pieceCaptured, int pieceCapturedOrdinal, int score)
 {
     this.EnemyStatus = Player.PlayerStatusNames.Normal;
     this.TurnNo = turnNo;
     this.LastMoveTurnNo = lastMoveTurnNo;
     this.Name = moveName;
     this.Piece = piece;
     this.From = from;
     this.To = to;
     this.PieceCaptured = pieceCaptured;
     this.PieceCapturedOrdinal = pieceCapturedOrdinal;
     this.Score = score;
     if (moveName != MoveNames.NullMove && pieceCaptured == null && piece != null && piece.Name != Piece.PieceNames.Pawn)
     {
         this.FiftyMoveDrawCounter = Game.MoveHistory.Count > 0 ? Game.MoveHistory.Last.FiftyMoveDrawCounter + 1 : (Game.FiftyMoveDrawBase / 2) + 1;
     }
 }
Example #3
0
 /// <summary>
 /// Remove the square from the list.
 /// </summary>
 /// <param name="square">
 /// The piece to remove.
 /// </param>
 public void Remove(Square square)
 {
     this.squareList.Remove(square);
 }
Example #4
0
        /// <summary>
        /// Move the piece to a new square.
        /// </summary>
        /// <param name="moveName">
        /// The move name.
        /// </param>
        /// <param name="square">
        /// The square.
        /// </param>
        /// <returns>
        /// Move made.
        /// </returns>
        public Move Move(Move.MoveNames moveName, Square square)
        {
            Square squarepieceCaptured = square;

            if (moveName == Model.Move.MoveNames.EnPassent)
            {
                // Override when en passent
                squarepieceCaptured = Board.GetSquare(square.Ordinal - this.Player.PawnForwardOffset);
            }

            Board.HashCodeA ^= this.HashCodeA; // Un-XOR current piece position
            Board.HashCodeB ^= this.HashCodeB; // Un-XOR current piece position
            if (this.Name == PieceNames.Pawn)
            {
                Board.PawnHashCodeA ^= this.HashCodeA;
                Board.PawnHashCodeB ^= this.HashCodeB;
            }

            Move move = new Move(
                Game.TurnNo,
                this.LastMoveTurnNo,
                moveName,
                this,
                this.Square,
                square,
                squarepieceCaptured.Piece,
                squarepieceCaptured.Piece == null ? -1 : squarepieceCaptured.Piece.Player.Pieces.IndexOf(squarepieceCaptured.Piece),
                0);

            if (square.Piece != null)
            {
                if (squarepieceCaptured.Piece != null)
                {
                    Board.HashCodeA ^= squarepieceCaptured.Piece.HashCodeA; // un-XOR the piece taken
                    Board.HashCodeB ^= squarepieceCaptured.Piece.HashCodeB; // un-XOR the piece taken
                    if (squarepieceCaptured.Piece.Name == PieceNames.Pawn)
                    {
                        Board.PawnHashCodeA ^= squarepieceCaptured.Piece.HashCodeA;
                        Board.PawnHashCodeB ^= squarepieceCaptured.Piece.HashCodeB;
                    }

                    squarepieceCaptured.Piece.Capture();
                }
            }

            Game.TurnNo++;

            this.Square.Piece = null;
            square.Piece      = this;
            this.Square       = square;

            this.LastMoveTurnNo = Game.TurnNo;
            this.NoOfMoves++;

            Piece pieceRook;

            switch (moveName)
            {
            case Model.Move.MoveNames.CastleKingSide:
                pieceRook = move.Piece.Player.Colour == Player.PlayerColourNames.White
                                    ? Board.GetPiece(7, 0)
                                    : Board.GetPiece(7, 7);
                Board.HashCodeA         ^= pieceRook.HashCodeA;
                Board.HashCodeB         ^= pieceRook.HashCodeB;
                pieceRook.Square.Piece   = null;
                pieceRook.LastMoveTurnNo = Game.TurnNo;
                pieceRook.NoOfMoves++;
                Board.GetSquare(5, square.Rank).Piece = pieceRook;
                pieceRook.Square       = Board.GetSquare(5, square.Rank);
                Board.HashCodeA       ^= pieceRook.HashCodeA;
                Board.HashCodeB       ^= pieceRook.HashCodeB;
                this.Player.HasCastled = true;
                break;

            case Model.Move.MoveNames.CastleQueenSide:
                pieceRook = move.Piece.Player.Colour == Player.PlayerColourNames.White
                                    ? Board.GetPiece(0, 0)
                                    : Board.GetPiece(0, 7);
                Board.HashCodeA         ^= pieceRook.HashCodeA;
                Board.HashCodeB         ^= pieceRook.HashCodeB;
                pieceRook.Square.Piece   = null;
                pieceRook.LastMoveTurnNo = Game.TurnNo;
                pieceRook.NoOfMoves++;
                Board.GetSquare(3, square.Rank).Piece = pieceRook;
                pieceRook.Square       = Board.GetSquare(3, square.Rank);
                Board.HashCodeA       ^= pieceRook.HashCodeA;
                Board.HashCodeB       ^= pieceRook.HashCodeB;
                this.Player.HasCastled = true;
                break;

            case Model.Move.MoveNames.PawnPromotionQueen:
                this.Promote(PieceNames.Queen);
                break;

            case Model.Move.MoveNames.PawnPromotionRook:
                this.Promote(PieceNames.Rook);
                break;

            case Model.Move.MoveNames.PawnPromotionBishop:
                this.Promote(PieceNames.Bishop);
                break;

            case Model.Move.MoveNames.PawnPromotionKnight:
                this.Promote(PieceNames.Knight);
                break;

            case Model.Move.MoveNames.EnPassent:
                Board.HashCodeA     ^= Board.GetPiece(this.Square.Ordinal - this.Player.PawnForwardOffset).HashCodeA;
                Board.HashCodeB     ^= Board.GetPiece(this.Square.Ordinal - this.Player.PawnForwardOffset).HashCodeB;
                Board.PawnHashCodeA ^= Board.GetPiece(this.Square.Ordinal - this.Player.PawnForwardOffset).HashCodeA;
                Board.PawnHashCodeB ^= Board.GetPiece(this.Square.Ordinal - this.Player.PawnForwardOffset).HashCodeB;
                Board.GetPiece(this.Square.Ordinal - this.Player.PawnForwardOffset).Capture();

                // Take enemy pawn that is now behind us
                break;
            }

            Board.HashCodeA ^= this.HashCodeA; // XOR piece into new piece position
            Board.HashCodeB ^= this.HashCodeB; // XOR piece into new piece position
            if (this.Name == PieceNames.Pawn)
            {
                Board.PawnHashCodeA ^= this.HashCodeA;
                Board.PawnHashCodeB ^= this.HashCodeB;
            }

            move.IsInCheck      = move.Piece.Player.IsInCheck;
            move.IsEnemyInCheck = move.Piece.Player.OpposingPlayer.IsInCheck;

            move.HashCodeA = Board.HashCodeA;
            move.HashCodeB = Board.HashCodeB;

            Game.MoveHistory.Add(move);

            if (move.Piece.Player.CanClaimThreeMoveRepetitionDraw)
            {
                Board.HashCodeA           ^= 31;
                Board.HashCodeB           ^= 29;
                move.HashCodeA             = Board.HashCodeA;
                move.HashCodeB             = Board.HashCodeB;
                move.IsThreeMoveRepetition = true;
            }

            return(move);
        }
Example #5
0
        /// <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;
            }
        }
Example #6
0
 /// <summary>
 /// The add.
 /// </summary>
 /// <param name="turnNo">
 /// The turn no.
 /// </param>
 /// <param name="lastMoveTurnNo">
 /// The last move turn no.
 /// </param>
 /// <param name="moveName">
 /// The move name.
 /// </param>
 /// <param name="piece">
 /// The piece moving.
 /// </param>
 /// <param name="from">
 /// The square the peice is moving from.
 /// </param>
 /// <param name="to">
 /// The square the peice is moving to.
 /// </param>
 /// <param name="pieceCaptured">
 /// The piece being captured.
 /// </param>
 /// <param name="pieceCapturedOrdinal">
 /// Ordinal position of the piece being captured.
 /// </param>
 /// <param name="score">
 /// The positional score.
 /// </param>
 public void Add(int turnNo, int lastMoveTurnNo, Move.MoveNames moveName, Piece piece, Square from, Square to, Piece pieceCaptured, int pieceCapturedOrdinal, int score)
 {
     this.moves.Add(new Move(turnNo, lastMoveTurnNo, moveName, piece, from, to, pieceCaptured, pieceCapturedOrdinal, score));
 }
Example #7
0
        /// <summary>
        /// Move the piece to a new square.
        /// </summary>
        /// <param name="moveName">
        /// The move name.
        /// </param>
        /// <param name="square">
        /// The square.
        /// </param>
        /// <returns>
        /// Move made.
        /// </returns>
        public Move Move(Move.MoveNames moveName, Square square)
        {
            Square squarepieceCaptured = square;

            if (moveName == Model.Move.MoveNames.EnPassent)
            {
                // Override when en passent
                squarepieceCaptured = Board.GetSquare(square.Ordinal - this.Player.PawnForwardOffset);
            }

            Board.HashCodeA ^= this.HashCodeA; // Un-XOR current piece position
            Board.HashCodeB ^= this.HashCodeB; // Un-XOR current piece position
            if (this.Name == PieceNames.Pawn)
            {
                Board.PawnHashCodeA ^= this.HashCodeA;
                Board.PawnHashCodeB ^= this.HashCodeB;
            }

            Move move = new Move(
                Game.TurnNo,
                this.LastMoveTurnNo,
                moveName,
                this,
                this.Square,
                square,
                squarepieceCaptured.Piece,
                squarepieceCaptured.Piece == null ? -1 : squarepieceCaptured.Piece.Player.Pieces.IndexOf(squarepieceCaptured.Piece),
                0);

            if (square.Piece != null)
            {
                if (squarepieceCaptured.Piece != null)
                {
                    Board.HashCodeA ^= squarepieceCaptured.Piece.HashCodeA; // un-XOR the piece taken
                    Board.HashCodeB ^= squarepieceCaptured.Piece.HashCodeB; // un-XOR the piece taken
                    if (squarepieceCaptured.Piece.Name == PieceNames.Pawn)
                    {
                        Board.PawnHashCodeA ^= squarepieceCaptured.Piece.HashCodeA;
                        Board.PawnHashCodeB ^= squarepieceCaptured.Piece.HashCodeB;
                    }
                    //TODO: aCapturinf Pice Here. -> SIL
                    squarepieceCaptured.Piece.Capture();
                }
            }

            Game.TurnNo++;

            this.Square.Piece = null;
            square.Piece = this;
            this.Square = square;

            this.LastMoveTurnNo = Game.TurnNo;
            this.NoOfMoves++;

            Piece pieceRook;
            switch (moveName)
            {
                case Model.Move.MoveNames.CastleKingSide:
                    pieceRook = move.Piece.Player.Colour == Player.PlayerColourNames.White
                                    ? Board.GetPiece(7, 0)
                                    : Board.GetPiece(7, 7);
                    Board.HashCodeA ^= pieceRook.HashCodeA;
                    Board.HashCodeB ^= pieceRook.HashCodeB;
                    pieceRook.Square.Piece = null;
                    pieceRook.LastMoveTurnNo = Game.TurnNo;
                    pieceRook.NoOfMoves++;
                    Board.GetSquare(5, square.Rank).Piece = pieceRook;
                    pieceRook.Square = Board.GetSquare(5, square.Rank);
                    Board.HashCodeA ^= pieceRook.HashCodeA;
                    Board.HashCodeB ^= pieceRook.HashCodeB;
                    this.Player.HasCastled = true;
                    break;

                case Model.Move.MoveNames.CastleQueenSide:
                    pieceRook = move.Piece.Player.Colour == Player.PlayerColourNames.White
                                    ? Board.GetPiece(0, 0)
                                    : Board.GetPiece(0, 7);
                    Board.HashCodeA ^= pieceRook.HashCodeA;
                    Board.HashCodeB ^= pieceRook.HashCodeB;
                    pieceRook.Square.Piece = null;
                    pieceRook.LastMoveTurnNo = Game.TurnNo;
                    pieceRook.NoOfMoves++;
                    Board.GetSquare(3, square.Rank).Piece = pieceRook;
                    pieceRook.Square = Board.GetSquare(3, square.Rank);
                    Board.HashCodeA ^= pieceRook.HashCodeA;
                    Board.HashCodeB ^= pieceRook.HashCodeB;
                    this.Player.HasCastled = true;
                    break;

                case Model.Move.MoveNames.PawnPromotionQueen: {
                    this.Promote(PieceNames.Queen);
                    break;
                }

                case Model.Move.MoveNames.PawnPromotionRook: {
                    this.Promote(PieceNames.Rook);
                    break;
                }

                case Model.Move.MoveNames.PawnPromotionBishop: {
                    this.Promote(PieceNames.Bishop);
                    break;
                }

                case Model.Move.MoveNames.PawnPromotionKnight: {
                    this.Promote(PieceNames.Knight);
                    break;
                }
                case Model.Move.MoveNames.EnPassent: {
                    Board.HashCodeA ^= Board.GetPiece(this.Square.Ordinal - this.Player.PawnForwardOffset).HashCodeA;
                    Board.HashCodeB ^= Board.GetPiece(this.Square.Ordinal - this.Player.PawnForwardOffset).HashCodeB;
                    Board.PawnHashCodeA ^= Board.GetPiece(this.Square.Ordinal - this.Player.PawnForwardOffset).HashCodeA;
                    Board.PawnHashCodeB ^= Board.GetPiece(this.Square.Ordinal - this.Player.PawnForwardOffset).HashCodeB;
                    Board.GetPiece(this.Square.Ordinal - this.Player.PawnForwardOffset).Capture();

                        // Take enemy pawn that is now behind us
                    break;
            }
            }

            Board.HashCodeA ^= this.HashCodeA; // XOR piece into new piece position
            Board.HashCodeB ^= this.HashCodeB; // XOR piece into new piece position
            if (this.Name == PieceNames.Pawn)
            {
                Board.PawnHashCodeA ^= this.HashCodeA;
                Board.PawnHashCodeB ^= this.HashCodeB;
            }

            move.IsInCheck = move.Piece.Player.IsInCheck;
            move.IsEnemyInCheck = move.Piece.Player.OpposingPlayer.IsInCheck;

            move.HashCodeA = Board.HashCodeA;
            move.HashCodeB = Board.HashCodeB;

            Game.MoveHistory.Add(move);

            if (move.Piece.Player.CanClaimThreeMoveRepetitionDraw)
            {
                Board.HashCodeA ^= 31;
                Board.HashCodeB ^= 29;
                move.HashCodeA = Board.HashCodeA;
                move.HashCodeB = Board.HashCodeB;
                move.IsThreeMoveRepetition = true;
            }

            return move;
        }
Example #8
0
        /// <summary>
        /// The move piece to fen position.
        /// </summary>
        /// <param name="charToken">
        /// The char token.
        /// </param>
        /// <param name="intFile">
        /// The int file.
        /// </param>
        /// <param name="intRank">
        /// The int rank.
        /// </param>
        /// <param name="blnAnyLocation">
        /// The bln any location.
        /// </param>
        /// <param name="blnAllowPromotion">
        /// The bln allow promotion.
        /// </param>
        private static void MovePieceToFenPosition(
            ref char charToken, int intFile, int intRank, bool blnAnyLocation, bool blnAllowPromotion)
        {
            Piece.PieceNames piecename = Piece.PieceNames.King;
            Player           player    = charToken.ToString() == charToken.ToString().ToUpper() ? Game.PlayerWhite : Game.PlayerBlack;

            switch (charToken.ToString().ToUpper())
            {
            case "K":
                piecename = Piece.PieceNames.King;
                break;

            case "Q":
                piecename = Piece.PieceNames.Queen;
                break;

            case "R":
                piecename = Piece.PieceNames.Rook;
                break;

            case "B":
                piecename = Piece.PieceNames.Bishop;
                break;

            case "N":
                piecename = Piece.PieceNames.Knight;
                break;

            case "P":
                piecename = Piece.PieceNames.Pawn;
                break;
            }

            // Try to find the required piece in from the available pool of captured
            // pieces that haven't been placed on the board yet.
            Piece pieceToUse = null;

            foreach (Piece pieceCaptured in player.OpposingPlayer.CapturedEnemyPieces)
            {
                if ((pieceCaptured.Name == piecename || (blnAllowPromotion && pieceCaptured.Name == Piece.PieceNames.Pawn)) &&
                    (pieceCaptured.StartLocation == Board.GetSquare(intFile, intRank) || blnAnyLocation))
                {
                    pieceToUse = pieceCaptured;
                    break;
                }
            }

            if (pieceToUse != null)
            {
                Square square = Board.GetSquare(intFile, intRank);
                pieceToUse.Uncapture(0);
                square.Piece         = pieceToUse;
                pieceToUse.Square    = square;
                pieceToUse.NoOfMoves = blnAnyLocation ? 1 : 0;
                if (pieceToUse.Name != piecename)
                {
                    pieceToUse.Promote(piecename);
                }

                // Mark the token in the original FEN string with a * to indicate that the piece has been processed
                charToken = '.';
            }
        }
Example #9
0
        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;
        }
Example #10
0
        public bool CanAttackSquare(Square target_square)
        {
            int intOrdinal = this.Base.Square.Ordinal;
            Square square;

            square = Board.GetSquare(this.Base.Square.Ordinal + this.Base.Player.PawnAttackLeftOffset);
            if (square != null && target_square.Ordinal == square.Ordinal)
                return true;
            square = Board.GetSquare(this.Base.Square.Ordinal + this.Base.Player.PawnAttackRightOffset);
            if (square != null && target_square.Ordinal == square.Ordinal)
                return true;

            return false;

        }
Example #11
0
 public bool CanAttackSquare(Square square)
 {
     return this.Top.CanAttackSquare(square);
 }
Example #12
0
 /// <summary>
 /// Initializes a new instance of the <see cref="Move"/> class.
 /// </summary>
 /// <param name="turnNo">
 /// The turn no.
 /// </param>
 /// <param name="lastMoveTurnNo">
 /// The last move turn no.
 /// </param>
 /// <param name="moveName">
 /// The move name.
 /// </param>
 /// <param name="piece">
 /// The piece moving.
 /// </param>
 /// <param name="from">
 /// The square the peice is moving from.
 /// </param>
 /// <param name="to">
 /// The square the peice is moving to.
 /// </param>
 /// <param name="pieceCaptured">
 /// The piece being captured.
 /// </param>
 /// <param name="pieceCapturedOrdinal">
 /// Ordinal position of the piece being captured.
 /// </param>
 /// <param name="score">
 /// The positional score.
 /// </param>
 public Move(int turnNo, int lastMoveTurnNo, MoveNames moveName, Piece piece, Square from, Square to, Piece pieceCaptured, int pieceCapturedOrdinal, int score)
 {
     this.EnemyStatus          = Player.PlayerStatusNames.Normal;
     this.TurnNo               = turnNo;
     this.LastMoveTurnNo       = lastMoveTurnNo;
     this.Name                 = moveName;
     this.Piece                = piece;
     this.From                 = from;
     this.To                   = to;
     this.PieceCaptured        = pieceCaptured;
     this.PieceCapturedOrdinal = pieceCapturedOrdinal;
     this.Score                = score;
     if (moveName != MoveNames.NullMove && pieceCaptured == null && piece != null && piece.Name != Piece.PieceNames.Pawn)
     {
         this.FiftyMoveDrawCounter = Game.MoveHistory.Count > 0 ? Game.MoveHistory.Last.FiftyMoveDrawCounter + 1 : (Game.FiftyMoveDrawBase / 2) + 1;
     }
 }
Example #13
0
 public bool CanAttackSquare(Square target_square)
 {
     Square square;
     for (int i = 0; i < moveVectors.Length; i++)
     {
         square = Board.GetSquare(this.Base.Square.Ordinal + moveVectors[i]);
         if (square != null && square.Ordinal == target_square.Ordinal)
             return true;
     }
     return false;
 }
Example #14
0
 public static bool DoesPieceAttackSquare(Square square, Player player, out Piece attackingPiece)
 {
     return Piece.DoesLeaperPieceTypeAttackSquare(square, player, _pieceType, moveVectors, out attackingPiece);
 }
Example #15
0
 /// <summary>
 /// Searches for the specified square and returns its index.
 /// </summary>
 /// <param name="square">
 /// The piece to search for.
 /// </param>
 /// <returns>
 /// Index value of the found square. or null if not found.
 /// </returns>
 public int IndexOf(Square square)
 {
     return this.squareList.IndexOf(square);
 }
Example #16
0
        /// <summary>
        ///   Make the specified move. For internal use only.
        /// </summary>
        /// <param name="moveName"> The move name. </param>
        /// <param name="piece"> The piece to move. </param>
        /// <param name="square"> The square to move to. </param>
        private static void MakeAMoveInternal(Move.MoveNames moveName, Piece piece, Square square)
        {
            MoveRedoList.Clear();
            Move move = piece.Move(moveName, square);
            move.EnemyStatus = move.Piece.Player.OpposingPlayer.Status;
            PlayerToPlay.Clock.Stop();
            MoveHistory.Last.TimeStamp = PlayerToPlay.Clock.TimeElapsed;
            if (PlayerToPlay.Intellegence == Player.PlayerIntellegenceNames.Computer)
            {
                WinBoard.SendMove(move);
                if (!PlayerToPlay.OpposingPlayer.CanMove) {
                    if (PlayerToPlay.OpposingPlayer.IsInCheckMate) {
                        WinBoard.SendCheckMate ();
                    } else if (!PlayerToPlay.OpposingPlayer.IsInCheck) {
                        WinBoard.SendCheckStaleMate ();
                    }
                } else if (PlayerToPlay.OpposingPlayer.CanClaimThreeMoveRepetitionDraw == true) {
                    WinBoard.SendDrawByRepetition ();
                } else if (PlayerToPlay.OpposingPlayer.CanClaimFiftyMoveDraw == true) {
                    WinBoard.SendDrawByFiftyMoveRule ();
                } else if (PlayerToPlay.OpposingPlayer.CanClaimInsufficientMaterialDraw == true) {
                    WinBoard.SendDrawByInsufficientMaterial ();
                }
            }

            BroadcastMovedName (move);
            CheckGameStatus ();
            PlayerToPlay = PlayerToPlay.OpposingPlayer;
            PlayerToPlay.Clock.Start();
        }
Example #17
0
 /// <summary>
 /// The add.
 /// </summary>
 /// <param name="turnNo">
 /// The turn no.
 /// </param>
 /// <param name="lastMoveTurnNo">
 /// The last move turn no.
 /// </param>
 /// <param name="moveName">
 /// The move name.
 /// </param>
 /// <param name="piece">
 /// The piece moving.
 /// </param>
 /// <param name="from">
 /// The square the peice is moving from.
 /// </param>
 /// <param name="to">
 /// The square the peice is moving to.
 /// </param>
 /// <param name="pieceCaptured">
 /// The piece being captured.
 /// </param>
 /// <param name="pieceCapturedOrdinal">
 /// Ordinal position of the piece being captured.
 /// </param>
 /// <param name="score">
 /// The positional score.
 /// </param>
 public void Add(int turnNo, int lastMoveTurnNo, Move.MoveNames moveName, Piece piece, Square from, Square to, Piece pieceCaptured, int pieceCapturedOrdinal, int score)
 {
     this.moves.Add(new Move(turnNo, lastMoveTurnNo, moveName, piece, from, to, pieceCaptured, pieceCapturedOrdinal, score));
 }
Example #18
0
 public bool CanAttackSquare(Square square)
 {
     return this.Top.CanAttackSquare(square);
 }
Example #19
0
        /// <summary>
        /// The player_ move considered handler.
        /// </summary>
        private void Player_MoveConsideredHandler()
        {
            this.RenderStatus();

            if (this.m_squareLastFrom != null)
            {
                this.m_picSquares[this.m_squareLastFrom.File, this.m_squareLastFrom.Rank].BackColor =
                    this.m_squareLastFrom.Colour == Square.ColourNames.White
                         ? this.BOARD_SQUARE_COLOUR_WHITE
                         : this.BOARD_SQUARE_COLOUR_BLACK;
            }

            if (Game.ShowThinking && Game.PlayerToPlay.Brain.IsThinking && !Game.PlayerToPlay.Brain.IsPondering)
            {
                if (Game.PlayerToPlay.Brain.Search.CurrentMoveSearched != null)
                {
                    this.m_squareLastFrom = Game.PlayerToPlay.Brain.Search.CurrentMoveSearched.From;

                    // m_picSquares[m_squareLastFrom.File, m_squareLastFrom.Rank].BackColor = System.Drawing.Color.Yellow;
                    this.m_picSquares[this.m_squareLastFrom.File, this.m_squareLastFrom.Rank].BackColor =
                        Board.GetSquare(
                            (int)this.m_picSquares[this.m_squareLastFrom.File, this.m_squareLastFrom.Rank].Tag).Colour
                        == Square.ColourNames.White
                            ? this.BOARD_SQUARE_COLOUR_WHITE_BRIGHT
                            : this.BOARD_SQUARE_COLOUR_BLACK_BRIGHT;
                }
            }

            if (this.m_squareLastTo != null)
            {
                this.m_picSquares[this.m_squareLastTo.File, this.m_squareLastTo.Rank].BackColor =
                    this.m_squareLastTo.Colour == Square.ColourNames.White
                         ? this.BOARD_SQUARE_COLOUR_WHITE
                         : this.BOARD_SQUARE_COLOUR_BLACK;
            }

            if (Game.ShowThinking && Game.PlayerToPlay.Brain.IsThinking && !Game.PlayerToPlay.Brain.IsPondering)
            {
                if (Game.PlayerToPlay.Brain.Search.CurrentMoveSearched != null)
                {
                    this.m_squareLastTo = Game.PlayerToPlay.Brain.Search.CurrentMoveSearched.To;

                    // m_picSquares[m_squareLastTo.File, m_squareLastTo.Rank].BackColor=System.Drawing.Color.Yellow;
                    this.m_picSquares[this.m_squareLastTo.File, this.m_squareLastTo.Rank].BackColor =
                        Board.GetSquare((int)this.m_picSquares[this.m_squareLastTo.File, this.m_squareLastTo.Rank].Tag).
                            Colour == Square.ColourNames.White
                            ? this.BOARD_SQUARE_COLOUR_WHITE_BRIGHT
                            : this.BOARD_SQUARE_COLOUR_BLACK_BRIGHT;
                }
            }
        }
Example #20
0
        /// <summary>
        /// Determines whether a sliding piece could slide to this square from the specified start square, 
        /// in the specified direction-offset. Checks that no pieces are blocking the route.
        /// </summary>
        /// <param name="squareStart">
        /// The starting square.
        /// </param>
        /// <param name="directionOffset">
        /// The direciton offset.
        /// </param>
        /// <returns>
        /// True if the piece can be slid.
        /// </returns>
        /// <exception cref="ApplicationException">
        /// An exception indicting that the alogrithm has hit the edge of the board.
        /// </exception>
        public bool CanSlideToHereFrom(Square squareStart, int directionOffset)
        {
            int intOrdinal = squareStart.Ordinal;
            Square square;

            intOrdinal += directionOffset;
            while ((square = Board.GetSquare(intOrdinal)) != null)
            {
                if (square == this)
                {
                    return true;
                }

                if (square.Piece != null)
                {
                    return false;
                }

                intOrdinal += directionOffset;
            }

            throw new ApplicationException("CanSlideToHereFrom: Hit edge of board!");
        }
Example #21
0
 /// <summary>
 /// The remove last history item.
 /// </summary>
 private void RemoveLastHistoryItem()
 {
     this.lvwMoveHistory.Items.RemoveAt(this.lvwMoveHistory.Items.Count - 1);
     this.m_squareFrom = null;
     this.m_movesPossible = new Moves();
 }
Example #22
0
        /// <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;
        }
Example #23
0
 /// <summary>
 /// The pic square_ drag drop.
 /// </summary>
 /// <param name="sender">
 /// The sender.
 /// </param>
 /// <param name="e">
 /// The e.
 /// </param>
 private void picSquare_DragDrop(object sender, DragEventArgs e)
 {
     int intOrdinal = Convert.ToInt32(((PictureBox)sender).Tag);
     this.m_squareTo = Board.GetSquare(intOrdinal);
 }
Example #24
0
        /// <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;
        }
Example #25
0
        /// <summary>
        /// The pic square_ mouse down.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The e.
        /// </param>
        private void picSquare_MouseDown(object sender, MouseEventArgs e)
        {
            if (this.m_blnInMouseDown || e.Button != MouseButtons.Left)
            {
                return;
            }

            if (Game.PlayerToPlay.Brain.IsThinking && !Game.PlayerToPlay.Brain.IsPondering)
            {
                return;
            }

            this.m_blnIsLeftMouseButtonDown = true;
            this.m_blnInMouseDown = true;

            Game.SuspendPondering();

            PictureBox picFrom = (PictureBox)sender;

            int intOrdinalFrom = Convert.ToInt32(picFrom.Tag);

            Square squareFrom = Board.GetSquare(intOrdinalFrom);

            Piece pieceFrom = squareFrom.Piece;
            if (pieceFrom != null && pieceFrom.Player.Colour == Game.PlayerToPlay.Colour)
            {
                picFrom.Image = null;
                picFrom.Refresh();

                this.m_curPieceCursor = this.GetPieceCursor(pieceFrom);
                this.pnlEdging.Cursor = this.m_curPieceCursor;

                // Mark possible moves
                this.m_squareFrom = squareFrom;
                this.m_squareTo = null;
                this.m_movesPossible = new Moves();
                pieceFrom.GenerateLegalMoves(this.m_movesPossible);
                this.RenderBoardColours();
                this.pnlEdging.Refresh();

                Game.ResumePondering();

                if (this.m_blnIsLeftMouseButtonDown
                    && ((PictureBox)sender).DoDragDrop(pieceFrom, DragDropEffects.Move) == DragDropEffects.Move)
                {
                    Game.SuspendPondering();

                    bool blnMoveMade = false;
                    Piece pieceTo = this.m_squareTo.Piece;

                    // Is it an empty space or enemy piece
                    if (pieceTo == null || pieceTo != null && pieceTo.Player.Colour != Game.PlayerToPlay.Colour)
                    {
                        // Check to see it the move is valid, by comparing against all possible valid moves
                        bool blnIsPromotion = false;
                        Move.MoveNames movenamePromotion = Model.Move.MoveNames.NullMove;
                        foreach (Move move in this.m_movesPossible)
                        {
                            if (move.To == this.m_squareTo)
                            {
                                if (!blnIsPromotion)
                                {
                                    switch (move.Name)
                                    {
                                        case Model.Move.MoveNames.PawnPromotionQueen:
                                        case Model.Move.MoveNames.PawnPromotionRook:
                                        case Model.Move.MoveNames.PawnPromotionBishop:
                                        case Model.Move.MoveNames.PawnPromotionKnight:
                                            blnIsPromotion = true;
                                            frmPieceSelector formPieceSelector = new frmPieceSelector();
                                            formPieceSelector.Colour = move.Piece.Player.Colour;
                                            formPieceSelector.ShowDialog(this);
                                            movenamePromotion = formPieceSelector.MoveNameSelected;
                                            break;
                                    }
                                }

                                if (!blnIsPromotion || move.Name == movenamePromotion)
                                {
                                    this.m_squareFrom = null;
                                    this.m_movesPossible = new Moves();

                                    Game.MakeAMove(move.Name, move.Piece, move.To);
                                    blnMoveMade = true;
                                    break;
                                }
                            }
                        }
                    }

                    if (!blnMoveMade)
                    {
                        this.m_picSquares[this.m_squareFrom.File, this.m_squareFrom.Rank].Image =
                            this.imgPieces.Images[this.m_squareFrom.Piece.ImageIndex];
                        this.m_squareFrom = null;
                        this.m_movesPossible = null;
                        this.RenderBoardColours();
                    }
                }
                else
                {
                    Game.SuspendPondering();

                    this.m_picSquares[this.m_squareFrom.File, this.m_squareFrom.Rank].Image =
                        this.imgPieces.Images[this.m_squareFrom.Piece.ImageIndex];
                    this.m_squareFrom = null;
                    this.m_movesPossible = null;
                    this.RenderBoardColours();

                    Game.ResumePondering();
                }

                this.pnlEdging.Cursor = Cursors.Default;
            }
            else
            {
                Game.ResumePondering();
            }

            this.m_blnInMouseDown = false;
        }
Example #26
0
 /// <summary>
 /// Move the piece to a new square, after testing that the move is valid.
 /// </summary>
 /// <param name="moveName">
 /// The move name.
 /// </param>
 /// <param name="square">
 /// The square.
 /// </param>
 /// <returns>
 /// Move made, or null if move is not valid.
 /// </returns>
 public Move TestAndMakeMove(Move.MoveNames moveName, Square square)
 {
     return(null);
 }
Example #27
0
        /// <summary>
        /// The game_ board position changed handler.
        /// </summary>
        private void Game_BoardPositionChangedHandler()
        {
            // 			if (Game.IsPaused) return;
            if (Game.MoveHistory.Count > 0)
            {
                Move move = Game.MoveHistory.Last;
                if (this.sbr.Text.StartsWith("Thinking..."))
                {
                    this.sbr.Text = this.sbr.Text.Substring("Thinking...".Length);
                }

                this.sbr.Text += "  Moved: " + move.Piece.Name.ToString() //+ " " + move.From.Name + "-" + move.To.Name
                                 + " " + move.Description;
            }

            this.pbr.Value = 0;

            this.m_squareFrom = null;
            this.m_movesPossible = new Moves();

            this.RenderMoveAnalysis();
            this.RenderBoard();
        }
Example #28
0
 /// <summary>
 /// Insert a sqaure into the list. at the specified index position.
 /// </summary>
 /// <param name="ordinal">
 /// The ordinal index position where the square will be inserted.
 /// </param>
 /// <param name="square">
 /// The piece.
 /// </param>
 public void Insert(int ordinal, Square square)
 {
     this.squareList.Insert(ordinal, square);
 }
Example #29
0
 /// <summary>
 /// Move the piece to a new square, after testing that the move is valid.
 /// </summary>
 /// <param name="moveName">
 /// The move name.
 /// </param>
 /// <param name="square">
 /// The square.
 /// </param>
 /// <returns>
 /// Move made, or null if move is not valid.
 /// </returns>
 public Move TestAndMakeMove(Move.MoveNames moveName, Square square)
 {
     return null;
 }
Example #30
0
 /// <summary>
 /// Adds a new square to the list.
 /// </summary>
 /// <param name="square">
 /// The square to add.
 /// </param>
 public void Add(Square square)
 {
     this.squareList.Add(square);
 }
Example #31
0
 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;
 }
Example #32
0
        /// <summary>
        /// The generate lazy moves.
        /// </summary>
        /// <param name="depth">
        /// The depth.
        /// </param>
        /// <param name="moves">
        /// The moves.
        /// </param>
        /// <param name="movesType">
        /// The moves type.
        /// </param>
        /// <param name="squareAttacking">
        /// The square attacking.
        /// </param>
        public void GenerateLazyMoves(int depth, Moves moves, Moves.MoveListNames movesType, Square squareAttacking)
        {
            // if (squareAttacking==null)
            // {
            // All moves as defined by movesType
            foreach (Piece piece in this.Pieces)
            {
                piece.GenerateLazyMoves(moves, movesType);

                /*
                if (movesType != Moves.MoveListNames.All)
                {
                    int intIndex;
                    for (intIndex = moves.Count - 1; intIndex >= 0; intIndex--)
                    {
                        Move move = moves[intIndex];
                        if (!(
                             move.Name == Move.MoveNames.PawnPromotionQueen
                             &&
                             move.PieceCaptured == null
                             (move.Name == Move.MoveNames.Standard
                              && move.From.Piece.BasicValue < move.To.Piece.BasicValue)
                             ||
                             (move.Name == Move.MoveNames.Standard
                              && !move.To.PlayerCanMoveToThisSquare(move.Piece.Player.OtherPlayer))
                             ||
                             move.To.Ordinal==squareAttacking.Ordinal
                             ))
                        {
                            moves.Remove(move);
                        }
                    }
                }
                */
            }

            // }
            // else
            // {
            // Just re-capture moves
            // squareAttacking.AttackerMoveList(moves, this);
            // }
        }
Example #33
0
 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;
 }
Example #34
0
 /// <summary>
 ///   Make a move.
 /// </summary>
 /// <param name="moveName"> The move name. </param>
 /// <param name="piece"> The piece to move. </param>
 /// <param name="square"> The square to move to. </param>
 public static void MakeAMove(Move.MoveNames moveName, Piece piece, Square square)
 {
     SuspendPondering();
     MakeAMoveInternal(moveName, piece, square);
     SaveBackup();
     SendBoardPositionChangeEvent();
        // CheckIfAutoNextMove();
 }
Example #35
0
 /// <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);
 }