Exemplo 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;
        }
Exemplo n.º 2
0
 private void CheckIfToPositionIsTakenByAttacker(IPlayer attacker, Position to)
 {
     var figureAtPosition = this.gameBoard.SeeFigureOnPosition(to.Row, to.Col);
     if (figureAtPosition != null && figureAtPosition.Color == attacker.Color)
     {
         throw new InvalidOperationException("You already have figure on that position!");
     }
 }
Exemplo n.º 3
0
        public static Move ParseCommand(string command, IBoard board)
        {
            var parsedCommand = command.Split(new[] { '-' }, StringSplitOptions.RemoveEmptyEntries);
            var currentPositionRow = GetPositionValue(parsedCommand[0][1], board);
            var currentPositionCol = GetPositionValue(parsedCommand[0][0], board);
            var from = new Position(currentPositionRow, currentPositionCol);

            var nextPositionRow = GetPositionValue(parsedCommand[1][1], board);
            var nextPositionCol = GetPositionValue(parsedCommand[1][0], board);
            var to = new Position(nextPositionRow, nextPositionCol);

            return new Move(from, to);
        }
Exemplo n.º 4
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);
        }
Exemplo n.º 5
0
 public void AddFigure(Position position, IFigure figure)
 {
     if (!this.figures.Keys.Contains(position))
     {
         this.figures.Add(position, figure);
     }
 }
Exemplo n.º 6
0
        private IDictionary<Position, IFigure> GetStarGameFifures()
        {
            var figures = new Dictionary<Position, IFigure>();
            var figureIndex = 0;
            var direction = this.Color == FigureColor.White ? -1 : 1;
            var currentRow = this.Color == FigureColor.White ? GlobalConstants.StandatdBoardGameSize - 1 : 0;

            for (int row = 0; row < GlobalConstants.InitialRowsWithFigures; row++)
            {
                for (int col = 0; col < GlobalConstants.StandatdBoardGameSize; col++)
                {
                    var currentPositon = new Position(currentRow, col);
                    var currentFigure = (IFigure)Activator.CreateInstance(GlobalConstants.StartFigureOrderStandartGame[figureIndex], this.Color);
                    figures.Add(currentPositon, currentFigure);
                    figureIndex++;
                }

                currentRow += direction;
            }

            return figures;
        }
Exemplo n.º 7
0
 public void TakeFigure(Position position, IFigure figure, IPlayer secondPlayer)
 {
     secondPlayer.RemoveFigure(position);
     this.AddFigure(position, figure);
     this.TakenFigures.Add(figure);
 }
Exemplo n.º 8
0
 public void RemoveFigure(Position position)
 {
     if (this.figures.Keys.Contains(position))
     {
         this.figures.Remove(position);
     }
 }
Exemplo n.º 9
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);
        }
Exemplo n.º 10
0
 private void CheckPlayerContainsFigure(IPlayer attacker, Position position)
 {
     if (!attacker.Figures.Keys.Contains(position))
     {
         throw new ArgumentException("Figure is not yours!");
     }
 }