Skip to content

Instantly share code, notes, and snippets.

@davidgrenier
Created April 8, 2012 18:39
Show Gist options
  • Save davidgrenier/2339069 to your computer and use it in GitHub Desktop.
Save davidgrenier/2339069 to your computer and use it in GitHub Desktop.
Bejeweled Initial Fill
let init settings =
let getJewel a b =
let rnd m = rand() * (float (settings.JewelTypes - m)) |> floor |> int
let shift j jewl = if jewl >= j then jewl + 1 else jewl
match a, b with
| None, None -> rnd 0
| None, Some i
| Some i, None -> rnd 1 |> shift i
| Some i, Some j when i = j -> rnd 1 |> shift j
| Some i, Some j -> rnd 2 |> shift (min i j) |> shift (max i j)
let addJewel = function
| n, _, _, _, _, _ when n = settings.GridSize -> None
| n, top, mid, m, i, j ->
let a = if j = i || j = m then j else None
let mid, top, m2, b =
match mid with
| [] -> [], [], None, None
| m2 :: mid ->
match top with
| t :: top when m2 = t ->
mid, top, Some m2, Some m2
| _ :: top | top ->
let eq = Some m2 = j || Some m2 = m || not mid.IsEmpty && mid.Head = m2
mid, top, Some m2, if eq then Some m2 else None
let jewel = getJewel a b
Some (jewel, (n+1, top, mid, m2, j, Some jewel))
let addLine = function
| n, _, _ when n = settings.GridSize -> None
| n, top, mid ->
let line = Seq.unfold addJewel (0, top, mid, None, None, None) |> Seq.toList
Some (line, (n + 1, mid, line))
Seq.unfold addLine (0, [], [])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment