Exemple #1
0
        private async Task <MovingValidationResult> CanMoveDownAsync(GameGrid area, Figure figure)
        {
            Point[] position = await figure.GetPositionWithOffsetAsync();

            List <Point> bottomPoints = position
                                        .GroupBy(point => point.X, point => point.Y, (x, ys) => new Point(x, ys.Max()))
                                        .ToList();

            List <GameCell> cells = area.ToList();

            foreach (Point bottomPoint in bottomPoints)
            {
                int bottomBorder = cells
                                   .Where(cell => cell.Location.X == bottomPoint.X && cell.IsOccupiedWithNotFigure)
                                   .Where(cell => cell.Location.Y > bottomPoint.Y).Select(cell => cell.Location.Y)
                                   .DefaultIfEmpty(area.Height).Min();
                if (bottomBorder == bottomPoint.Y + 1)
                {
                    return(new MovingValidationResult {
                        Dead = true
                    });
                }
            }
            return(new MovingValidationResult {
                Allow = true
            });
        }
Exemple #2
0
        private async Task <MovingValidationResult> CanMoveLeftAsync(GameGrid area, Figure figure)
        {
            Point[] position = await figure.GetPositionWithOffsetAsync();

            List <Point> leftPoints = position
                                      .GroupBy(point => point.Y, point => point.X, (y, xs) => new Point(xs.Min(), y))
                                      .ToList();

            List <GameCell> cells = area.ToList();

            foreach (Point leftPoint in leftPoints)
            {
                int rightBorder = cells
                                  .Where(cell => cell.Location.Y == leftPoint.Y && cell.IsOccupiedWithNotFigure)
                                  .Where(cell => cell.Location.X < leftPoint.X).Select(cell => cell.Location.X)
                                  .DefaultIfEmpty(-1).Max();

                if (rightBorder == leftPoint.X - 1)
                {
                    return(new MovingValidationResult {
                        Allow = false
                    });
                }
            }
            return(new MovingValidationResult {
                Allow = true
            });
        }