4 people like it.

Minesweeper Kata

Solution to Minesweeper Kata challenge at Goto Copenhagen 2012 conference "Programming with the Stars" track.

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
19: 
20: 
21: 
22: 
23: 
24: 
25: 
26: 
27: 
28: 
29: 
30: 
31: 
32: 
33: 
34: 
let board = "*...
....
.*..
...."

let compute (board:string[]) =
    let value c = 
        match c with
        | '*' -> 1
        | '.' -> 0
        | _ -> failwith "Unexpected value"
    let count (x,y) =
        [-1,-1; 0,-1; 1,-1
         -1, 0;       1, 0
         -1, 1; 0, 1; 1, 1]
        |> List.sumBy (fun (dx,dy) ->
            let x, y = x + dx, y + dy
            if y>=0 && y<board.Length && x>=0 && x<board.[y].Length
            then board.[y].[x] |> value
            else 0
        )
    board |> Array.mapi (fun y line ->
        line.ToCharArray() |> Array.mapi (fun x c ->         
            match c with
            | '*' -> c
            | '.' -> '0' + char (count(x,y))
            | _ -> failwith "Unexpected value"  
        ) |> fun xs -> (System.String(xs) |> string)
    )

let view =
    let options = System.StringSplitOptions.RemoveEmptyEntries
    let board = board.Split([|'\r';'\n'|], options) 
    compute board
val board : string

Full name: Script.board
val compute : board:string [] -> string []

Full name: Script.compute
val board : string []
Multiple items
val string : value:'T -> string

Full name: Microsoft.FSharp.Core.Operators.string

--------------------
type string = System.String

Full name: Microsoft.FSharp.Core.string
val value : (char -> int)
val c : char
val failwith : message:string -> 'T

Full name: Microsoft.FSharp.Core.Operators.failwith
val count : (int * int -> int)
val x : int
val y : int
Multiple items
module List

from Microsoft.FSharp.Collections

--------------------
type List<'T> =
  | ( [] )
  | ( :: ) of Head: 'T * Tail: 'T list
  interface IEnumerable
  interface IEnumerable<'T>
  member Head : 'T
  member IsEmpty : bool
  member Item : index:int -> 'T with get
  member Length : int
  member Tail : 'T list
  static member Cons : head:'T * tail:'T list -> 'T list
  static member Empty : 'T list

Full name: Microsoft.FSharp.Collections.List<_>
val sumBy : projection:('T -> 'U) -> list:'T list -> 'U (requires member ( + ) and member get_Zero)

Full name: Microsoft.FSharp.Collections.List.sumBy
val dx : int
val dy : int
property System.Array.Length: int
module Array

from Microsoft.FSharp.Collections
val mapi : mapping:(int -> 'T -> 'U) -> array:'T [] -> 'U []

Full name: Microsoft.FSharp.Collections.Array.mapi
val line : string
System.String.ToCharArray() : char []
System.String.ToCharArray(startIndex: int, length: int) : char []
Multiple items
val char : value:'T -> char (requires member op_Explicit)

Full name: Microsoft.FSharp.Core.Operators.char

--------------------
type char = System.Char

Full name: Microsoft.FSharp.Core.char
val xs : char []
namespace System
Multiple items
type String =
  new : value:char -> string + 7 overloads
  member Chars : int -> char
  member Clone : unit -> obj
  member CompareTo : value:obj -> int + 1 overload
  member Contains : value:string -> bool
  member CopyTo : sourceIndex:int * destination:char[] * destinationIndex:int * count:int -> unit
  member EndsWith : value:string -> bool + 2 overloads
  member Equals : obj:obj -> bool + 2 overloads
  member GetEnumerator : unit -> CharEnumerator
  member GetHashCode : unit -> int
  ...

Full name: System.String

--------------------
System.String(value: nativeptr<char>) : unit
System.String(value: nativeptr<sbyte>) : unit
System.String(value: char []) : unit
System.String(c: char, count: int) : unit
System.String(value: nativeptr<char>, startIndex: int, length: int) : unit
System.String(value: nativeptr<sbyte>, startIndex: int, length: int) : unit
System.String(value: char [], startIndex: int, length: int) : unit
System.String(value: nativeptr<sbyte>, startIndex: int, length: int, enc: System.Text.Encoding) : unit
val view : string []

Full name: Script.view
val options : System.StringSplitOptions
type StringSplitOptions =
  | None = 0
  | RemoveEmptyEntries = 1

Full name: System.StringSplitOptions
field System.StringSplitOptions.RemoveEmptyEntries = 1
Next Version Raw view Test code New version

More information

Link:http://fssnip.net/cb
Posted:11 years ago
Author:Phillip Trelford
Tags: kata , game