public override void WinnginConditions()
        {
            for (int i = 0; i < this.Board.TotalCols; i++)
            {
                try
                {
                    var figure = this.Board.GetFigureAtPosition(new Position(this.Board.TotalRows, (char)('a' + i)));
                    if (figure is King)
                    {
                        this.GameState = GameState.WhiteWon;
                        break;
                    }
                }
                catch (Exception)
                {
                }
            }

            var kingPosition = this.Board.GetKingPosition(ChessColor.White);

            var toUpLeft = new Position(kingPosition.Row + 1, (char)(kingPosition.Col - 1));
            var toUpRight = new Position(kingPosition.Row + 1, (char)(kingPosition.Col + 1));
            var toDownLeft = new Position(kingPosition.Row - 1, (char)(kingPosition.Col - 1));
            var toDownRight = new Position(kingPosition.Row - 1, (char)(kingPosition.Col + 1));

            if (!(this.CheckKingToOptions(toUpLeft) ||
                this.CheckKingToOptions(toUpRight) ||
                this.CheckKingToOptions(toDownLeft) ||
                this.CheckKingToOptions(toDownRight)))
            {
                this.GameState = GameState.BlackWon;
            }
        }
 private void AddKingToBoardRow(IPlayer player, IBoard board, int chessRow, char chessCol)
 {
     var figureInstance = new King(player.Color);
         player.AddFigure(figureInstance);
         var position = new Position(chessRow, chessCol);
         board.AddFigure(figureInstance, position);
 }
        public void AddFigureToBoard(IPlayer firstPlayser, IPlayer secondPlayer, IBoard board, string fen)
        {
            var splitedFen = fen.Split(new[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
            var index = 0;

            for (int row = splitedFen.Length - 1; row >= 0; row--)
            {
                var currentRow = this.MakeRow(splitedFen[row]);

                for (int col = 0; col < currentRow.Length; col++)
                {
                    if (currentRow[col] == Pown)
                    {
                        var pawn = new Pawn(secondPlayer.Color);
                        secondPlayer.AddFigure(pawn);
                        var position = new Position(index + 1, (char)(col + 'a'));
                        board.AddFigure(pawn, position);
                    }
                    else if (currentRow[col] == King)
                    {
                        var figureInstance = new King(firstPlayser.Color);
                        firstPlayser.AddFigure(figureInstance);
                        var position = new Position(index + 1, (char)(col + 'a'));
                        board.AddFigure(figureInstance, position);
                    }
                }

                index++;
            }
        }
        private void AddPawnsToBoardRow(IPlayer player, IBoard board, int chessRow)
        {
            for (int i = 0; i < GlobalConstants.StandartGameTotalBoardCols - 2; i += 2)
            {
                var pawn = new Pawn(player.Color);
                player.AddFigure(pawn);
                var position = new Position(chessRow, (char)(i + 'a'));
                board.AddFigure(pawn, position);
            }

            // TODO: Remove
            //// For testing only
            //var pawn = new Pawn(player.Color);
            //player.AddFigure(pawn);
            //var position = new Position(8, (char)(0 + 'a'));
            //board.AddFigure(pawn, position);

            //pawn = new Pawn(player.Color);
            //player.AddFigure(pawn);
            //position = new Position(8, (char)(2 + 'a'));
            //board.AddFigure(pawn, position);

            //pawn = new Pawn(player.Color);
            //player.AddFigure(pawn);
            //position = new Position(8, (char)(4 + 'a'));
            //board.AddFigure(pawn, position);

            //pawn = new Pawn(player.Color);
            //player.AddFigure(pawn);
            //position = new Position(6, (char)(4 + 'a'));
            //board.AddFigure(pawn, position);
        }
        public static Position FromChessCoordinates(int chessRow, char chessCol)
        {
            var newPosition = new Position(chessRow, chessCol);
            ChechIfValid(newPosition);

            return newPosition;
        }
        public IFigure GetFigureAtPosition(Position position)
        {
            int arrRow = this.GetArrayRow(position.Row);
            int arrCol = this.GetArrayCol(position.Col);

            return this.board[arrRow, arrCol];
        }
        public void AddFigure(IFigure figure, Position position)
        {
            ObjectValidator.CheckIfObjectIsNull(figure, GlobalErrorMessages.NullFigureErrorMessage);

            Position.ChechIfValid(position);

            int arrRow = this.GetArrayRow(position.Row);
            int arrCol = this.GetArrayCol(position.Col);
            this.board[arrRow, arrCol] = figure;
        }
 private void AddPawnsToBoardRow(IPlayer player, IBoard board, int chessRow)
 {
     for (int i = 0; i < GlobalConstants.StandartGameTotalBoardCols; i++)
     {
         var pawn = new Pawn(player.Color);
         player.AddFigure(pawn);
         var position = new Position(chessRow, (char)(i + 'a'));
         board.AddFigure(pawn, position);
     }
 }
 private void AddArmyToBoardRow(IPlayer player, IBoard board, int chessRow)
 {
     for (int i = 0; i < GlobalConstants.StandartGameTotalBoardCols; i++)
     {
         var figureType = this.figureTypes[i];
         var figureInstance = (IFigure)Activator.CreateInstance(figureType, player.Color);
         player.AddFigure(figureInstance);
         var position = new Position(chessRow, (char)(i + 'a'));
         board.AddFigure(figureInstance, position);
     }
 }
        public static void ChechIfValid(Position position)
        {
            if (position.Row < GlobalConstants.MinimumRowValueOnBoard
                || position.Row > GlobalConstants.MaximumRowValueOnBoard)
            {
                throw new IndexOutOfRangeException("Selected row position is not valid");
            }

            if (position.Col < GlobalConstants.MinimumColValueOnBoard
                || position.Col > GlobalConstants.MaximumColValueOnBoard)
            {
                throw new IndexOutOfRangeException("Selected col position is not valid");
            }
        }
        public void CheckIfSquareIsFree(IFigure figure, Position position)
        {
            try
            {
                ObjectValidator.CheckIfObjectIsNull(figure, GlobalErrorMessages.NullFigureErrorMessage);

                Position.ChechIfValid(position);

            }
            catch (Exception)
            {
                throw new ArgumentException(GlobalErrorMessages.InvalidMove);
            }
        }
        private bool CheckKingToOptions(Position to)
        {
            var chessColor = ChessColor.White;
            var king = new King(chessColor);

            try
            {
                this.CheckIfToPositionIsEmpty(king, to);
            }
            catch (Exception)
            {
                return false;
                throw;
            }

            return true;
        }
        public Position GetKingPosition(ChessColor color)
        {
            var currentRow = 0;
            var currentCol = (char)('a');

            for (int row = 0; row < this.TotalCols; row++)
            {
                for (int col = 0; col < this.TotalRows; col++)
                {
                    currentRow = row;
                    currentCol = (char)('a' + col);
                    var position = new Position(currentRow, currentCol);
                    var figureAtPosition = this.GetFigureAtPosition(position);
                    if (figureAtPosition is Figures.King && figureAtPosition.Color == color)
                    {
                        return new Position(currentRow, currentCol);
                    }
                }
            }

            return new Position(currentRow, currentCol);
        }
        private bool CheckOtherFigureIfValid(IBoard board, Position to, ChessColor color)
        {
            var otherFigure = board.GetFigureAtPosition(to);
            if (otherFigure != null && otherFigure.Color == color)
            {
                return false;
            }

            return true;
        }
        public void GameEngine(string gameId, string userId, string moveFrom, string moveTo)
        {
            var game = this.GetGame(gameId);
            var playerID = userId;
            var players = this.GetPlayers();
            var firstPlayer = players
                .FirstOrDefault(x => x.Id == game.FirstPlayerId);
            var secondPlayer = players
                .FirstOrDefault(x => x.Id == game.SecondPlayerId);
            var oldFen = game.Board;

            // if game doesn't exist
            if (game == null)
            {
                return;
            }

            if (firstPlayer == null || secondPlayer == null)
            {
                return;
            }

            // nothing to move
            if (moveFrom == moveTo)
            {
                return;
            }

            // check If player is 1 or 2
            if (playerID != game.FirstPlayerId && playerID != game.SecondPlayerId)
            {
                // return you are not part of this game
                this.Clients.Group(gameId).move(oldFen);
                return;
            }

            if (playerID == game.FirstPlayerId && game.State != KingSurvivalGameState.TurnKing)
            {
                // this is not your turn
                this.Clients.Group(gameId).move(oldFen);
                return;
            }

            if (playerID == game.SecondPlayerId && game.State != KingSurvivalGameState.TurnPown)
            {
                // this is not your turn
                this.Clients.Group(gameId).move(oldFen);
                return;
            }

            var renderer = new WebRenderer(this.Clients, game, this.data);
            var furstPlayer = new Player(firstPlayer.UserName, ChessColor.White);
            var scondPlayer = new Player(secondPlayer.UserName, Chess.Common.ChessColor.White);
            var kingSurvivalInitilizeStrategy = new KingSurvivalGameWebInitializationStrategy(oldFen, furstPlayer, scondPlayer);
            var from = new Position(moveFrom[1] - '0', moveFrom[0]);
            var to = new Position(moveTo[1] - '0', moveTo[0]);
            var move = new Move(from, to);
            var inputProvider = new WebInputProvider(move);
            var movementStrategy = new KingSurvivalMovementStrategy();

            var gameEngine = new KingSurvivalEngineWeb(renderer, inputProvider, movementStrategy);

            gameEngine.Initialize(kingSurvivalInitilizeStrategy);
            gameEngine.Play();
        }
 private bool CheckDiagonalMove(Position from, Position to)
 {
     return from.Col + 1 == to.Col || from.Col - 1 == to.Col;
 }
        public void RemoveFigure(Position position)
        {
            Position.ChechIfValid(position);

            int arrRow = this.GetArrayRow(position.Row);
            int arrCol = this.GetArrayCol(position.Col);
            this.board[arrRow, arrCol] = null;
        }
        public void MoveFigureAtPosition(IFigure figure, Position from, Position to)
        {
            int arrFromRow = this.GetArrayRow(from.Row);
            int arrFromCol = this.GetArrayCol(from.Col);
            this.board[arrFromRow, arrFromCol] = null;

            int arrToRow = this.GetArrayRow(to.Row);
            int arrToCol = this.GetArrayCol(to.Col);
            this.board[arrToRow, arrToCol] = figure;
        }
        protected void CheckIfPlayerOwnsFigure(IPlayer player, IFigure figure, Position from)
        {
            if (figure == null)
            {
                throw new InvalidOperationException(string.Format("Position {0}{1} is empty", from.Col, from.Row));
            }

            if (figure.Color != player.Color)
            {
                throw new InvalidOperationException(
                    string.Format("Figure at this position {0}{1} is not yours", from.Col, from.Row));
            }
        }
 protected void CheckIfToPositionIsEmpty(IFigure figure, Position to)
 {
     var figureAtPosition = this.Board.GetFigureAtPosition(to);
     if (figureAtPosition != null && figureAtPosition.Color == figure.Color)
     {
         throw new InvalidOperationException(
            string.Format("You already have a figure at {0}{1}!", to.Col, to.Row));
     }
 }