Ejemplo n.º 1
0
        private bool CheckIfCheck(IBoard gameBoard, Position kingPosition, IDictionary<Position, IFigure> defenderFigures)
        {
            var defenderFiguresPositions = defenderFigures.Keys;
            var counter = 0;

            foreach (var position in defenderFiguresPositions)
            {
                var figure = defenderFigures[position];
                var availableMovements = figure.Move(this.strategy);

                try
                {
                    var tryMove = new Move(position, kingPosition);
                    this.CheckValidMove(figure, availableMovements, tryMove);
                }
                catch (Exception ex)
                {
                    counter++;
                }
            }
            if (counter == defenderFigures.Count)
            {
                return false;
            }

            return true;
        }
Ejemplo n.º 2
0
 public virtual void ValidateMove(IFigure figure, IBoard board, Move move)
 {
     if (figure.IsFirstMove)
        {
        figure.IsFirstMove = false;
        }
 }
Ejemplo n.º 3
0
        public void ValidateMove(IFigure figure, IBoard board, Move move)
        {
            var rowDistance = Math.Abs(move.From.Row - move.To.Row);
            var colDistance = Math.Abs(move.From.Col - move.To.Col);

            if (rowDistance != colDistance)
            {
                throw new InvalidOperationException(string.Format(GlobalConstants.ExceptionMessege, figure.Type));
            }

            int rowDirection = move.From.Row > move.To.Row ? -1 : 1;
            int colDirection = move.From.Col > move.To.Col ? -1 : 1;

            var row = move.From.Row;
            var col = move.From.Col;

            var endRow = move.To.Row + (rowDirection * (-1));
            var endCol = move.To.Col + (colDirection * (-1));

            while (row != endRow && col != endCol)
            {
                row += rowDirection;
                col += colDirection;

                if (board.SeeFigureOnPosition(row, col) != null)
                {
                    throw new InvalidOperationException(string.Format(GlobalConstants.ExceptionMessege, figure.Type));
                }
            }

            base.ValidateMove(figure, board, move);
        }
Ejemplo n.º 4
0
        private bool CheckValidStep(Move move, int directionRow, int directionCol)
        {
            if (move.To.Row == move.From.Row + directionRow && move.To.Col == move.From.Col + directionCol)
            {
                return true;
            }

            return false;
        }
Ejemplo n.º 5
0
        private bool ValidCastleMove(IFigure figure, IBoard board, Move move)
        {
            var whiteLongCastle = CheckCaslte(figure, board, move, FigureColor.White, 7, 2, 1, 4);
            var whiteShortCastle = CheckCaslte(figure, board, move, FigureColor.White, 7, 6, 5, 6);
            var blackLongCastle = CheckCaslte(figure, board, move, FigureColor.Black, 0, 2, 1, 4);
            var blackShortCastle = CheckCaslte(figure, board, move, FigureColor.Black, 0, 6, 5, 6);

            return whiteShortCastle || whiteLongCastle ||
                   blackShortCastle || blackLongCastle;
        }
Ejemplo n.º 6
0
        public void ValidateMove(IFigure figure, IBoard board, Move move)
        {
            var otherColor = figure.Color == FigureColor.White ? FigureColor.Black : FigureColor.White;

            if (!this.ValidMove(move))
            {
                throw new InvalidOperationException(string.Format(GlobalConstants.ExceptionMessege, figure.Type));
            }

            base.ValidateMove(figure, board, move);
        }
Ejemplo n.º 7
0
        public override void ValidateMove(IFigure figure, IBoard board, Move move)
        {
            if (!ValidCastleMove(figure, board, move))
            {
                if (!this.ValidKingMove(figure, board, move))
                {
                    throw new InvalidOperationException(string.Format(GlobalConstants.ExceptionMessege, figure.Type));
                }
            }

            base.ValidateMove(figure, board, move);
        }
Ejemplo n.º 8
0
        public void ValidateMove(IFigure figure, IBoard board, Move move)
        {
            this.startRow = figure.Color == FigureColor.White ? board.Size - 2 : 1;
            this.oppositeColor = figure.Color == FigureColor.White ? FigureColor.Black : FigureColor.White;
            this.direction = (figure.Color == FigureColor.White) ? -1 : 1;

            if (!this.ValidPLayerMoves(move, board))
            {
                throw new InvalidOperationException(string.Format(GlobalConstants.ExceptionMessege, figure.Type));
            }

            base.ValidateMove(figure, board, move);
        }
Ejemplo n.º 9
0
        private bool ValidPLayerMoves(Move move, IBoard board)
        {
            var figureToPosition = board.SeeFigureOnPosition(move.To.Row, move.To.Col);

            if (figureToPosition == null)
            {
                return this.ValidEmptyCellMoves(move, figureToPosition, board);
            }
            else if (figureToPosition.Color == this.oppositeColor)
            {
                if (move.From.Row + this.direction == move.To.Row && (move.From.Col - 1 == move.To.Col || move.From.Col + 1 == move.To.Col))
                {
                    return true;
                }
            }

            return false;
        }
Ejemplo n.º 10
0
        private void CheckIfToPositionSafeKing(IPlayer attacker, IFigure playFigure, Move move, IBoard gameBoard, Position kingPosition, IDictionary<Position, IFigure> defenderFigures)
        {
            var toFigure = gameBoard.SeeFigureOnPosition(move.To.Row, move.To.Col);
            gameBoard.SetFigure(move.To.Row, move.To.Col, playFigure);
            gameBoard.SetFigure(move.From.Row, move.From.Col, null);
            kingPosition = this.FindKingPosition(attacker);

            if (this.CheckIfCheck(gameBoard, kingPosition, defenderFigures))
            {
                gameBoard.SetFigure(move.To.Row, move.To.Col, toFigure);
                gameBoard.SetFigure(move.From.Row, move.From.Col, playFigure);

                throw new InvalidOperationException("Under Chess Save Your King!");
            }

            gameBoard.SetFigure(move.To.Row, move.To.Col, toFigure);
            gameBoard.SetFigure(move.From.Row, move.From.Col, playFigure);
        }
Ejemplo n.º 11
0
        private bool ValidKingMove(IFigure figure, IBoard board, Move move)
        {
            var startRow = move.From.Row;
            var startCol = move.From.Col;

            for (int row = startRow - 1; row <= startRow + 1; row++)
            {
                for (int col = startCol - 1; col <= startCol + 1; col++)
                {
                    if (move.To.Row == row && move.To.Col == col)
                    {
                        return true;
                    }
                }
            }

            return false;
        }
Ejemplo n.º 12
0
        private bool ValidMove(Move move)
        {
            var counter = 0;
            for (int i = 0; i < this.directions.Count; i++)
            {
                var directionRow = this.directions[i][0];
                var directionCol = this.directions[i][1];

                if (!this.CheckValidStep(move, directionRow, directionCol) && !this.CheckValidStep(move, directionRow * (-1), directionCol * (-1)))
                {
                    counter++;
                }
            }

            if (counter == this.directions.Count)
            {
                return false;
            }

            return true;
        }
Ejemplo n.º 13
0
        public override void ValidateMove(IFigure figure, IBoard board, Move move)
        {
            var rowDistance = Math.Abs(move.From.Row - move.To.Row);
            var colDistance = Math.Abs(move.From.Col - move.To.Col);

            if (rowDistance > 0 && colDistance > 0)
            {
                throw new InvalidOperationException(string.Format(GlobalConstants.ExceptionMessege, figure.Type));
            }

            var rowDirection = move.From.Row < move.To.Row ? 1 : -1;
            var colDirection = move.From.Col < move.To.Col ? 1 : -1;

            rowDirection = rowDistance > 0 ? rowDirection : 0;
            colDirection = colDistance > 0 ? colDirection : 0;

            var row = move.From.Row;
            var col = move.From.Col;

            var finalRow = move.To.Row;// + (rowDirection * (-1));
            var finalCol = move.To.Col;// + (colDirection * (-1));

            while (true)
            {
                row += rowDirection;
                col += colDirection;

                if (board.SeeFigureOnPosition(row, col) != null)
                {
                    throw new InvalidOperationException(string.Format(GlobalConstants.ExceptionMessege, figure.Type));
                }

                if (row == finalRow && col == finalCol)
                {
                    break;
                }
            }

            base.ValidateMove(figure, board, move);
        }
