public void TestCapturePossibility()
        {
            ChessBoard cb = new ChessBoard ();
            Bishop bsh = new Bishop (cb [4, 4], "B");
            cb [4, 4].PlacePiece (bsh);

            //Check attempt to capture same color piece
            cb [1, 1].PlacePiece (new Bishop(cb[1, 1], "B"));
            cb [6, 6].PlacePiece (new Bishop(cb[6, 6], "B"));
            cb [2, 6].PlacePiece (new Bishop(cb[2, 6], "B"));
            cb [6, 2].PlacePiece (new Bishop(cb[6, 2], "B"));
            Assert.IsFalse (bsh.CheckMovementRule (cb [1, 1], cb));
            Assert.IsFalse(bsh.CheckMovementRule (cb [6, 6], cb));
            Assert.IsFalse (bsh.CheckMovementRule (cb [2, 6], cb));
            Assert.IsFalse (bsh.CheckMovementRule (cb [6, 2], cb));

            //Check attempt to capture different color piece
            cb [1, 1].PlacePiece (new Bishop(cb[1, 1], "W"));
            cb [6, 6].PlacePiece (new Bishop(cb[6, 6], "W"));
            cb [2, 6].PlacePiece (new Bishop(cb[2, 6], "W"));
            cb [6, 2].PlacePiece (new Bishop(cb[6, 2], "W"));
            Assert.IsTrue (bsh.CheckMovementRule (cb [1, 1], cb));
            Assert.IsTrue(bsh.CheckMovementRule (cb [6, 6], cb));
            Assert.IsTrue (bsh.CheckMovementRule (cb [2, 6], cb));
            Assert.IsTrue (bsh.CheckMovementRule (cb [6, 2], cb));
        }
        public void TestIfPieceisMoved()
        {
            ChessBoard cb = new ChessBoard ();
            Bishop cpb = new Bishop (cb [4, 4], "B");
            cpb.MovePiece (cb [5, 5], cb);

            Assert.AreEqual (cb [5, 5], cpb.Grid);
        }
        public void TestIfDestinationIsSameGrid()
        {
            ChessBoard cb = new ChessBoard ();
            Bishop cpb = new Bishop (cb [4, 4], "B");

            Assert.IsTrue (cpb.IsDestinationSameGrid(cb[4, 4]));
            Assert.IsFalse (cpb.IsDestinationSameGrid(cb[4, 5]));
        }
        public void TestPieceVerticalPath()
        {
            ChessBoard cb = new ChessBoard ();
            Bishop cpb = new Bishop (cb [4, 4], "B");
            King cpk = new King (cb [3, 3], "W");

            Assert.IsTrue (cpb.CheckHorizontal(cb[3, 2], cb));
            Assert.IsTrue (cpk.CheckVertical(cb[0, 0], cb));
        }
        public void TestPiecePlayer()
        {
            ChessBoard cb = new ChessBoard ();
            Bishop cpb = new Bishop (cb [4, 4], "B");
            King cpk = new King (cb [3, 3], "W");

            Assert.AreEqual ("B", cpb.Player);
            Assert.AreEqual ("W", cpk.Player);
        }
        public void TestPieceName()
        {
            ChessBoard cb = new ChessBoard ();
            Bishop cpb = new Bishop (cb [4, 4], "B");
            King cpk = new King (cb [3, 3], "W");

            Assert.AreEqual ("BB", cpb.Name);
            Assert.AreEqual ("WKG", cpk.Name);
        }
        public void TestPathCheck()
        {
            ChessBoard cb = new ChessBoard ();
            Bishop bsh = new Bishop (cb [4, 4], "B");
            cb [4, 4].PlacePiece (bsh);

            Assert.IsTrue (bsh.CheckDiagonal (cb [1, 1], cb));
            Assert.IsTrue (bsh.CheckDiagonal (cb [6, 6], cb));
            Assert.IsTrue (bsh.CheckDiagonal (cb [2, 6], cb));
            Assert.IsTrue (bsh.CheckDiagonal (cb [6, 2], cb));
        }
        public void TestPieceGrid()
        {
            ChessBoard cb = new ChessBoard ();
            Bishop cpb = new Bishop (cb [4, 4], "B");
            King cpk = new King (cb [3, 3], "W");

            cpk.MovePiece(cb[3, 4], cb);
            cpk.MovePiece(cb[6, 6], cb);

            Assert.AreEqual (cb[4, 4], cpb.Grid);
            Assert.AreEqual (cb[3, 4], cpk.Grid);
        }
Example #9
0
        public override bool Equals(object obj)
        {
            if (obj == null)
            {
                return(false);
            }
            Bishop other = obj as Bishop;

            if (other == null)
            {
                return(false);
            }
            return(this.IsWhite == other.IsWhite);
        }
