Exemplo n.º 1
0
        private static ChessPiece GetNewPieceFromChar(char type, PieceMaterial material, Vector2D position)
        {
            ChessPiece piece = null;

            switch (type.ToString().ToUpper())
            {
            case "Q":
                piece = new Queen(position, material);
                break;

            case "K":
                piece = new King(position, material);
                break;

            case "P":
                piece = new Pawn(position, material);
                break;

            case "N":
                piece = new Knight(position, material);
                break;

            case "B":
                piece = new Bishop(position, material);
                break;

            case "R":
                piece = new Rook(position, material);
                break;

            default:
                break;
            }
            return(piece);
        }
Exemplo n.º 2
0
 public ChessBoardSprite(ContentManager content, PieceMaterial mat, Vector2D renderPosition, Vector2D boardPosition, ChessPiece piece = null)
 {
     Texture                      = content.Load <Texture2D>((mat == PieceMaterial.L ? "chessboard_light.png" : "chessboard_dark.png"));
     BoardPosition                = boardPosition;
     RenderPosition               = renderPosition;
     OriginalPosition             = new Vector2D(renderPosition.X, renderPosition.Y);
     MouseInteraction.LeftClick  += OnLeftClick;
     MouseInteraction.RightClick += OnRightClick;
     PieceInThisPosition          = piece;
     UpdateChessPieceTexture();
 }
Exemplo n.º 3
0
        private static PieceMaterial GetPieceMaterialFromChar(char pieceMaterial)
        {
            PieceMaterial p = PieceMaterial.D;

            if (pieceMaterial.ToString().ToUpper() == "L")
            {
                p = PieceMaterial.L;
            }

            return(p);
        }
Exemplo n.º 4
0
        private void AddChessBoard()
        {
            for (int y = 0; y < 8; y++)
            {
                for (int x = 0; x < 8; x++)
                {
                    bool          isDark          = ((x + y) % 2 != 0);
                    PieceMaterial material        = (isDark ? PieceMaterial.D : PieceMaterial.L);
                    string        textureFilename = "chessboard_" + (isDark ? "dark" : "light") + ".png";

                    ChessBoardSprite boardSprite = new ChessBoardSprite(content, material,
                                                                        new Vector2D(x * TextureDimension, y * TextureDimension), new Vector2D(x, y));

                    Sprites.Add(boardSprite);
                }
            }
        }
Exemplo n.º 5
0
        public List <ChessPiece> FindByMaterial(PieceMaterial mat)
        {
            List <ChessPiece> pieces = new List <ChessPiece>();

            for (int y = 0; y < Height; y++)
            {
                for (int x = 0; x < Width; x++)
                {
                    var p = Board[y, x];
                    if (p != null && p.PieceType == mat)
                    {
                        pieces.Add(p);
                    }
                }
            }

            return(pieces);
        }
Exemplo n.º 6
0
        public ChessPiece FindPiece(Type pieceType, PieceMaterial mat)
        {
            ChessPiece found = null;

            for (int y = 0; y < Height && found == null; y++)
            {
                for (int x = 0; x < Width; x++)
                {
                    ChessPiece p = Board[y, x];
                    if (p != null && p.GetType() == pieceType && p.PieceType == mat)
                    {
                        found = p;
                        break;
                    }
                }
            }

            return(found);
        }
Exemplo n.º 7
0
        public override bool IsValidMove(Vector2D toPosition)
        {
            bool isValid = false;

            if (IsValidBishopMovement(BoardPosition, toPosition) ||
                IsValidRookMovement(BoardPosition, toPosition))
            {
                Direction     heading = GetDirection(BoardPosition, toPosition);
                PieceMaterial color   = PieceType;

                if ((color == PieceMaterial.L && acceptableLightDirections.Contains(heading)) ||
                    (color == PieceMaterial.D && acceptableDarkDirections.Contains(heading)))
                {
                    bool passesCoordTest = ((toPosition.Y >= 0 && toPosition.Y < ChessBoard.Height) && (toPosition.X >= 0 && toPosition.X < ChessBoard.Width));
                    if (passesCoordTest)
                    {
                        int yDiff = Math.Abs(BoardPosition.Y - toPosition.Y);
                        if (yDiff == 1 || (IsFirstMove && yDiff == EnPassantMoveLength))
                        {
                            int        xDiff           = Math.Abs(BoardPosition.X - toPosition.X);
                            ChessPiece pieceAtPosition = ChessGame.ChessBoard.Board[toPosition.Y, toPosition.X];
                            if (!(pieceAtPosition != null && pieceAtPosition.GetType() == typeof(King)))
                            {
                                if (xDiff == 0)
                                {
                                    isValid = (pieceAtPosition == null);
                                }
                                else if (IsValidBishopMovement(BoardPosition, toPosition) && xDiff == 1)
                                {
                                    if (pieceAtPosition != null && PiecesAreSeparateTeams(BoardPosition, toPosition))
                                    {
                                        isValid = true;
                                    }
                                }
                            }
                        }
                    }
                }
            }

            return(isValid);
        }
Exemplo n.º 8
0
        private static string ExecuteAndTranslatePlacement(string command)
        {
            string translated = "";

            // Get data from the command.
            char pieceType       = command[pieceData_Type];
            char pieceMaterialId = command[pieceData_Material];
            char pieceBoardX     = command[pieceData_BoardX];
            char pieceBoardY     = command[pieceData_BoardY];

            // Convert chars to ints and store in a Vector2D
            int      boardX   = pieceBoardX.ToString().ToUpper()[0] - LowAsciiCharIndex;
            int      boardY   = (pieceBoardY.ToString().ToUpper()[0] - (LowAsciiNumIndex + 1)).Inverted();
            Vector2D position = new Vector2D(boardX, boardY);

            // Get PieceMaterial from char.
            PieceMaterial pieceMaterial = GetPieceMaterialFromChar(pieceMaterialId);

            // Get an instance of the correct piece from the data provided.
            ChessPiece pieceToBePlaced = GetNewPieceFromChar(pieceType, pieceMaterial, position);

            // Actually place the piece if the slot is free.
            var gameBoard = ChessGame.ChessBoard;

            if (gameBoard.IsSpotFree(pieceToBePlaced.BoardPosition.X, pieceToBePlaced.BoardPosition.Y))
            {
                // Actually place the piece on the board.
                gameBoard.Board[pieceToBePlaced.BoardPosition.Y, pieceToBePlaced.BoardPosition.X] = pieceToBePlaced;

                // Make it English now.
                translated += "Placed a " + (pieceToBePlaced.PieceType == PieceMaterial.L ? "white " : "black ");
                translated += pieceToBePlaced.GetType().Name +
                              " at BoardPosition (" + pieceToBePlaced.BoardPosition.X + ", " + pieceToBePlaced.BoardPosition.Y + ")";
            }
            else
            {
                Console.WriteLine("Can't place piece. Position is not empty!");
            }

            return(translated);
        }
Exemplo n.º 9
0
 public Rook(Vector2D position, PieceMaterial pieceType) : base(position, pieceType)
 {
 }
Exemplo n.º 10
0
 public ChessPiece(Vector2D position, PieceMaterial pieceType)
 {
     BoardPosition = position;
     PieceType     = pieceType;
 }
Exemplo n.º 11
0
 public Bishop(Vector2D position, PieceMaterial pieceType) : base(position, pieceType)
 {
 }
Exemplo n.º 12
0
 public Knight(Vector2D position, PieceMaterial pieceType) : base(position, pieceType)
 {
 }