Ejemplo n.º 1
0
        public void constructor_whitePawn()
        {
            Piece        piece = new WhitePawn(new Point(0, 0), new ChessBoard());
            PieceManager pm    = new PieceManager(piece, new Game());

            Assert.AreEqual(pm.Owner, Players.WhitePlayer);
            Assert.AreEqual(pm.Type, PieceTypes.Pawn);
        }
Ejemplo n.º 2
0
        public void CanMoveTo_correct(int x, int y)
        {
            ChessBoard board = new ChessBoard();

            new BlackPawn(new Point(2, 4), board);
            new BlackPawn(new Point(4, 4), board);
            Piece whitePawn = new WhitePawn(new Point(3, 3), board);

            Assert.IsTrue(whitePawn.canMoveTo(new Point(x, y)));
        }
Ejemplo n.º 3
0
        public void CanMoveTo_false_becouse_King_is_checked(int x, int y)
        {
            ChessBoard board = new ChessBoard();

            new BlackPawn(new Point(2, 4), board);
            new BlackPawn(new Point(4, 4), board);
            new Queen("Black", new Point(3, 0), board);
            Piece whitePawn = new WhitePawn(new Point(3, 3), board);

            Assert.IsFalse(whitePawn.canMoveTo(new Point(x, y)));
        }
Ejemplo n.º 4
0
        public void constructor_correct(int x, int y)
        {
            ChessBoard board     = new ChessBoard();
            Piece      whitePawn = new WhitePawn(new Point(x, y), board);

            Assert.AreEqual(whitePawn.Color, "White");
            Assert.AreEqual(whitePawn.Name, "Pawn");
            Assert.IsTrue(whitePawn is SlowPiece);
            Assert.IsFalse(whitePawn.WasMoved);
            Assert.AreSame(board.GetPiece(new Point(x, y)), whitePawn);
            Assert.AreEqual(whitePawn.Position, new Point(x, y));
        }
Ejemplo n.º 5
0
 public void SetUp()
 {
     _chessBoard  = new ChessBoard();
     _blackBishop = new Bishop(PieceColor.Black, _chessBoard);
     _whiteRook   = new Rook(PieceColor.White, _chessBoard);
     _whitePawn   = new WhitePawn(_chessBoard);
     _blackKing   = new BlackKing(_chessBoard);
     _whiteKing   = new WhiteKing(_chessBoard);
     _blackPawn   = new BlackPawn(_chessBoard);
     _chessBoard.Add(_blackKing, 4, 7);
     _chessBoard.Add(_whiteKing, 4, 0);
 }
Ejemplo n.º 6
0
 public void SetUp()
 {
     _chessBoard  = new ChessBoard();
     _whiteBishop = new Bishop(PieceColor.White, _chessBoard);
     _blackQueen  = new Queen(PieceColor.Black, _chessBoard);
     _blackKnight = new Knight(PieceColor.Black, _chessBoard);
     _whitePawn   = new WhitePawn(_chessBoard);
     _blackKing   = new BlackKing(_chessBoard);
     _whiteKing   = new WhiteKing(_chessBoard);
     _chessBoard.Add(_blackKing, 4, 7);
     _chessBoard.Add(_whiteKing, 4, 0);
 }
Ejemplo n.º 7
0
        public void GenerateLegalMoves()
        {

            Board board = new Board();

            var initialPosition = board.GetCell(6, 2);
  
            var piece = initialPosition.Piece;

            WhitePawn wp = (WhitePawn)piece;
            var list = wp.GenerateLegalMoves();

            Assert.Equal(2, list.Count());
        }
Ejemplo n.º 8
0
        public void IsMoveValid_DestinationIsOutsideTheBoard_ShouldReturnFalse(int x, int y)
        {
            // Arrange
            var movingPlayer  = new Player(PlayerColor.White);
            var waitingPlayer = new Player(PlayerColor.Black);
            var piece         = new WhitePawn(new Position(5, 5));
            var dest          = new Position(x, y);

            // Act
            var result = gameValidationService.ValidateMove(TODO, TODO);

            // Assert
            result.ShouldBe(false);
        }
Ejemplo n.º 9
0
        public void move_for_the_first_time(int x, int y)
        {
            ChessBoard board = new ChessBoard();

            new BlackPawn(new Point(2, 4), board);
            new BlackPawn(new Point(4, 4), board);
            Piece whitePawn = new WhitePawn(new Point(3, 3), board);

            Assert.AreEqual(whitePawn.MoveSet[0], new Point(0, 1));
            Assert.AreEqual(whitePawn.MoveSet[1], new Point(0, 2));
            Assert.IsFalse(whitePawn.WasMoved);
            whitePawn.moveTo(new Point(x, y));
            Assert.AreEqual(whitePawn.MoveSet[0], new Point(0, 1));
            Assert.IsTrue(whitePawn.WasMoved);
        }
Ejemplo n.º 10
0
        public void IsMoveValid_MovingPlayerTriesToMoveOpponentsPiece_ShouldReturnFalse()
        {
            // Arrange
            var movingPlayer  = new Player(PlayerColor.White);
            var waitingPlayer = new Player(PlayerColor.Black);

            var piece = new WhitePawn(new Position(1, 1));

            waitingPlayer.Pieces.Add(piece);

            var dest = new Position(2, 2);

            // Act
            var result = gameValidationService.ValidateMove(TODO, TODO);

            // Assert
            result.ShouldBe(false);
        }
Ejemplo n.º 11
0
        public void IsMoveValid_UniDirectionalBlackPieceMovesUpTheBoard_ShouldReturnFalse()
        {
            // Arrange
            var movingPlayer  = new Player(PlayerColor.Black);
            var waitingPlayer = new Player(PlayerColor.White);

            var piece = new WhitePawn(new Position(1, 1));

            movingPlayer.Pieces.Add(piece);

            var dest = new Position(1, 0);

            // Act
            var result = gameValidationService.ValidateMove(TODO, TODO);

            // Assert
            result.ShouldBe(false);
        }
Ejemplo n.º 12
0
        public void IsMoveValid_PawnTriesToMoveDiagonallyButNoOpponentPieceIsThereToTake_ShouldReturnFalse()
        {
            // Arrange
            var movingPlayer  = new Player(PlayerColor.White);
            var waitingPlayer = new Player(PlayerColor.Black);

            var pawn = new WhitePawn(new Position(1, 1));
            var king = new King(new Position(7, 7));

            movingPlayer.Pieces.Add(pawn);
            movingPlayer.Pieces.Add(king);

            var dest = new Position(0, 0);

            // Act
            var result = gameValidationService.ValidateMove(TODO, TODO);

            // Assert
            result.ShouldBe(false);
        }
Ejemplo n.º 13
0
        public void IsMoveValid_PathIsBlockedByEnemyPiece_ShouldReturnFalse()
        {
            // Arrange
            var movingPlayer  = new Player(PlayerColor.White);
            var waitingPlayer = new Player(PlayerColor.Black);

            var pieceSource   = new Queen(new Position(1, 1));
            var blockingPiece = new WhitePawn(new Position(2, 2));

            movingPlayer.Pieces.Add(pieceSource);
            waitingPlayer.Pieces.Add(blockingPiece);

            var dest = new Position(3, 3);

            // Act
            var result = gameValidationService.ValidateMove(TODO, TODO);

            // Assert
            result.ShouldBe(false);
        }
Ejemplo n.º 14
0
        public void IsMoveValid_DestinationIsNotOnAPossiblePathFor_Pawn_ShouldReturnFalse()
        {
            // Arrange
            var movingPlayer  = new Player(PlayerColor.White);
            var waitingPlayer = new Player(PlayerColor.Black);

            var pieceSource      = new WhitePawn(new Position(1, 1));
            var pieceDestination = new WhitePawn(new Position(4, 2));

            movingPlayer.Pieces.Add(pieceSource);
            waitingPlayer.Pieces.Add(pieceDestination);

            var dest = new Position(4, 2);

            // Act
            var result = gameValidationService.ValidateMove(TODO, TODO);

            // Assert
            result.ShouldBe(false);
        }
Ejemplo n.º 15
0
        public void IsMoveValid_DestinationIsOccupiedByFriendlyPiece_ShouldReturnFalse()
        {
            // Arrange
            var movingPlayer  = new Player(PlayerColor.White);
            var waitingPlayer = new Player(PlayerColor.Black);

            var pieceSource      = new Queen(new Position(1, 1));
            var pieceDestination = new WhitePawn(new Position(2, 2));

            movingPlayer.Pieces.Add(pieceSource);
            movingPlayer.Pieces.Add(pieceDestination);

            var dest = new Position(2, 2);

            // Act
            var result = gameValidationService.ValidateMove(TODO, TODO);

            // Assert
            result.ShouldBe(false);
        }
