Ejemplo n.º 1
0
        public void TestKnightInvalidMovement()
        {
            ChessBoard cb = new ChessBoard ();
            Knight kn = new Knight (cb [4, 4], "B");
            cb [4, 4].PlacePiece (kn);

            Assert.IsFalse(kn.CheckMovementRule(cb[3,3], cb));
            Assert.IsFalse(kn.CheckMovementRule(cb[7,7], cb));
            Assert.IsFalse(kn.CheckMovementRule(cb[4, 3], cb));

            Assert.AreEqual (kn.MovePiece (cb [4, 3], cb), "Invalid move");
            Assert.AreNotSame (kn.Grid, cb [4, 3]);
            Assert.AreSame (kn.Grid, cb [4, 4]);
        }
Ejemplo n.º 2
0
        public void TestKnightMovementOnEmpty()
        {
            ChessBoard cb = new ChessBoard ();
            Knight kn = new Knight (cb [4, 4], "B");
            cb [4, 4].PlacePiece (kn);

            Assert.IsTrue(kn.CheckMovementRule(cb[6, 5], cb));
            Assert.IsTrue(kn.CheckMovementRule(cb[5, 6], cb));
            Assert.IsTrue(kn.CheckMovementRule(cb[3, 2], cb));
            Assert.IsTrue(kn.CheckMovementRule(cb[2, 3], cb));
            Assert.IsTrue(kn.CheckMovementRule(cb[3, 6], cb));
            Assert.IsTrue(kn.CheckMovementRule(cb[6, 3], cb));
            Assert.IsTrue(kn.CheckMovementRule(cb[2, 5], cb));
            Assert.IsTrue(kn.CheckMovementRule(cb[5, 2], cb));

            Assert.IsNull (kn.MovePiece (cb [5, 2], cb));
            Assert.AreEqual (kn.Grid, cb [5, 2]);
        }
Ejemplo n.º 3
0
        public void TestKnightMovementOnGridWithEnemyPiece()
        {
            ChessBoard cb = new ChessBoard ();
            Knight kn = new Knight (cb [4, 4], "B");
            cb [4, 4].PlacePiece (kn);

            cb [6, 5].PlacePiece (new Knight (cb [6, 5], "W"));
            Assert.IsTrue(kn.CheckMovementRule(cb[6, 5], cb));
            cb [5, 6].PlacePiece (new Knight (cb [5, 6], "W"));
            Assert.IsTrue(kn.CheckMovementRule(cb[5, 6], cb));
            cb [3, 2].PlacePiece (new Knight (cb [3, 2], "W"));
            Assert.IsTrue(kn.CheckMovementRule(cb[3, 2], cb));
            cb [2, 3].PlacePiece (new Knight (cb [2, 3], "W"));
            Assert.IsTrue(kn.CheckMovementRule(cb[2, 3], cb));
            cb [3, 6].PlacePiece (new Knight (cb [3, 6], "W"));
            Assert.IsTrue(kn.CheckMovementRule(cb[3, 6], cb));
            cb [6, 3].PlacePiece (new Knight (cb [6, 3], "W"));
            Assert.IsTrue(kn.CheckMovementRule(cb[6, 3], cb));
            cb [2, 5].PlacePiece (new Knight (cb [2, 5], "W"));
            Assert.IsTrue(kn.CheckMovementRule(cb[2, 5], cb));
            cb [5, 2].PlacePiece (new Knight (cb [5, 2], "W"));
            Assert.IsTrue(kn.CheckMovementRule(cb[5, 2], cb));
        }
Ejemplo n.º 4
0
        private void CanvasMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            if (draggedImage != null)
            {
                boardCanvas.ReleaseMouseCapture();

                var Position = e.GetPosition(boardCanvas);
                var offset   = Position - mousePosition;
                mousePosition = Position;

                int      destRow = (int)(8 * Position.Y / boardCanvas.Height);
                int      destCol = (int)(8 * Position.X / boardCanvas.Width);
                Position to      = new Position(destRow, destCol);

                if (engine.ValidHumanMove(selectedPiece, to))
                {
                    Position from    = selectedPiece.Position;
                    Piece    capture = engine.CapturePiece(selectedPiece, to);

                    bool promotion = selectedPiece is Pawn && (destRow == 7 || destRow == 0);

                    Move move = new Move(from, to, selectedPiece, capture)
                    {
                        Promotion = promotion
                    };

                    if (move.Promotion)
                    {
                        PawnPromotion pawnPromotion = new PawnPromotion();
                        pawnPromotion.ShowDialog();
                        Piece promoteTo;

                        switch (pawnPromotion.SelectedPiece)
                        {
                        case PieceType.Queen:
                            promoteTo = new Queen(move.Piece.Color, engine.Board, move.Piece.Index);
                            break;

                        case PieceType.Bishop:
                            promoteTo = new Bishop(move.Piece.Color, engine.Board, move.Piece.Index);
                            break;

                        case PieceType.Rook:
                            promoteTo = new Rook(move.Piece.Color, engine.Board, move.Piece.Index);
                            break;

                        case PieceType.Knight:
                            promoteTo = new Knight(move.Piece.Color, engine.Board, move.Piece.Index);
                            break;

                        default:
                            promoteTo = new Queen(move.Piece.Color, engine.Board, move.Piece.Index);
                            break;
                        }

                        move.AddPromotion(selectedPiece, promoteTo);
                    }

                    if (move.Piece is Pawn)
                    {
                        move.EnPassantMove = Math.Abs(move.To.Row - move.From.Row) == 2;
                    }
                    if (move.Piece is King)
                    {
                        if (move.From.Col - move.To.Col == 2)
                        {
                            move.Castle     = true;
                            move.CastleMove = new Move(new Position(move.Piece.Position.Row, 0), new Position(move.Piece.Position.Row, 3),
                                                       engine.Board.GetPiece(move.Piece.Position.Row, 0), engine.Board.GetPiece(move.Piece.Position.Row, 3));
                        }
                        else if (move.From.Col - move.To.Col == -2)
                        {
                            move.Castle     = true;
                            move.CastleMove = new Move(new Position(move.Piece.Position.Row, 7), new Position(move.Piece.Position.Row, 5),
                                                       engine.Board.GetPiece(move.Piece.Position.Row, 7), engine.Board.GetPiece(move.Piece.Position.Row, 5));
                        }
                    }

                    engine.MovePiece(move);
                    Debug.WriteLine(engine.PrintBoard());
                    Debug.WriteLine(engine.WriteMove(move));
                    engine.ShowMove();
                }
                else
                {
                    Canvas.SetTop(imageDictionary[selectedPiece], selectedPiece.Position.Row * boardCanvas.Height / 8);
                    Canvas.SetLeft(imageDictionary[selectedPiece], selectedPiece.Position.Col * boardCanvas.Width / 8);
                    draggedImage.InvalidateVisual();
                }

                Panel.SetZIndex(draggedImage, 0);
                draggedImage = null;
            }
        }
