Skip to content

Instantly share code, notes, and snippets.

@lefthandedgoat
Created October 31, 2012 01:43
Show Gist options
  • Save lefthandedgoat/3984326 to your computer and use it in GitHub Desktop.
Save lefthandedgoat/3984326 to your computer and use it in GitHub Desktop.
minesweeper kata
// Learn more about F# at http://fsharp.net
open System
let mutable tests = []
let test f = tests <- List.append tests [f]
let xtest f = ()
let mutable before = fun () -> ()
let run _ = List.map (fun f -> (before ()
f ())) tests
let describe description = Console.WriteLine(description.ToString())
let (==) value1 value2 =
if value1 = value2 then Console.ForegroundColor <- ConsoleColor.Green else Console.ForegroundColor <- ConsoleColor.Red
Console.Write((value1 = value2))
Console.ResetColor()
Console.WriteLine(" expected: {0} got: {1}", value2, value1)
/////// implimnentation
type cell = { x : int; y : int ; hasBomb : bool; mutable clicked: bool; mutable areaBombs : int }
type result = { hasBomb: bool ; board: cell list }
let draw (board : cell list) =
let paint (c : cell) =
if c.hasBomb = true then
Console.ForegroundColor <- ConsoleColor.Red
Console.Write(" * ")
Console.ResetColor()
else
if c.clicked = true then
Console.ForegroundColor <- ConsoleColor.Green
Console.Write(" {0} ", c.areaBombs)
Console.ResetColor()
else
Console.ForegroundColor <- ConsoleColor.Green
Console.Write(" - ")
Console.ResetColor()
()
board |> List.iter (fun c -> paint c)
System.Console.WriteLine()
let click x y board =
let left a b = a - b
let right a b = a + b
let rec go f x y board =
let newX = f x 1
if (board |> List.exists (fun c -> c.x = newX && c.y = y)) then
let cell = board |> List.find (fun c -> c.x = newX && c.y = y)
if cell.hasBomb = false then
cell.clicked <- true
go f newX y board
else //there is a bomb
let previousCell = board |> List.find (fun c -> c.x = x && c.y = y)
previousCell.areaBombs <- previousCell.areaBombs + 1
()
else
()
let cell = board |> List.find (fun c -> c.x = x && c.y = y)
cell.clicked <- true
go left x y board
go right x y board
draw board
{ hasBomb = cell.hasBomb; board = board }
//////tests
test (fun _ ->
describe "one 1 x 1 with bomb returns exploded"
let board = [{x = 1; y = 1; hasBomb = true; clicked = false; areaBombs = 0}]
let resultingBoard = [{x = 1; y = 1; hasBomb = true; clicked = true; areaBombs = 0}]
click 1 1 board == { hasBomb = true; board = resultingBoard }
)
test (fun _ ->
describe "one 1 x 1 without bomb returns not exploded"
let board = [{x = 1; y = 1; hasBomb = false; clicked = false; areaBombs = 0}]
let resultingBoard = [{x = 1; y = 1; hasBomb = false; clicked = true; areaBombs = 0}]
click 1 1 board == { hasBomb = false; board = resultingBoard }
)
test (fun _ ->
describe "one 1 x 6 without bomb returns not exploded and area bomb marked 1"
let board =
[
{x = 1; y = 1; hasBomb = false; clicked = false; areaBombs = 0};
{x = 2; y = 1; hasBomb = false; clicked = false; areaBombs = 0};
{x = 3; y = 1; hasBomb = false; clicked = false; areaBombs = 0};
{x = 4; y = 1; hasBomb = true; clicked = false; areaBombs = 0};
{x = 5; y = 1; hasBomb = false; clicked = false; areaBombs = 0};
{x = 6; y = 1; hasBomb = false; clicked = false; areaBombs = 0};
]
let resultingBoard =
[
{x = 1; y = 1; hasBomb = false; clicked = false; areaBombs = 0};
{x = 2; y = 1; hasBomb = false; clicked = false; areaBombs = 0};
{x = 3; y = 1; hasBomb = false; clicked = false; areaBombs = 0};
{x = 4; y = 1; hasBomb = true; clicked = false; areaBombs = 0};
{x = 5; y = 1; hasBomb = false; clicked = true; areaBombs = 1}; //true
{x = 6; y = 1; hasBomb = false; clicked = true; areaBombs = 0}; //true
]
click 5 1 board == { hasBomb = false; board = resultingBoard }
)
test (fun _ ->
describe "one 1 x 3 without bomb returns not exploded and area bomb marked 2"
let board =
[
{x = 1; y = 1; hasBomb = true; clicked = false; areaBombs = 0};
{x = 2; y = 1; hasBomb = false; clicked = false; areaBombs = 0};
{x = 3; y = 1; hasBomb = true; clicked = false; areaBombs = 0};
]
let resultingBoard =
[
{x = 1; y = 1; hasBomb = true; clicked = false; areaBombs = 0};
{x = 2; y = 1; hasBomb = false; clicked = true; areaBombs = 2};
{x = 3; y = 1; hasBomb = true; clicked = false; areaBombs = 0};
]
click 2 1 board == { hasBomb = false; board = resultingBoard }
)
run()
System.Console.ReadKey()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment