< Summary

Class:FpMineSweeper.MineFieldInit
Assembly:FpMineSweeper
File(s):/home/runner/work/KATA-MineSweeper-20210502b/KATA-MineSweeper-20210502b/src/FpMineSweeper/MineField.cs
Covered lines:26
Uncovered lines:0
Coverable lines:26
Total lines:74
Line coverage:100% (26 of 26)
Covered branches:0
Total branches:0
Tag:52_817390446

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
.ctor(...)0%110100%
SetBombs(...)0%17170100%

File(s)

/home/runner/work/KATA-MineSweeper-20210502b/KATA-MineSweeper-20210502b/src/FpMineSweeper/MineField.cs

#LineLine coverage
 1using System.Collections.Generic;
 2using System.Linq;
 3using LanguageExt;
 4using static FpMineSweeper.Prelude;
 5
 6namespace FpMineSweeper
 7{
 8    public record MineField(int Width, int Height, IEnumerable<Cell> Cells)
 9    {
 10        protected IEnumerable<(int X, int Y)> NearCellGenerator((int X, int Y) pos)
 11        {
 12            IEnumerable<(int X, int Y)> NearCellsInner()
 13            {
 14                yield return (pos.X - 1, pos.Y - 1);
 15                yield return (pos.X, pos.Y - 1);
 16                yield return (pos.X + 1, pos.Y - 1);
 17                yield return (pos.X - 1, pos.Y);
 18                yield return (pos.X + 1, pos.Y);
 19                yield return (pos.X - 1, pos.Y + 1);
 20                yield return (pos.X, pos.Y + 1);
 21                yield return (pos.X + 1, pos.Y + 1);
 22            }
 23
 24            bool CheckBoundary((int X, int Y) pos) => pos switch
 25            {
 26                (var x, var y) when x < 0 || y < 0 || x >= Width || y >= Height => false,
 27                _ => true
 28            };
 29
 30            return NearCellsInner().Where(CheckBoundary);
 31        }
 32    }
 33
 34    public record MineFieldInit : MineField
 35    {
 36        public MineFieldInit(int width, int height)
 337            : base(width, height, Enumerable.Empty<Cell>())
 338        {
 339        }
 40
 41        public MineFieldReady SetBombs(IEnumerable<(int X, int Y)> bombPosGenerator) =>
 242           new(this with
 243           {
 244               Cells = from y in Enumerable.Range(0, Height)
 845                       from x in Enumerable.Range(0, Width)
 2046                       select (x, y) into pos
 2047                       join bombPos in from bombPosInner in bombPosGenerator
 2648                                       select new { Pos = bombPosInner }
 4449                       on pos equals bombPos.Pos into bombPosGroup
 3850                       from bombPosGroupItem in bombPosGroup.DefaultIfEmpty(null)
 2051                       join nearBombCount in from bombPosInner in bombPosGenerator
 5452                                             from nearBombPos in NearCellGenerator(bombPosInner)
 7653                                             group nearBombPos by nearBombPos into nearBombPosGroup
 3854                                             select new
 3855                                             {
 3856                                                 Pos = nearBombPosGroup.Key,
 3857                                                 Count = nearBombPosGroup.Count()
 3858                                             }
 5659                       on pos equals nearBombCount.Pos into nearBombCountGroup
 2060                       from nearBombCountGroupItem in nearBombCountGroup.DefaultIfEmpty()
 2061                       select cell(pos,
 2062                                   bombPosGroupItem is not null,
 2063                                   nearBombCountGroupItem.Count)
 264           });
 65    }
 66
 67    public record MineFieldReady : MineField
 68    {
 69        public MineFieldReady(MineFieldInit mineFieldInit) :
 70            base(mineFieldInit.Width, mineFieldInit.Height, mineFieldInit.Cells)
 71        {
 72        }
 73    }
 74}