Example #1
0
 public Pawn(Figure fig)
 {
     this.pos = new PositionModel()
     {
         Row = fig.PositionRow,
         Col = fig.PositionCol
     };
     isWhite = fig.IsWhite;
     IsMoved = fig.IsMoved;
 }
Example #2
0
        public static FigureModel ConvertToModel(Figure figure)
        {
            return new FigureModel()
            {
                Id = figure.Id,
                PosRow = figure.PositionRow,
                PosCol = figure.PositionCol,
                Type = figure.FigureType.TypeName,
                IsWhite=figure.IsWhite,
                IsMoved=figure.IsMoved

            };
        }
 public static IFigure GetFigure(Figure figure)
 {
     var type=figure.FigureType.TypeName.ToLower().Trim();
     switch (type)
     {
         case "pawn":
             return new Pawn(figure);
         case "king":
             return new King(figure);
         case "queen":
             return new Queen(figure);
         case "rook":
             return new Rook(figure);
         case "bishop":
             return new Bishop(figure);
         case "knight":
             return new Knight(figure);
         default:
             throw new InvalidOperationException("Unknown figure");
     }
 }
        private bool ValidateMove(Figure figure, int toRow, int toCol)
        {
            PositionModel toPosition = new PositionModel() { Row = toRow, Col = toCol };
            PositionModel fromPosition = new PositionModel() { Row = figure.PositionRow, Col = figure.PositionCol };

            if (toRow > 8 || toCol > 8)
            {
                return false;
            }

            if (toRow < 0 || toCol < 0)
            {
                return false;
            }

            var currentFigure = FigureFactory.GetFigure(figure);

            List<PositionModel> possibleMoves = currentFigure.GetPossibleMoves();
            List<PositionModel> possibleHits = currentFigure.GetPossibleHits();

            var checkedPosMoves = possibleMoves.Find(x => x.Col == toPosition.Col && x.Row == toPosition.Row);
            var checkedPosHits = possibleHits.Find(x => x.Col == toPosition.Col && x.Row == toPosition.Row);

            if (checkedPosMoves == null && checkedPosHits == null)
            {
                return false;
            }

            if (!currentFigure.CanJump())
            {
                FigureRepository figureRepository = data.GetFigureRepository();

                HashSet<PositionModel> movePath = GetMovePath(fromPosition, toPosition);
                IQueryable<Figure> allGameFigures = figureRepository.GetGameFigures(figure.GameId);

                HashSet<PositionModel> gameFiguresPositions = new HashSet<PositionModel>();
                foreach (Figure gameFig in allGameFigures)
                {
                    gameFiguresPositions.Add(new PositionModel()
                    {
                        Row = gameFig.PositionRow,
                        Col = gameFig.PositionCol
                    });
                }

                var positionIntersection = movePath.Intersect<PositionModel>(gameFiguresPositions);

                if (positionIntersection.Count() > 0)
                {
                    return false;
                }
            }

            return true;
        }