| | 1 | | using LanguageExt; |
| | 2 | | using Minesweeper.Domain; |
| | 3 | | using static LanguageExt.Prelude; |
| | 4 | |
|
| | 5 | | namespace Minesweeper; |
| | 6 | |
|
| | 7 | | public static class Prelude |
| | 8 | | { |
| 1 | 9 | | public static ICell click(ICell cell) => cell switch |
| 1 | 10 | | { |
| 1 | 11 | | Covered c => c.Inner, |
| 0 | 12 | | _ => cell |
| 1 | 13 | | }; |
| | 14 | |
|
| 10 | 15 | | public static ICell plus(ICell cell) => cell switch |
| 10 | 16 | | { |
| 1 | 17 | | Zero => one, |
| 1 | 18 | | One => two, |
| 1 | 19 | | Two => three, |
| 1 | 20 | | Three => four, |
| 1 | 21 | | Four => five, |
| 1 | 22 | | Five => six, |
| 1 | 23 | | Six => seven, |
| 1 | 24 | | Seven => eight, |
| 2 | 25 | | _ => cell |
| 10 | 26 | | }; |
| | 27 | |
|
| 4 | 28 | | public static Bomb bomb { get; } = new(); |
| 3 | 29 | | public static Zero zero { get; } = new(); |
| 5 | 30 | | public static One one { get; } = new(); |
| 4 | 31 | | public static Two two { get; } = new(); |
| 4 | 32 | | public static Three three { get; } = new(); |
| 4 | 33 | | public static Four four { get; } = new(); |
| 4 | 34 | | public static Five five { get; } = new(); |
| 4 | 35 | | public static Six six { get; } = new(); |
| 4 | 36 | | public static Seven seven { get; } = new(); |
| 4 | 37 | | public static Eight eight { get; } = new(); |
| | 38 | |
|
| 10 | 39 | | public static string show(ICell cell) => cell switch |
| 10 | 40 | | { |
| 1 | 41 | | Covered => ".", |
| 0 | 42 | | Zero => " ", |
| 1 | 43 | | One => "1", |
| 1 | 44 | | Two => "2", |
| 1 | 45 | | Three => "3", |
| 1 | 46 | | Four => "4", |
| 1 | 47 | | Five => "5", |
| 1 | 48 | | Six => "6", |
| 1 | 49 | | Seven => "7", |
| 1 | 50 | | Eight => "8", |
| 1 | 51 | | Bomb => "*", |
| 10 | 52 | | }; |
| | 53 | |
|
| 1 | 54 | | public static Func<Width, Func<Height, MineField>> newMineField { get; private set; } = |
| 2 | 55 | | width => height => |
| 3 | 56 | | new MineField(width, height, Enumerable.Repeat<ICell>(zero, width.Value * height.Value).ToArr()); |
| | 57 | |
|
| 1 | 58 | | public static Option<int> index1D(X x, Y y, MineField @this) => (x.Value, y.Value) switch |
| 1 | 59 | | { |
| 0 | 60 | | ( < 0, _) => None, |
| 0 | 61 | | (_, < 0) => None, |
| 1 | 62 | | (var a, _) when a >= @this.Width.Value => None, |
| 1 | 63 | | (_, var a) when a >= @this.Height.Value => None, |
| 1 | 64 | | (var a, var b) => Some(b + a * @this.Width.Value) |
| 1 | 65 | | }; |
| | 66 | |
|
| | 67 | | public static Func<MineField, Func<X, Func<Y, IEnumerable<int>>>> indexesNear => |
| 0 | 68 | | @this => x => y => |
| 0 | 69 | | from _1 in new List<(X X, Y Y)> |
| 0 | 70 | | { |
| 0 | 71 | | (-1, -1), (0, -1), (1, -1), |
| 0 | 72 | | (-1, 0), (1, 0), |
| 0 | 73 | | (-1, 1), (0, 1), (1, 1), |
| 0 | 74 | | } |
| 0 | 75 | | let _2 = (_1.X + x, _1.Y + y) |
| 0 | 76 | | let _3 = index1D(_2.Item1, _2.Item2, @this) |
| 0 | 77 | | where _3 != None |
| 0 | 78 | | select _3.Match(v => v, () => default); |
| | 79 | |
|
| | 80 | |
|
| 1 | 81 | | public static Func<MineField, Func<X, Func<Y, Func<Func<ICell, ICell>, MineField>>>> ReplaceItem { get; private set; |
| 4 | 82 | | @this => x => y => hof => |
| 5 | 83 | | index1D(x, y, @this).Case switch |
| 5 | 84 | | { |
| 0 | 85 | | null => @this, |
| 1 | 86 | | int v => @this with |
| 1 | 87 | | { |
| 1 | 88 | | Cells = @this.Cells.SetItem(v, hof(@this.Cells[v])) |
| 1 | 89 | | } |
| 5 | 90 | | }; |
| | 91 | |
|
| | 92 | |
|
| | 93 | | } |
| | 94 | |
|