Skip to content

Instantly share code, notes, and snippets.

@SteveGilham
Last active March 18, 2017 15:56
Show Gist options
  • Save SteveGilham/299e0561c2ac0586fc35485e81989be6 to your computer and use it in GitHub Desktop.
Save SteveGilham/299e0561c2ac0586fc35485e81989be6 to your computer and use it in GitHub Desktop.
open System.Linq
type Cell = North | East | Root
let rows = 10
let cols = 10
let rand = System.Random()
let row () = Seq.append (Enumerable.Repeat(East, cols - 1)) (Seq.singleton Root)
let maingrid() = Enumerable.Repeat(row(), rows - 1)
let grid = (row() |> Seq.toList) ::
(maingrid ()
|> Seq.map (fun r -> r |> Seq.map (fun cell -> match cell with
| Root -> North
| East when rand.Next(2) = 1 -> North
| _ -> East)
|> Seq.toList) // row
|> Seq.toList) // grid
grid |> List.iter (fun r -> printf "+"
Seq.iter (fun cell -> match cell with
| North -> printf " "
| _ -> printf "---"
printf "+") r
printfn ""
printf "|"
Seq.iter (fun cell -> match cell with
| East -> printf " "
| _ -> printf " |") r
printfn "")
grid |> List.head |> (fun r -> printf "+"
Seq.iter (fun cell -> printf "---+") r
printfn "" )
type Connected = { exit: Cell; West : Connected option; South : Connected option }
let ((seed::seedrow)::body) = List.rev grid
let seed' = { exit=seed; West=None; South=None }
let seedrow' = List.fold (fun accum cell -> { exit=cell; West=Some (List.head accum); South=None} :: accum) [seed'] seedrow
|> List.rev
let grid' = List.fold (fun accum row -> let (x::xs) = List.zip row (List.head accum)
let seed'' = {exit = fst x; West=None; South=Some (snd x)}
let seedrow'' = List.fold (fun accum' cell -> {exit=fst cell; West=Some (List.head accum'); South = Some(snd cell)} :: accum')
[seed''] xs
|> List.rev
seedrow'' :: accum) [seedrow'] body
grid' |> List.iter (fun r -> printf "+"
Seq.iter (fun cell -> match cell.exit with
| North -> printf " "
| _ -> printf "---"
printf "+") r
printfn ""
printf "|"
Seq.iter (fun cell -> let w = match cell.West with
| Some x when x.exit = East -> "<"
| _ -> " "
let s = match cell.South with
| Some x when x.exit = North -> "V"
| _ -> " "
let x = match cell.exit with
| East -> " "
| _ -> "|"
printf "%s%s %s" w s x) r
printfn "")
graphed |> List.head |> (fun r -> printf "+"
Seq.iter (fun cell -> printf "---+") r
printfn "" )
;;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment