Skip to content

Instantly share code, notes, and snippets.

@darkf
Created May 31, 2012 23:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save darkf/2847214 to your computer and use it in GitHub Desktop.
Save darkf/2847214 to your computer and use it in GitHub Desktop.
type actor = {mutable hp : int; name : string}
type battle = {enemy : actor; mutable turn : int}
type attack = Slash of int
let player = {hp=100; name="Player"}
let runAttack by who atk =
match atk with
| Slash dmg -> printfn "%s slashes %s for %d" by.name who.name dmg; who.hp <- who.hp - dmg
let runTurn b =
printfn "Turn %d" b.turn
if b.turn % 2 = 1 then
// player's turn
runAttack player b.enemy <| Slash 10
else
// enemy's turn
runAttack b.enemy player <| Slash 5
b.turn <- b.turn + 1
printfn "Status: Player HP: %d | Enemy HP: %d" player.hp b.enemy.hp
let main =
let b = {enemy={hp=100; name="Bob Dylan"}; turn=1}
for i = 0 to 5 do
runTurn b
System.Console.ReadKey true |> ignore
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment