< Summary

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

Metrics

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

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)
 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) :
 270            base(mineFieldInit.Width, mineFieldInit.Height, mineFieldInit.Cells)
 271        {
 272        }
 73    }
 74}