< Summary

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

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
.ctor(...)0%110100%
NearCellGenerator(...)0%110100%
>c__DisplayClass15_0/<<NearCellGenerator()0%11110100%

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{
 208    public record MineField(int Width, int Height, IEnumerable<Cell> Cells)
 59    {
 510        protected IEnumerable<(int X, int Y)> NearCellGenerator((int X, int Y) pos)
 1211        {
 512            IEnumerable<(int X, int Y)> NearCellsInner()
 1213            {
 1214                yield return (pos.X - 1, pos.Y - 1);
 1215                yield return (pos.X, pos.Y - 1);
 1216                yield return (pos.X + 1, pos.Y - 1);
 1217                yield return (pos.X - 1, pos.Y);
 1218                yield return (pos.X + 1, pos.Y);
 1219                yield return (pos.X - 1, pos.Y + 1);
 1220                yield return (pos.X, pos.Y + 1);
 1221                yield return (pos.X + 1, pos.Y + 1);
 1222            }
 523
 6124            bool CheckBoundary((int X, int Y) pos) => pos switch
 6125            {
 14226                (var x, var y) when x < 0 || y < 0 || x >= Width || y >= Height => false,
 9227                _ => true
 6128            };
 529
 1230            return NearCellsInner().Where(CheckBoundary);
 1231        }
 532    }
 33
 34    public record MineFieldInit : MineField
 35    {
 36        public MineFieldInit(int width, int height)
 37            : base(width, height, Enumerable.Empty<Cell>())
 38        {
 39        }
 40
 41        public MineFieldReady SetBombs(IEnumerable<(int X, int Y)> bombPosGenerator) =>
 42           new(this with
 43           {
 44               Cells = from y in Enumerable.Range(0, Height)
 45                       from x in Enumerable.Range(0, Width)
 46                       select (x, y) into pos
 47                       join bombPos in from bombPosInner in bombPosGenerator
 48                                       select new { Pos = bombPosInner }
 49                       on pos equals bombPos.Pos into bombPosGroup
 50                       from bombPosGroupItem in bombPosGroup.DefaultIfEmpty(null)
 51                       join nearBombCount in from bombPosInner in bombPosGenerator
 52                                             from nearBombPos in NearCellGenerator(bombPosInner)
 53                                             group nearBombPos by nearBombPos into nearBombPosGroup
 54                                             select new
 55                                             {
 56                                                 Pos = nearBombPosGroup.Key,
 57                                                 Count = nearBombPosGroup.Count()
 58                                             }
 59                       on pos equals nearBombCount.Pos into nearBombCountGroup
 60                       from nearBombCountGroupItem in nearBombCountGroup.DefaultIfEmpty()
 61                       select cell(pos,
 62                                   bombPosGroupItem is not null,
 63                                   nearBombCountGroupItem.Count)
 64           });
 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}