Ejemplo n.º 16
0
        /// <summary>
        /// Gets a board in the starting position.
        /// </summary>
        /// <returns></returns>
        public static Board GetStartingBoard()
        {
            var squares = new Piece[SquareNo];
            var status  = new BoardStatus();

            squares[A8] = new BlackRook();
            squares[B8] = new BlackKnight();
            squares[C8] = new BlackBishop();
            squares[D8] = new BlackQueen();
            squares[E8] = new BlackKing();
            squares[F8] = new BlackBishop();
            squares[G8] = new BlackKnight();
            squares[H8] = new BlackRook();

            for (var sqIndex = SideSquareNo; sqIndex < SideSquareNo * 2; sqIndex++)
            {
                squares[sqIndex] = new BlackPawn();
            }

            for (var sqIndex = SideSquareNo * (SideSquareNo - 2); sqIndex < SideSquareNo * (SideSquareNo - 1); sqIndex++)
            {
                squares[sqIndex] = new WhitePawn();
            }

            squares[A1] = new WhiteRook();
            squares[B1] = new WhiteKnight();
            squares[C1] = new WhiteBishop();
            squares[D1] = new WhiteQueen();
            squares[E1] = new WhiteKing();
            squares[F1] = new WhiteBishop();
            squares[G1] = new WhiteKnight();
            squares[H1] = new WhiteRook();

            status.WhiteTurn            = true;
            status.WhiteCouldCastleLong = status.WhiteCouldCastleShort = status.BlackCouldCastleLong = status.BlackCouldCastleShort = true;
            status.EnPassantTarget      = null;
            status.Ply   = 0;
            status.Moves = 1;

            return(new Board(squares, status));
        }
Ejemplo n.º 17
0
        public void IsMoveValid_PieceMovesAndBlocksCheckToOwnKing_ShouldReturnTrue()
        {
            // Arrange
            var movingPlayer  = new Player(PlayerColor.White);
            var waitingPlayer = new Player(PlayerColor.Black);

            var king          = new King(new Position(0, 0));
            var saviorPiece   = new WhitePawn(new Position(2, 1));
            var checkingPiece = new Queen(new Position(2, 2));

            movingPlayer.Pieces.Add(king);
            movingPlayer.Pieces.Add(saviorPiece);
            waitingPlayer.Pieces.Add(checkingPiece);

            var dest = new Position(1, 1);

            // Act
            var result = gameValidationService.ValidateMove(TODO, TODO);

            // Assert
            result.ShouldBe(true);
        }
Ejemplo n.º 18
0
        public void IsMoveValid_PawnTriesToMoveForwardButFriendlyPieceBlocksIt_ShouldReturnFalse(int destX, int destY, int friendlyPawnX, int friendlyPawnY)
        {
            // Arrange
            var movingPlayer  = new Player(PlayerColor.White);
            var waitingPlayer = new Player(PlayerColor.Black);

            var friendlyPiece = new BlackPawn(new Position(friendlyPawnX, friendlyPawnY));
            var movingPawn    = new WhitePawn(new Position(2, 0));
            var king          = new King(new Position(7, 7));

            movingPlayer.Pieces.Add(movingPawn);
            movingPlayer.Pieces.Add(king);
            movingPlayer.Pieces.Add(friendlyPiece);

            var dest = new Position(destX, destY);

            // Act
            var result = gameValidationService.ValidateMove(TODO, TODO);

            // Assert
            result.ShouldBe(false);
        }