Ejemplo n.º 14
0
        private bool CheckCaslte(IFigure figure, IBoard board, Move move, FigureColor color, int row, int col, int startCheck, int endCheck)
        {
            if (figure.Color == color && move.To.Row == row && move.To.Col == col)
            {
                var rook = board.SeeFigureOnPosition(row, 0);

                if (rook.Color == color && rook.IsFirstMove && figure.IsFirstMove)
                {
                    for (int i = startCheck; i < endCheck; i++)
                    {
                        if (board.SeeFigureOnPosition(row, i) != null)
                        {
                            return false;
                        }

                    }

                    return true;
                }
            }

            return false;
        }
Ejemplo n.º 15
0
        private bool ValidEmptyCellMoves(Move move, IFigure figure, IBoard board)
        {
            if (this.startRow == move.From.Row &&
                  move.From.Col == move.To.Col &&
                  move.From.Row + (2 * this.direction) == move.To.Row &&
                  board.SeeFigureOnPosition(move.From.Row + this.direction, move.From.Col) == null)
            {
                return true;
            }

            if (this.startRow == move.From.Row &&
                    move.From.Col == move.To.Col &&
                    move.From.Row + this.direction == move.To.Row)
            {
                return true;
            }

            if (move.From.Row + this.direction == move.To.Row && move.From.Col == move.To.Col && figure == null)
            {
                return true;
            }

            return false;
        }
Ejemplo n.º 16
0
        private void CheckValidMove(IFigure playFigure, ICollection<IMovement> availableMovements, Move move)
        {
            var validMove = false;
            var fountExeprition = new Exception();

            foreach (var movement in availableMovements)
            {
                try
                {
                    movement.ValidateMove(playFigure, this.gameBoard, move);
                    validMove = true;
                    break;
                }
                catch (Exception ex)
                {
                    fountExeprition = ex;
                }
            }

            if (!validMove)
            {
                throw fountExeprition;
            }
        }
Ejemplo n.º 17
0
 private void TakeDefenderFigure(IPlayer attacker, IPlayer defender, Move move, IBoard gameBoard)
 {
     var figure = gameBoard.SeeFigureOnPosition(move.To.Row, move.To.Col);
     if (figure != null)
     {
         attacker.TakeFigure(move.To, figure, defender);
     }
 }
Ejemplo n.º 18
0
        private void MoveRookCastle(Move move, IPlayer attacker)
        {
            var isLongCastle = move.From.Col > move.To.Col;
            var rookRow = move.From.Row;
            var rookFromCol = isLongCastle ? 0 : 7;
            var rookToCol = isLongCastle ? move.From.Col - 1 : move.From.Col + 1;
            var rook = this.gameBoard.SeeFigureOnPosition(rookRow, rookFromCol);

            var rookFromPosition = new Position(rookRow, rookFromCol);
            var rookToPosition = new Position(rookRow, rookToCol);
            var rookMove = new Move(rookFromPosition, rookToPosition);

            this.MoveFigures(rookMove, rook);
            attacker.RemoveFigure(rookMove.From);
            attacker.AddFigure(rookMove.To, rook);
        }
Ejemplo n.º 19
0
 private void MoveFigures(Move move, IFigure playFigure)
 {
     this.gameBoard.GetFigure(move.From.Row, move.From.Col);
     this.gameBoard.SetFigure(move.To.Row, move.To.Col, playFigure);
 }