Example #10
0
        public void TestMovement()
        {
            ChessBoard cb = new ChessBoard ();
            Bishop bsh = new Bishop (cb [4, 4], "B");
            cb [4, 4].PlacePiece (bsh);

            //Test correct movement
            Assert.IsTrue(bsh.CheckMovementRule (cb [6, 6], cb));
            Assert.IsNull (bsh.MovePiece (cb [6, 6], cb));
            Assert.AreSame (bsh.Grid, cb [6, 6]);

            //Test invalid movement
            Assert.IsFalse (bsh.CheckMovementRule (cb[4, 3], cb));
            Assert.AreEqual (bsh.MovePiece (cb [4, 3], cb), "Invalid move");
            Assert.AreSame (bsh.Grid, cb [6, 6]);
        }
Example #11
0
        public void TestPathBlock()
        {
            ChessBoard cb = new ChessBoard ();
            Bishop bsh = new Bishop (cb [4, 4], "B");
            cb [4, 4].PlacePiece (bsh);

            //Create obstructions
            cb [3, 3].PlacePiece (new Bishop(cb[3, 3], "B"));
            cb [5, 5].PlacePiece (new Bishop(cb[5, 5], "B"));
            cb [3, 5].PlacePiece (new Bishop(cb[3, 5], "W"));
            cb [5, 3].PlacePiece (new Bishop(cb[5, 3], "W"));

            Assert.IsFalse (bsh.CheckDiagonal (cb [1, 1], cb));
            Assert.IsFalse(bsh.CheckDiagonal (cb [6, 6], cb));
            Assert.IsFalse (bsh.CheckDiagonal (cb [2, 6], cb));
            Assert.IsFalse (bsh.CheckDiagonal (cb [6, 2], cb));
        }
Example #12
0
        public void FiguresCantSpawnOnOthers()
        {
            var game        = new Game();
            var whiteQueen  = new Queen(Color.White);
            var blackPawn   = new Pawn(Color.Black);
            var blackBishop = new Bishop(Color.Black);

            game.SelectedFigureCoordinates = Tuple.Create(3, 3);
            game.Board[3, 3] = whiteQueen;
            game.Board[4, 3] = blackPawn;
            game.Board[0, 0] = blackBishop;
            game.MoveFigure(Tuple.Create(4, 3));
            game.SelectedFigureCoordinates = Tuple.Create(0, 0);
            game.MoveFigure(Tuple.Create(1, 1));
            game.SpawnFigure(Tuple.Create(1, 1), "Pawn");
            game.SpawnFigure(Tuple.Create(4, 3), "Pawn");
            Assert.AreEqual(game.Board[1, 1].GetType(), blackBishop.GetType());
            Assert.AreEqual(game.Board[4, 3].GetType(), whiteQueen.GetType());
        }
Example #13
0
        /*
         * Initializes the board with a new set of pieces
         */
        public Board(Player p1, Player p2)
        {
            board = new Piece[8, 8];

            //player 1 (white)
            for (int i = 0; i < 8; i++)
            {
                board[6, i] = new Pawn(p1);
            }
            board[7, 0] = new Rook(p1);
            board[7, 1] = new Knight(p1);
            board[7, 2] = new Bishop(p1);
            board[7, 3] = new Queen(p1);
            board[7, 4] = new King(p1);
            board[7, 5] = new Bishop(p1);
            board[7, 6] = new Knight(p1);
            board[7, 7] = new Rook(p1);

            //player 2 (black)
            for (int i = 0; i < 8; i++)
            {
                board[1, i] = new Pawn(p2);
            }
            board[0, 0] = new Rook(p2);
            board[0, 1] = new Knight(p2);
            board[0, 2] = new Bishop(p2);
            board[0, 3] = new King(p2);
            board[0, 4] = new Queen(p2);
            board[0, 5] = new Bishop(p2);
            board[0, 6] = new Knight(p2);
            board[0, 7] = new Rook(p2);

            for (int i = 0; i < 8; i++)
            {
                for (int j = 0; j < 8; j++)
                {
                    if (null == board[i, j])
                    {
                        board[i, j] = new EmptyPiece();
                    }
                }
            }
        }
Example #14
0
        public void BishopMovesCorrectly()
        {
            var game        = new Game();
            var blackBishop = new Bishop(Color.Black);

            game.Board[3, 3] = blackBishop;
            game.SelectedFigureCoordinates = Tuple.Create(3, 3);
            for (int i = 0; i < game.Board.GetLength(0); i++)
            {
                for (int j = 0; j < game.Board.GetLength(1); j++)
                {
                    if (i != 3 && ((i == j) ||
                                   (i + j == 6)))
                    {
                        Assert.IsTrue(blackBishop.CorrectMove(game.SelectedFigureCoordinates, Tuple.Create(i, j)));
                    }
                    else
                    {
                        Assert.IsFalse(blackBishop.CorrectMove(game.SelectedFigureCoordinates, Tuple.Create(i, j)));
                    }
                }
            }
        }
Example #15
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;
            }
        }
Example #16
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);
            }
        }