Skip to content

Instantly share code, notes, and snippets.

@lefthandedgoat
Created June 26, 2013 01:55
Show Gist options
  • Save lefthandedgoat/5864164 to your computer and use it in GitHub Desktop.
Save lefthandedgoat/5864164 to your computer and use it in GitHub Desktop.
wizards duel
// 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)
//implementation
type move =
| Stab
| P
| W
| F
| D
| S
let mutable wizard1HP = 15
let mutable wizard2HP = 15
let mutable wizard1LeftHand : move list = []
let mutable wizard1RightHand : move list = []
let mutable wizard2LeftHand : move list = []
let mutable wizard2RightHand : move list = []
let mutable wizard1HasBlocked = false
let mutable wizard2HasBlocked = false
let mutable wizard1HasStabbed = false
let mutable wizard2HasStabbed = false
let mutable wizard1LHWasMove = false
let mutable wizard1RHWasMove = false
let mutable wizard2LHWasMove = false
let mutable wizard2RHWasMove = false
let mutable wizard1HasCountered = false
let mutable wizard2HasCountered = false
let reset () =
wizard1HP <- 15
wizard2HP <- 15
wizard1LeftHand <- []
wizard1RightHand <- []
wizard2LeftHand <- []
wizard2RightHand <- []
wizard1HasBlocked <- false
wizard2HasBlocked <- false
wizard1HasStabbed <- false
wizard2HasStabbed <- false
wizard1LHWasMove <- false
wizard1RHWasMove <- false
wizard2LHWasMove <- false
wizard2RHWasMove <- false
wizard1HasCountered <- false
wizard2HasCountered <- false
before <- reset
let isLW (move : move) (previousMoves : move list) =
let combined = previousMoves @ [move]
match combined with
| W :: F :: P :: _ -> true
| _ :: W :: F :: P :: _ -> true
| _ -> false
let isHW (move : move) (previousMoves : move list) =
let combined = previousMoves @ [move]
match combined with
| W :: P :: F :: D :: _ -> true
| _ :: W :: P :: F :: D :: _ -> true
| _ -> false
let isCS (move : move) (previousMoves : move list) =
let combined = previousMoves @ [move]
match combined with
| W :: P :: P :: _ -> true
| _ :: W :: P :: P :: _ -> true
| W :: W :: S :: _ -> true
| _ :: W :: W :: S :: _ -> true
| _ -> false
let rec play (moves : (move * move * move * move) list)=
if moves.Length > 0 then
let w1lh, w1rh, w2lh, w2rh = moves.Head
//counter spell
if (isCS w1lh wizard1LeftHand) then
wizard1HasCountered <- true
wizard1LeftHand <- []
if (isCS w1rh wizard1RightHand) then
wizard1HasCountered <- true
wizard1RightHand <- []
if (isCS w2lh wizard2LeftHand) then
wizard2HasCountered <- true
wizard2LeftHand <- []
if (isCS w2rh wizard2RightHand) then
wizard2HasCountered <- true
wizard2RightHand <- []
//heavy wounds
if (isHW w1lh wizard1LeftHand) then
if not wizard2HasCountered then wizard2HP <- wizard2HP - 3
wizard1LHWasMove <- true
wizard1LeftHand <- []
if (isHW w1rh wizard1RightHand) then
if not wizard2HasCountered then wizard2HP <- wizard2HP - 3
wizard1RHWasMove <- true
wizard1RightHand <- []
if (isHW w2lh wizard2LeftHand) then
if not wizard1HasCountered then wizard1HP <- wizard1HP - 3
wizard2LHWasMove <- true
wizard2LeftHand <- []
if (isHW w2rh wizard2RightHand) then
if not wizard1HasCountered then wizard1HP <- wizard1HP - 3
wizard2RHWasMove <- true
wizard2RightHand <- []
//light wounds
if (isLW w1lh wizard1LeftHand) then
if not wizard2HasCountered then wizard2HP <- wizard2HP - 2
wizard1LHWasMove <- true
wizard1LeftHand <- []
if (isLW w1rh wizard1RightHand) then
if not wizard2HasCountered then wizard2HP <- wizard2HP - 2
wizard1RHWasMove <- true
wizard1RightHand <- []
if (isLW w2lh wizard2LeftHand) then
if not wizard1HasCountered then wizard1HP <- wizard1HP - 2
wizard2LHWasMove <- true
wizard2LeftHand <- []
if (isLW w2rh wizard2RightHand) then
if not wizard1HasCountered then wizard1HP <- wizard1HP - 2
wizard2RHWasMove <- true
wizard2RightHand <- []
//stab
if not wizard1LHWasMove then if w1lh = Stab then wizard1HasStabbed <- true
if not wizard1RHWasMove then if w1rh = Stab then wizard1HasStabbed <- true
if not wizard2LHWasMove then if w2lh = Stab then wizard2HasStabbed <- true
if not wizard2RHWasMove then if w2rh = Stab then wizard2HasStabbed <- true
//block
if not wizard1LHWasMove then if w1lh = P && wizard1LeftHand = [] then wizard1HasBlocked <- true
if not wizard1RHWasMove then if w1rh = P && wizard1RightHand = [] then wizard1HasBlocked <- true
if not wizard2LHWasMove then if w2lh = P && wizard2LeftHand = [] then wizard2HasBlocked <- true
if not wizard2RHWasMove then if w2rh = P && wizard2RightHand = [] then wizard2HasBlocked <- true
if wizard1HasStabbed && not wizard2HasBlocked then wizard2HP <- wizard2HP - 1
if wizard2HasStabbed && not wizard1HasBlocked then wizard1HP <- wizard1HP - 1
//append moves
if not wizard1LHWasMove then wizard1LeftHand <- wizard1LeftHand @ [w1lh]
if not wizard1RHWasMove then wizard1RightHand <- wizard1RightHand @ [w1rh]
if not wizard2LHWasMove then wizard2LeftHand <- wizard2LeftHand @ [w2lh]
if not wizard2RHWasMove then wizard2RightHand <- wizard2RightHand @ [w2rh]
play moves.Tail
()
test (fun _ ->
describe "when stabbing each other one damage"
play [(Stab, Stab, Stab, Stab)]
wizard1HP == 14
wizard2HP == 14
)
test (fun _ ->
describe "when getting stabbed, P will stop the damage"
play [(Stab, P, P, Stab)]
wizard1HP == 15
wizard2HP == 15
)
test (fun _ ->
describe "when getting stabbed, P will stop the damage"
play [(Stab, Stab, P, Stab)]
wizard1HP == 14
wizard2HP == 15
)
test (fun _ ->
describe "WFP casts light wounds and it isnt blocked and causes 2 damage"
[
(P, W, P, Stab)
(P, F, P, Stab)
(P, P, P, Stab)
] |> play
wizard1HP == 15
wizard2HP == 13
)
test (fun _ ->
describe "both are light wounds"
[
(P, W, P, W)
(P, F, P, F)
(P, P, P, P)
] |> play
wizard1HP == 13
wizard2HP == 13
)
test (fun _ ->
describe "WPFD casts heavy wounds and it isnt blocked and causes 3 damage"
[
(P, W, P, Stab)
(P, P, P, Stab)
(P, F, P, Stab)
(P, D, P, Stab)
] |> play
wizard1HP == 15
wizard2HP == 12
)
test (fun _ ->
describe "counter SPELLLLLLLLLLLLLLLLLLLLLLL"
[
(P, W, P, Stab)
(P, P, W, Stab)
(P, F, P, Stab)
(P, D, P, Stab)
] |> play
wizard1HP == 15
wizard2HP == 15
)
test (fun _ ->
describe "counter SPELLLLLLLLLLLLLLLLLLLLLLL"
[
(P, W, W, Stab)
(P, W, P, Stab)
(P, W, F, Stab)
(P, S, D, Stab)
] |> play
wizard1HP == 15
wizard2HP == 15
)
run()
Console.ReadKey()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment