Example #1
0
        private Piece GetPieceAtSquare(ChessBoardCoord square)
        {
            byte ret = (byte)(_board[square.BoardX, square.BoardY] & (1 << LOWER_PIECETYPE_BIT));

            ret |= (byte)(_board[square.BoardX, square.BoardY] & (1 << MIDDLE_PIECETYPE_BIT));
            ret |= (byte)(_board[square.BoardX, square.BoardY] & (1 << UPPER_PIECETYPE_BIT));
            return((Piece)ret);
        }
Example #2
0
 internal static bool CheckValidMove(Piece movingPiece,
                                     PieceColor movingPieceColor,
                                     bool movingPieceFirstMove,
                                     ChessBoardCoord origin,
                                     Piece destinationPiece,
                                     PieceColor destinationPieceColor,
                                     ChessBoardCoord destination)
 {
     return(false);
 }
Example #3
0
        /// <summary>
        /// Debugging function, remove later...
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <returns></returns>
        public string GetPieceVisualizationString(ChessBoardCoord selectedSquare)
        {
            byte square = _board[selectedSquare.BoardX, selectedSquare.BoardY];

            if (square == 0xFF)
            {
                return("II");
            }
            else if (square == 0x00)
            {
                return("00");
            }

            PieceColor color = GetPieceColorAtSquare(selectedSquare);
            Piece      piece = GetPieceAtSquare(selectedSquare);
            string     ret   = "";

            switch (color)
            {
            case PieceColor.Black:
                ret += "B";
                break;

            case PieceColor.White:
                ret += "W";
                break;
            }
            switch (piece)
            {
            case Piece.Pawn:
                ret += "P";
                break;

            case Piece.Knight:
                ret += "N";
                break;

            case Piece.Bishop:
                ret += "B";
                break;

            case Piece.Rook:
                ret += "R";
                break;

            case Piece.Queen:
                ret += "Q";
                break;

            case Piece.King:
                ret += "K";
                break;
            }
            return(ret);
        }
Example #4
0
 public bool Move(ChessBoardCoord origin, ChessBoardCoord destination)
 {
     if (!IsSaneMove(origin, destination) || !IsPathClear(origin, destination))
     {
         return(false);
     }
     SetSquare(GetPieceAtSquare(origin), GetPieceColorAtSquare(origin), destination);
     ClearSquare(origin);
     SetMoved(destination);
     return(true);
 }
Example #5
0
        private void SetSquare(Piece piece, ChessBoardCoord square)
        {
            switch ((int)piece)
            {
            case 0:
                _board[square.BoardX, square.BoardY] = (byte)(_board[square.BoardX, square.BoardY] & ~(1 << LOWER_PIECETYPE_BIT));
                _board[square.BoardX, square.BoardY] = (byte)(_board[square.BoardX, square.BoardY] & ~(1 << MIDDLE_PIECETYPE_BIT));
                _board[square.BoardX, square.BoardY] = (byte)(_board[square.BoardX, square.BoardY] & ~(1 << UPPER_PIECETYPE_BIT));
                break;

            case 1:
                _board[square.BoardX, square.BoardY] = (byte)(_board[square.BoardX, square.BoardY] | (1 << LOWER_PIECETYPE_BIT));
                _board[square.BoardX, square.BoardY] = (byte)(_board[square.BoardX, square.BoardY] & ~(1 << MIDDLE_PIECETYPE_BIT));
                _board[square.BoardX, square.BoardY] = (byte)(_board[square.BoardX, square.BoardY] & ~(1 << UPPER_PIECETYPE_BIT));
                break;

            case 2:
                _board[square.BoardX, square.BoardY] = (byte)(_board[square.BoardX, square.BoardY] & ~(1 << LOWER_PIECETYPE_BIT));
                _board[square.BoardX, square.BoardY] = (byte)(_board[square.BoardX, square.BoardY] | (1 << MIDDLE_PIECETYPE_BIT));
                _board[square.BoardX, square.BoardY] = (byte)(_board[square.BoardX, square.BoardY] & ~(1 << UPPER_PIECETYPE_BIT));
                break;

            case 3:
                _board[square.BoardX, square.BoardY] = (byte)(_board[square.BoardX, square.BoardY] | (1 << LOWER_PIECETYPE_BIT));
                _board[square.BoardX, square.BoardY] = (byte)(_board[square.BoardX, square.BoardY] | (1 << MIDDLE_PIECETYPE_BIT));
                _board[square.BoardX, square.BoardY] = (byte)(_board[square.BoardX, square.BoardY] & ~(1 << UPPER_PIECETYPE_BIT));
                break;

            case 4:
                _board[square.BoardX, square.BoardY] = (byte)(_board[square.BoardX, square.BoardY] & ~(1 << LOWER_PIECETYPE_BIT));
                _board[square.BoardX, square.BoardY] = (byte)(_board[square.BoardX, square.BoardY] & ~(1 << MIDDLE_PIECETYPE_BIT));
                _board[square.BoardX, square.BoardY] = (byte)(_board[square.BoardX, square.BoardY] | (1 << UPPER_PIECETYPE_BIT));
                break;

            case 5:
                _board[square.BoardX, square.BoardY] = (byte)(_board[square.BoardX, square.BoardY] | (1 << LOWER_PIECETYPE_BIT));
                _board[square.BoardX, square.BoardY] = (byte)(_board[square.BoardX, square.BoardY] & ~(1 << MIDDLE_PIECETYPE_BIT));
                _board[square.BoardX, square.BoardY] = (byte)(_board[square.BoardX, square.BoardY] | (1 << UPPER_PIECETYPE_BIT));
                break;

            case 6:
                _board[square.BoardX, square.BoardY] = (byte)(_board[square.BoardX, square.BoardY] & ~(1 << LOWER_PIECETYPE_BIT));
                _board[square.BoardX, square.BoardY] = (byte)(_board[square.BoardX, square.BoardY] | (1 << MIDDLE_PIECETYPE_BIT));
                _board[square.BoardX, square.BoardY] = (byte)(_board[square.BoardX, square.BoardY] | (1 << UPPER_PIECETYPE_BIT));
                break;

            case 7:
                _board[square.BoardX, square.BoardY] = (byte)(_board[square.BoardX, square.BoardY] | (1 << LOWER_PIECETYPE_BIT));
                _board[square.BoardX, square.BoardY] = (byte)(_board[square.BoardX, square.BoardY] | (1 << MIDDLE_PIECETYPE_BIT));
                _board[square.BoardX, square.BoardY] = (byte)(_board[square.BoardX, square.BoardY] | (1 << UPPER_PIECETYPE_BIT));
                break;
            }
        }
Example #6
0
        private void SetSquare(PieceColor color, ChessBoardCoord square)
        {
            switch ((int)color)
            {
            case 0:
                _board[square.BoardX, square.BoardY] = (byte)(_board[square.BoardX, square.BoardY] & ~(1 << PIECE_COLOR_BIT));
                break;

            case 1:
                _board[square.BoardX, square.BoardY] = (byte)(_board[square.BoardX, square.BoardY] | (1 << PIECE_COLOR_BIT));
                break;
            }
        }
Example #7
0
        private bool IsSaneMove(ChessBoardCoord origin, ChessBoardCoord destination)
        {
            if (_board[origin.BoardX, origin.BoardY] == EMPTY_SQUARE ||
                _board[origin.BoardX, origin.BoardY] == INVALID_SQUARE ||
                _board[origin.BoardX, origin.BoardY] == INVALID_SQUARE)
            {
                return(false);
            }

            if (GetPieceColorAtSquare(origin) == GetPieceColorAtSquare(destination))
            {
                return(false);
            }

            return(true);
        }