Ejemplo n.º 19
0
    public void UndoMove()
    {
        Movement LastMove = allMoves[allMoves.Count - 1];

        if (LastMove.MovedTo.isSpecial)
        {
            if (LastMove.PieceMoved is King)
            {
                if (LastMove.MovedTo.theValidMove.indCol == 2)
                {
                    MovePieceTo(this.AllSquares[LastMove.PieceMoved.SquareOfPiece.indRow, 3].PieceInSquare, this.AllSquares[LastMove.PieceMoved.SquareOfPiece.indRow, 0]);
                    this.AllSquares[LastMove.PieceMoved.SquareOfPiece.indRow, 0].PieceInSquare.movesMade -= 2;
                }
                else
                {
                    MovePieceTo(this.AllSquares[LastMove.PieceMoved.SquareOfPiece.indRow, 5].PieceInSquare, this.AllSquares[LastMove.PieceMoved.SquareOfPiece.indRow, 7]);
                    this.AllSquares[LastMove.PieceMoved.SquareOfPiece.indRow, 7].PieceInSquare.movesMade -= 2;
                }
            }
            else if (LastMove.PieceMoved is WhitePawn)
            {
                GameObject tempObject    = LastMove.MovedTo.theValidMove.PieceInSquare.gameObject;
                int        tempMovesMade = LastMove.MovedTo.theValidMove.PieceInSquare.movesMade;

                DestroyImmediate(LastMove.MovedTo.theValidMove.PieceInSquare);
                LastMove.MovedTo.theValidMove.PieceInSquare = null;

                WhitePawn ReAddedPiece = tempObject.AddComponent <WhitePawn>();
                LastMove.MovedTo.theValidMove.PieceInSquare.gameObject.GetComponentInChildren <RawImage>().texture = Resources.Load("Pawns/whitepawn") as Texture;

                ReAddedPiece.Initialize(true, tempMovesMade);
            }
            else if (LastMove.PieceMoved is BlackPawn)
            {
                GameObject tempObject    = LastMove.MovedTo.theValidMove.PieceInSquare.gameObject;
                int        tempMovesMade = LastMove.MovedTo.theValidMove.PieceInSquare.movesMade;

                DestroyImmediate(LastMove.MovedTo.theValidMove.PieceInSquare);
                LastMove.MovedTo.theValidMove.PieceInSquare = null;

                BlackPawn ReAddedPiece = tempObject.AddComponent <BlackPawn>();
                LastMove.MovedTo.theValidMove.PieceInSquare.gameObject.GetComponentInChildren <RawImage>().texture = Resources.Load("Pawns/blackpawn") as Texture;

                ReAddedPiece.Initialize(false, tempMovesMade);
            }
        }



        LastMove.MovedTo.theValidMove.PieceInSquare.transform.SetParent(LastMove.MovedFrom.gameObject.transform, false);
        LastMove.MovedFrom.PieceInSquare = LastMove.MovedTo.theValidMove.PieceInSquare;
        LastMove.MovedFrom.SetPieceCoordinates();
        LastMove.MovedTo.theValidMove.PieceInSquare.SetSquareOfPiece();
        LastMove.MovedTo.theValidMove.PieceInSquare.movesMade -= 1;
        LastMove.MovedTo.theValidMove.PieceInSquare            = null;


        if (LastMove.CapturedPiece != null)
        {
            LastMove.CapturedPiece.transform.SetParent(LastMove.CapturedPiece.SquareOfPiece.transform, false);
            LastMove.CapturedPiece.GetComponent <Canvas>().enabled      = true;
            LastMove.CapturedPiece.GetComponent <Canvas>().sortingOrder = 1;
            LastMove.CapturedPiece.SquareOfPiece.PieceInSquare          = LastMove.CapturedPiece;
        }

        allMoves.RemoveAt(allMoves.Count - 1);
        --turnNumber;

        AllowSquareSelection   = false;
        CurrentPieceSelection  = null;
        CurrentSquareSelection = null;
        foreach (Square j in this.AllSquares)
        {
            j.GetComponentInParent <Image>().color = new Color(j.GetComponentInParent <Image>().color.r,
                                                               j.GetComponentInParent <Image>().color.g,
                                                               j.GetComponentInParent <Image>().color.b,
                                                               1
                                                               );
        }



        if (turnNumber == 1)
        {
            UndoBtn.SetActive(false);
        }
        if (isWhiteTurn)
        {
            isWhiteTurn = false;
            whoseTurnText.GetComponent <Text>().text = "Black's turn";
        }
        else
        {
            isWhiteTurn = true;
            whoseTurnText.GetComponent <Text>().text = "White's turn";
        }


        stateOfGame = CheckSituationOfBoard();
        //  StateOfGame = CheckSituationOfBoard();
    }
Ejemplo n.º 20
0
 public void constructor_throw_ArgumentException(int x, int y)
 {
     ChessBoard board     = new ChessBoard();
     Piece      whitePawn = new WhitePawn(new Point(x, y), board);
 }