Last active
April 12, 2020 01:04
-
-
Save reckenrode/0e51a7add064165e6eee320eadd9714a to your computer and use it in GitHub Desktop.
OSE encounter/treasure generation (F# vs. Ruby)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
open FsRandom | |
let inline tryParse a = | |
let mutable r = Unchecked.defaultof<'a> | |
if (^a: (static member TryParse: string * ^a byref -> bool) (a, &r)) | |
then Some r | |
else None | |
type Stock = | |
Empty | Monster | Special | Trap | |
override this.ToString () = | |
match this with | |
| Empty -> "empty" | |
| Monster -> "monster" | |
| Special -> "special" | |
| Trap -> "trap" | |
let stocking = [| Empty; Empty; Monster; Monster; Special; Trap |] | |
let chances = Map.ofList [(Empty, 1); (Monster, 3); (Trap, 2)] | |
let rollD6 = Statistics.uniformDiscrete (1, 6) | |
let hasTreasure ttype = | |
random { | |
let chance = chances |> Map.tryFind ttype |> Option.fold (fun _ t -> t) 0 | |
let! roll = rollD6 | |
return roll <= chance | |
} | |
let runEncounters n = | |
random { | |
for x in 1 .. n do | |
let! roll = rollD6 | |
let encounter = stocking.[roll - 1] | |
let! treasure = hasTreasure encounter | |
printfn "Stocking room #%-3d: %s %s" | |
x (string encounter) (if treasure then "has treasure" else "") | |
} | |
[<EntryPoint>] | |
let main argv = | |
match argv |> Array.tryItem 0 |> Option.bind tryParse with | |
| Some n -> Utility.defaultState |> Random.get (runEncounters n) | |
| None -> printfn "usage: generate_treasure <n>" | |
0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
room_stocking = %i[empty empty monster monster special trap] | |
def treasure?(type) | |
chances = { empty: 1, monster: 3, trap: 2 } | |
chance = chances[type] || 0 | |
rand((1..6)) <= chance | |
end | |
16.times do |n| | |
encounter = room_stocking[rand((1..6))-1] | |
treasure = treasure?(encounter) | |
puts "Stocking room ##{format('%-3s', "#{n+1}:")} #{encounter} #{'has treasure' if treasure}" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment