< Summary

Information
Class: Minesweeper.MineField
Assembly: Minesweeper
File(s): /home/runner/work/poc-csharp-tdd-kata-minesweeper2/poc-csharp-tdd-kata-minesweeper2/src/Minesweeper/MineField.cs
Tag: 29_2324258472
Line coverage
96%
Covered lines: 85
Uncovered lines: 3
Coverable lines: 88
Total lines: 121
Line coverage: 96.5%
Branch coverage
92%
Covered branches: 26
Total branches: 28
Branch coverage: 92.8%
Method coverage

Method coverage is only available for sponsors.

Upgrade to PRO version

Coverage History

Metrics

MethodBranch coverage Cyclomatic complexity Line coverage
get_Width()100%1100%
get_Width()100%1100%
get_Width()100%1100%
get_Width()100%1100%
ToInnerStr()50%2100%
ToStr()50%2100%
ToCells()100%2100%
ClickTo(...)90.9%2293.93%
StartTo()90%1097.05%

File(s)

/home/runner/work/poc-csharp-tdd-kata-minesweeper2/poc-csharp-tdd-kata-minesweeper2/src/Minesweeper/MineField.cs

#LineLine coverage
 1using System.Security.Cryptography;
 2using System.Text;
 3using LanguageExt;
 4using LanguageExt.Common;
 5using static LanguageExt.Prelude;
 6using CellMap = LanguageExt.HashMap<(int, int), Minesweeper.Cell>;
 7
 8namespace Minesweeper;
 9
 10public record MineField
 11{
 12
 513    public record Setup(int Width, int Height) : MineField;
 1714    public record SetupWithBombs(int Width, int Height, int Bombs) : MineField;
 6615    public record SetupWithBombsPos(int Width, int Height, IEnumerable<(int X, int Y)> Bombs) : MineField;
 16216    public record Playing(int Width, int Height, CellMap Cells) : MineField;
 17    public record Win : MineField;
 18    public record Loose : MineField;
 19
 20    public string ToInnerStr() => this switch
 21    {
 122        Playing x => (from b in Enumerable.Range(0, x.Height)
 323                      from a in Enumerable.Range(0, x.Width)
 924                      select (a, b))
 925                     .OrderBy(x => x)
 926                     .Fold(new StringBuilder(), (s, e) => s.Append(x.Cells[e].ToInnerChar()))
 127                     .ToString()
 28    };
 29
 30    public string ToStr() => this switch
 31    {
 132        Playing x => (from b in Enumerable.Range(0, x.Height)
 333                      from a in Enumerable.Range(0, x.Width)
 934                      select (a, b))
 935                     .Fold(new StringBuilder(), (s, e) => s.Append(x.Cells[e].ToChar()))
 136                     .ToString()
 37    };
 38
 1539    public CellMap? ToCells() => this switch
 1540    {
 1041        Playing x => x.Cells,
 1542
 543        _ => null
 1544    };
 45
 5446    public MineField ClickTo(int xPos, int yPos) => this switch
 5447    {
 048        Setup x => x.StartTo().ClickTo(xPos, yPos),
 5449
 350        SetupWithBombsPos x => x.StartTo().ClickTo(xPos, yPos),
 5451
 052        SetupWithBombs x => x.StartTo().ClickTo(xPos, yPos),
 5453
 4254        Playing x => x.Cells.ContainsKey((xPos, yPos)) ? x.Cells[(xPos, yPos)] is Cell.Covered ? Id(
 4255            from _1 in Id(x with
 4256            {
 1357                Cells = x.Cells.AddOrUpdate((xPos, yPos), y => y.ClickTo(), new Cell.Empty())
 4258            })
 1359            let _2 = _1.Cells[(xPos, yPos)] switch
 1360            {
 161                Cell.Bomb => new Loose(),
 662                Cell.Number { Value: 0 } => _1.ClickTo(xPos - 1, yPos - 1)
 663                                              .ClickTo(xPos, yPos - 1)
 664                                              .ClickTo(xPos + 1, yPos - 1)
 665                                              .ClickTo(xPos - 1, yPos)
 666                                              .ClickTo(xPos + 1, yPos)
 667                                              .ClickTo(xPos - 1, yPos + 1)
 668                                              .ClickTo(xPos, yPos + 1)
 669                                              .ClickTo(xPos + 1, yPos + 1),
 670                _ => _1
 1371            }
 2672            select _2).Value.Map(a => a.ToCells()?
 11673                                       .Filter((k, v) => v is Cell.Covered)
 2474                                       .Exists((k, v) => v.IsBomb() is not true) ?? true ? a : new Win()).Value
 4275            : x : x,
 5476
 977        var x => x
 5478    };
 79
 80    public MineField StartTo() => this switch
 81    {
 682        SetupWithBombsPos x => new Playing(x.Width, x.Height, Id(() =>
 683        {
 684            var q = new CellMap(from b in Enumerable.Range(0, x.Height)
 1885                                from a in Enumerable.Range(0, x.Width)
 6086                                select ((a, b), Cell.New()));
 687
 2488            return x.Bombs.Fold(q, (s, e) => s.AddOrUpdate(e, x => x.BombTo(), new Cell.Empty())
 689                                              .AddOrUpdate((e.X - 1, e.Y - 1), x => x.AddNumberTo(), new Cell.Empty())
 790                                              .AddOrUpdate((e.X , e.Y - 1), x => x.AddNumberTo(), new Cell.Empty())
 491                                              .AddOrUpdate((e.X + 1, e.Y - 1), x => x.AddNumberTo(), new Cell.Empty())
 592                                              .AddOrUpdate((e.X - 1, e.Y), x => x.AddNumberTo(), new Cell.Empty())
 693                                              .AddOrUpdate((e.X + 1, e.Y), x => x.AddNumberTo(), new Cell.Empty())
 194                                              .AddOrUpdate((e.X - 1, e.Y + 1), x => x.AddNumberTo(), new Cell.Empty())
 595                                              .AddOrUpdate((e.X , e.Y + 1), x => x.AddNumberTo(), new Cell.Empty())
 1996                                              .AddOrUpdate((e.X + 1, e.Y + 1), x => x.AddNumberTo(), new Cell.Empty()));
 1297        }).Value()),
 98
 199        Setup x => new SetupWithBombs(x.Width, x.Height, 0).StartTo(),
 100
 2101        SetupWithBombs x => Id(() =>
 2102        {
 2103            var bombPos = (from j in RandomGenerator(x.Height)
 1104                           from i in RandomGenerator(x.Width)
 6105                           select (i, j)).Distinct().Take(x.Bombs);
 2106
 2107            return new SetupWithBombsPos(x.Width, x.Height, bombPos);
 2108
 2109            static IEnumerable<int> RandomGenerator(int max)
 2110            {
 5111                while (true)
 5112                {
 5113                    yield return RandomNumberGenerator.GetInt32(max);
 3114                }
 2115            }
 4116        }).Value().StartTo(),
 117
 0118        var x => x
 119    };
 120}
 121