Ejemplo n.º 5
0
        public ChessGame()
        {
            /*
             * 1  R N B K Q B N R
             * 2  P P P P P P P P
             * 3
             * 4
             * 5
             * 6
             * 7  P P P P P P P P
             * 8  R N B K Q B N R
             *    A B C D E F G H
             *
             * {
             * {0,0 0,1 0,2 0,3 0,4 0,5 0,6 0,7}
             * {1,0 1,1 1,2 1,3 1,4 1,5 1,6 1,7}
             * {2,0 2,1 2,2 2,3 2,4 2,5 2,6 2,7}
             * {3,0 3,1 3,2 3,3 3,4 3,5 3,6 3,7}
             * {4,0 4,1 4,2 4,3 4,4 4,5 4,6 4,7}
             * {5,0 5,1 5,2 5,3 5,4 5,5 5,6 5,7}
             * {6,0 6,1 6,2 6,3 6,4 6,5 6,6 6,7}
             * {7,0 7,1 7,2 7,3 7,4 7,5 7,6 7,7}
             * }
             *
             * Movement patterns:
             * Rook:
             * The Rook can only move in rows and columns, and therefore
             * can either move to the same index in a lower or higher indexed array contained in the first dimension,
             * or it can move to any other position within its current second dimension.
             *
             * Bishop:
             * The Bishop can only move diagonally and therefore its second-dimensional index must be shifted by 1
             * in either direction for every step it takes in the first dimension.
             *
             * Queen:
             * The Queen possesses the movement capabilities of both the Rook and the Bishop.
             *
             * King:
             * The King may only move 1 tile at a time, and therefore its indexed position
             * can only be shifted by 1 in any direction, in both the first and the second dimension.
             *
             * Pawn:
             * The Pawn can only move forward in a straight line,
             * except when there is a piece of the opposite color on a tile that is located
             * on either of the 2 frontal diagonal places.
             * The pawn may move 2 steps straight forward on its very first move.
             * If the pawn reaches the opposite side of the board, it may be converted into
             * a Rook, Bishop, Knight, or a Queen.
             *
             * Knight:
             * The Knight may move to any position that is 2 steps in any of the 4 cardinal directions,
             * and then displaced 1 step to either the right or left.
             * Unlike all other pieces, the Knight is not hindered by other pieces being in its way.
             *
             */
            //Add pieces to board and set appropriate types and colors
            board[7, 0] = new Rook(PieceColor.black, board);
            board[7, 1] = new Knight(PieceColor.black, board);
            board[7, 2] = new Bishop(PieceColor.black, board);
            board[7, 3] = new King(PieceColor.black, board);
            board[7, 4] = new Queen(PieceColor.black, board);
            board[7, 5] = new Bishop(PieceColor.black, board);
            board[7, 6] = new Knight(PieceColor.black, board);
            board[7, 7] = new Rook(PieceColor.black, board);

            board[0, 0] = new Rook(PieceColor.white, board);
            board[0, 1] = new Knight(PieceColor.white, board);
            board[0, 2] = new Bishop(PieceColor.white, board);
            board[0, 3] = new King(PieceColor.white, board);
            board[0, 4] = new Queen(PieceColor.white, board);
            board[0, 5] = new Bishop(PieceColor.white, board);
            board[0, 6] = new Knight(PieceColor.white, board);
            board[0, 7] = new Rook(PieceColor.white, board);

            for (int i = 0; i < 8; i += 1)
            {
                board[1, i] = new Pawn(PieceColor.white, board);
            }

            for (int i = 0; i < 8; i += 1)
            {
                board[6, i] = new Pawn(PieceColor.black, board);
            }
        }