Example #8
0
        private PieceColor GetPieceColorAtSquare(ChessBoardCoord square)
        {
            if (_board[square.BoardX, square.BoardY] == EMPTY_SQUARE)
            {
                return(PieceColor.EmptySquare);
            }

            if ((byte)(_board[square.BoardX, square.BoardY] & (1 << PIECE_COLOR_BIT)) != 0)
            {
                return((PieceColor)1);
            }
            else
            {
                return(0);
            }
        }
Example #9
0
 private bool IsPathClear(ChessBoardCoord origin, ChessBoardCoord destination)
 {
     if (origin.BoardX == destination.BoardX)
     {
         for (int i = (origin.BoardY + 1); i < (destination.BoardY); i++)
         {
             if (_board[origin.BoardX, i] != EMPTY_SQUARE)
             {
                 return(false);
             }
         }
     }
     else if (origin.BoardY == destination.BoardY)
     {
         for (int i = (origin.BoardX); i < destination.BoardX; i++)
         {
             if (_board[i, origin.BoardY] != EMPTY_SQUARE)
             {
                 return(false);
             }
         }
     }
     else if (Math.Abs(destination.BoardX - origin.BoardX) == Math.Abs(destination.BoardY - origin.BoardY))
     {
         for (int i = (origin.BoardX + 1), j = (origin.BoardY + 1); i < destination.BoardX; i++, j++)
         {
             if (_board[i, j] != EMPTY_SQUARE)
             {
                 return(false);
             }
         }
     }
     else
     {
         return(false);
     }
     return(true);
 }
Example #10
0
        internal static bool ValidMoveForPiece(Piece movingPiece,
                                               PieceColor movingPieceColor,
                                               bool movingPieceFirstMove,
                                               Piece destinationPiece,
                                               ChessBoardCoord origin,
                                               ChessBoardCoord destination)
        {
            switch (movingPiece)
            {
            case Piece.Pawn:
            {
                //Average case
                if (destination.Y == origin.Y + 1)
                {
                    if (movingPieceColor == PieceColor.White &&
                        destinationPiece == Piece.NoPiece)
                    {
                        return(true);
                    }
                    return(false);
                }
                else if (destination.X == origin.X - 1)
                {
                    if (movingPieceColor == PieceColor.Black &&
                        destinationPiece == Piece.NoPiece)
                    {
                        return(true);
                    }
                    return(false);
                }
                //Initial Move case
                else if ((destination.Y == origin.Y + 2))
                {
                    if (movingPieceColor == PieceColor.White &&
                        movingPieceFirstMove &&
                        destinationPiece == Piece.NoPiece)
                    {
                        return(true);
                    }
                    return(false);
                }
                else if ((destination.Y == origin.Y - 2))
                {
                    if (movingPieceColor == PieceColor.Black &&
                        movingPieceFirstMove &&
                        destinationPiece == Piece.NoPiece)
                    {
                        return(true);
                    }
                    return(false);
                }
                //Capture case
                else if (((destination.X == origin.X + 1) || (destination.X == origin.X - 1)) &&
                         (destination.Y == origin.Y + 1) &&
                         (movingPieceColor == PieceColor.White) &&
                         (destinationPiece != Piece.NoPiece))
                {
                    return(true);
                }
                else if (((destination.X == origin.X + 1) || (destination.X == origin.X - 1)) &&
                         (destination.Y == origin.Y - 1) &&
                         (movingPieceColor == PieceColor.Black) &&
                         (destinationPiece != Piece.NoPiece))
                {
                    return(true);
                }

                //En passant

                return(false);
            }

            default:
                return(false);
            }
        }
Example #11
0
 private void ClearSquare(ChessBoardCoord square)
 {
     _board[square.BoardX, square.BoardY] = 0;
 }
Example #12
0
 private void SetSquare(Piece piece, PieceColor color, ChessBoardCoord square)
 {
     SetSquare(piece, square);
     SetSquare(color, square);
 }
Example #13
0
 private void SetMoved(ChessBoardCoord square)
 {
     _board[square.BoardX, square.BoardY] = (byte)(_board[square.BoardX, square.BoardY] | (1 << PIECE_MOVED_BIT));
 }