Exemplo n.º 1
0
        private void CastlingDecoratorChanges(IPiece piece, Position position, ChessboardModel chessboard)
        {
            var strategyFacctory = new CheckStrategyFactory();

            switch (piece.Kind)
            {
            case KindOfPiece.Knight:
                piece.CheckStrategy = strategyFacctory.Create(piece.Kind, false);
                if (chessboard[0, position.Y].Piece?.Kind == KindOfPiece.Rook)
                {
                    chessboard[0, position.Y].Piece.CheckStrategy = strategyFacctory.Create(KindOfPiece.Rook, false);
                }
                if (chessboard[7, position.Y].Piece?.Kind == KindOfPiece.Rook)
                {
                    chessboard[7, position.Y].Piece.CheckStrategy = strategyFacctory.Create(KindOfPiece.Rook, false);
                }
                break;

            case KindOfPiece.Rook:
                piece.CheckStrategy = strategyFacctory.Create(piece.Kind, false);
                if (position.X == 0)
                {
                    chessboard[4, position.Y].Piece.CheckStrategy = strategyFacctory.CreateKingStrategyWithCastlingDecorator(TypeOfCastling.Short);
                }
                else if (position.X == 7)
                {
                    chessboard[4, position.Y].Piece.CheckStrategy = strategyFacctory.CreateKingStrategyWithCastlingDecorator(TypeOfCastling.Long);
                }
                break;

            default: throw new ApplicationException("unexpected argument");
            }
        }
Exemplo n.º 2
0
        public void Move(Position firstPosition, Position secondPosition, ChessboardModel chessboard)
        {
            Field firstField  = chessboard[firstPosition];
            Field secondField = chessboard[secondPosition];

            IsCastlingDecoratorChange(firstPosition, secondPosition, chessboard);

            if (secondField.Piece == null)
            {
                NormalMove(firstField, secondField);
            }
            else if (firstField.Piece.Kind == KindOfPiece.King &&
                     firstPosition.X == 4 &&
                     (secondPosition.X == 2 || secondPosition.X == 6))
            {
                Castling(firstField, secondField, secondPosition, chessboard);
            }
            else if (firstField.Piece.Colour != secondField.Piece.Colour)
            {
                Attack(firstField, secondField);
            }
            else
            {
                throw new ApplicationException("unexpected state");
            }
        }
Exemplo n.º 3
0
        public IBoard Create(TypeOfGame kind)
        {
            IPieceFactory pieceFactory = new ChessPieceFactory();

            switch (kind)
            {
            case TypeOfGame.Chess:

                IBoard chessboard = new ChessboardModel(new ChessMovie());

                //a1 - h1
                chessboard.Fields[0, 0].Piece = pieceFactory.Create(ColourOfPiece.White, KindOfPiece.Rook);
                chessboard.Fields[1, 0].Piece = pieceFactory.Create(ColourOfPiece.White, KindOfPiece.Knight);
                chessboard.Fields[2, 0].Piece = pieceFactory.Create(ColourOfPiece.White, KindOfPiece.Bishop);
                chessboard.Fields[3, 0].Piece = pieceFactory.Create(ColourOfPiece.White, KindOfPiece.Queen);
                chessboard.Fields[4, 0].Piece = pieceFactory.Create(ColourOfPiece.White, KindOfPiece.King);
                chessboard.Fields[5, 0].Piece = pieceFactory.Create(ColourOfPiece.White, KindOfPiece.Bishop);
                chessboard.Fields[6, 0].Piece = pieceFactory.Create(ColourOfPiece.White, KindOfPiece.Knight);
                chessboard.Fields[7, 0].Piece = pieceFactory.Create(ColourOfPiece.White, KindOfPiece.Rook);
                //a2 - h2
                for (int i = 0; i < 8; i++)
                {
                    chessboard.Fields[i, 1].Piece = pieceFactory.Create(ColourOfPiece.White, KindOfPiece.Pawn);
                }
                //a7 - h7
                for (int i = 0; i < 8; i++)
                {
                    chessboard.Fields[i, 6].Piece = pieceFactory.Create(ColourOfPiece.Black, KindOfPiece.Pawn);
                }
                //a8 - h8
                chessboard.Fields[0, 6].Piece = pieceFactory.Create(ColourOfPiece.Black, KindOfPiece.Rook);
                chessboard.Fields[1, 6].Piece = pieceFactory.Create(ColourOfPiece.Black, KindOfPiece.Knight);
                chessboard.Fields[2, 6].Piece = pieceFactory.Create(ColourOfPiece.Black, KindOfPiece.Bishop);
                chessboard.Fields[3, 6].Piece = pieceFactory.Create(ColourOfPiece.Black, KindOfPiece.Queen);
                chessboard.Fields[4, 6].Piece = pieceFactory.Create(ColourOfPiece.Black, KindOfPiece.King);
                chessboard.Fields[5, 6].Piece = pieceFactory.Create(ColourOfPiece.Black, KindOfPiece.Bishop);
                chessboard.Fields[6, 6].Piece = pieceFactory.Create(ColourOfPiece.Black, KindOfPiece.Knight);
                chessboard.Fields[7, 6].Piece = pieceFactory.Create(ColourOfPiece.Black, KindOfPiece.Rook);

                return(chessboard);

            case TypeOfGame.Checkers:
                return(null);

            default:
                return(null);
            }
        }
Exemplo n.º 4
0
 private void ChangeRookPosition(Position kingPosition, ChessboardModel chessboard)
 {
     if (kingPosition.X == 2)
     {
         chessboard[3, kingPosition.Y].Piece = chessboard[0, kingPosition.Y].Piece;
         chessboard[0, kingPosition.Y].Piece = null;
     }
     else if (kingPosition.X == 6)
     {
         chessboard[5, kingPosition.Y].Piece = chessboard[7, kingPosition.Y].Piece;
         chessboard[7, kingPosition.Y].Piece = null;
     }
     else
     {
         throw new ApplicationException("unexpected staitment");
     }
 }
Exemplo n.º 5
0
        private void Castling(Field firstField, Field secondField, Position kingPosition, ChessboardModel chessboard)
        {
            secondField.Piece = firstField.Piece;
            firstField.Piece  = null;

            ChangeRookPosition(kingPosition, chessboard);
        }
Exemplo n.º 6
0
        private void IsCastlingDecoratorChange(Position firstPosition, Position secondPosition, ChessboardModel chessboard)
        {
            Field firstField  = chessboard[firstPosition];
            Field secondField = chessboard[secondPosition];

            if (secondField.Piece?.CheckStrategy is IFirstMove)
            {
                CastlingDecoratorChanges(secondField.Piece, secondPosition, chessboard);
            }
            if (firstField.Piece.CheckStrategy is IFirstMove)
            {
                CastlingDecoratorChanges(firstField.Piece, firstPosition, chessboard);
            }
        }