| | 1 | | namespace Minesweeper |
| | 2 | |
|
| | 3 | | open System |
| | 4 | | open System.Text |
| | 5 | |
|
| | 6 | | type Minefield = |
| | 7 | | | Setup of width:int * height:int |
| | 8 | | | SetupWithBombPos of width:int * height:int * seq<int * int> |
| | 9 | | | Playing of width:int * height:int * Map<int * int, Cell> |
| | 10 | | | Win of width:int * height:int * Map<int * int, Cell> |
| | 11 | | | Loose of width:int * height:int * Map<int * int, Cell> |
| | 12 | |
|
| | 13 | | module Minefield = |
| | 14 | | let rec string v = |
| | 15 | | let print w z = |
| 3 | 16 | | let sb = (StringBuilder(), z) ||> Map.fold(fun s (y, x) z -> |
| 27 | 17 | | let _1 = s.Append(z |> Cell.char) |
| 9 | 18 | | match x with |
| 12 | 19 | | | _ when x = w - 1 -> _1.Append('\n') |
| 7 | 20 | | | _ -> _1.Append(' ')) |
| 1 | 21 | | sb.ToString() |
| 1 | 22 | | match v with |
| | 23 | | | Win (w, _, z) -> |
| 0 | 24 | | let r = (w, z) ||> print |
| 0 | 25 | | r + "\nWin" |
| | 26 | | | Loose (w, _, z) -> |
| 0 | 27 | | let r = (w, z) ||> print |
| 0 | 28 | | r + "\nLoose" |
| 3 | 29 | | | Playing (w, _, z) -> (w, z) ||> print |
| | 30 | |
|
| 0 | 31 | | | _ -> String.Empty |
| | 32 | |
|
| | 33 | | let ifWin v = |
| 12 | 34 | | match v with |
| | 35 | | | Playing (w, h, z) -> |
| 16 | 36 | | let isNotWin = z |> Map.exists (fun x y -> |
| 33 | 37 | | match y with |
| 7 | 38 | | | Covered(Number _) -> true |
| 34 | 39 | | | _ -> false) |
| 8 | 40 | | match isNotWin with |
| 1 | 41 | | | false -> Win(w, h, z) |
| 7 | 42 | | | _ -> v |
| 4 | 43 | | | _ -> v |
| | 44 | |
|
| | 45 | | let getCells = function |
| 5 | 46 | | | Playing (w, h, z) -> z |
| 0 | 47 | | | _ -> Map.empty |
| | 48 | |
|
| | 49 | | let rec click p v = |
| 51 | 50 | | match v with |
| | 51 | | | Playing (w, h, z) -> |
| | 52 | | let map = Option.map |
| 42 | 53 | | let mapClick = map Cell.click |
| 42 | 54 | | match Map.tryFind p z with |
| | 55 | | | Some(Covered _) -> |
| 26 | 56 | | let clicked = z |> Map.change p mapClick |
| 13 | 57 | | match Map.tryFind p clicked with |
| 1 | 58 | | | Some(Bomb) -> Loose(w, h, clicked) |
| | 59 | | | Some(Number 0) -> |
| 48 | 60 | | let (+) a b = (fst(a) + fst(b), snd(a) + snd(b)) |
| 12 | 61 | | Playing(w, h, clicked) |> click (p + (-1, -1)) |
| 6 | 62 | | |> click (p + (-1, 0)) |
| 6 | 63 | | |> click (p + (-1, 1)) |
| 6 | 64 | | |> click (p + (0, -1)) |
| 6 | 65 | | |> click (p + (0, 1)) |
| 6 | 66 | | |> click (p + (1, -1)) |
| 6 | 67 | | |> click (p + (1, 0)) |
| 6 | 68 | | |> click (p + (1, 1)) |
| 6 | 69 | | |> ifWin |
| 12 | 70 | | | _ -> Playing(w, h, clicked) |> ifWin |
| 29 | 71 | | | _ -> v |
| 9 | 72 | | | _ -> v |
| | 73 | |
|
| | 74 | | let rec start v = |
| 9 | 75 | | match v with |
| 10 | 76 | | | Setup (w, h) -> Playing (w, h, query { |
| 5 | 77 | | for y in 0..h - 1 do |
| 15 | 78 | | for x in 0..w - 1 do |
| 225 | 79 | | select ((y, x), 0 |> Number |> Covered) |
| 10 | 80 | | } |> Map.ofSeq) |
| | 81 | | | SetupWithBombPos (w, h, z) -> |
| 12 | 82 | | let cells = Setup (w, h) |> start |> getCells |
| 12 | 83 | | let cellsWithBombs = (cells, z) ||> Seq.fold (fun acc key -> |
| 4 | 84 | | let map = Option.map |
| 6 | 85 | | let mapAdd = map Cell.add |
| 48 | 86 | | let (+) a b = (fst(a) + fst(b), snd(a) + snd(b)) |
| 18 | 87 | | acc |> Map.change key (map(fun i -> Covered Bomb)) |
| 6 | 88 | | |> Map.change (key + (-1, -1)) mapAdd |
| 6 | 89 | | |> Map.change (key + (-1, 0)) mapAdd |
| 6 | 90 | | |> Map.change (key + (-1, 1)) mapAdd |
| 6 | 91 | | |> Map.change (key + ( 0, -1)) mapAdd |
| 6 | 92 | | |> Map.change (key + ( 0, 1)) mapAdd |
| 6 | 93 | | |> Map.change (key + ( 1, -1)) mapAdd |
| 6 | 94 | | |> Map.change (key + ( 1, 0)) mapAdd |
| 6 | 95 | | |> Map.change (key + ( 1, 1)) mapAdd |
| 4 | 96 | | ) |
| 4 | 97 | | Playing(w, h, cellsWithBombs) |
| 0 | 98 | | | _ -> v |
| | 99 | |
|
| | 100 | | let count = function |
| 2 | 101 | | | Playing (w, h, z) -> z |> Map.count |
| 0 | 102 | | | _ -> 0 |