DEV Community

loading...

Discussion on: Daily Challenge #43 - Boardgame Fight Resolver

Collapse
citizen428 profile image
Michael Kohl

F#:

type Piece = Archer | Swordsman | Pikeman | Cavalry

let fightResolve attacker defender =
  match attacker, defender with
  | Swordsman, Archer 
  | Pikeman, Swordsman 
  | Cavalry, Pikeman 
  | Archer, Cavalry -> defender
  | _ -> attacker
Collapse
citizen428 profile image
Michael Kohl

One could also turn the fight itself into a type:

type Piece = Archer | Swordsman | Pikeman | Cavalry

type Fight =
  { Attacker : Piece
    Defender : Piece }

let fightResolve fight =
  let { Attacker = attacker; Defender = defender } = fight
  match attacker, defender with
  | Swordsman, Archer 
  | Pikeman, Swordsman 
  | Cavalry, Pikeman 
  | Archer, Cavalry -> defender
  | _ -> attacker