A Gleam implementation of the Scoundrel solo card game
munksgaard.me/scoundrel
lustre
scoundrel
gleam
card
game
1import gleam/result
2import gleam/string
3import slot.{type Slot}
4
5pub type Action {
6 AttackBarehanded(slot: Slot)
7 AttackWithWeapon(slot: Slot)
8 Heal(slot: Slot)
9 Equip(slot: Slot)
10 Flee
11}
12
13pub fn to_string(action: Action) -> String {
14 case action {
15 AttackBarehanded(slot) -> "attack barehanded " <> slot.to_string(slot)
16 AttackWithWeapon(slot) -> "attack with weapon " <> slot.to_string(slot)
17 Heal(slot) -> "heal " <> slot.to_string(slot)
18 Equip(slot) -> "equip " <> slot.to_string(slot)
19 Flee -> "flee"
20 }
21}
22
23pub fn slot(action: Action) -> Result(Slot, Nil) {
24 case action {
25 AttackBarehanded(slot) | AttackWithWeapon(slot) | Heal(slot) | Equip(slot) ->
26 Ok(slot)
27 Flee -> Error(Nil)
28 }
29}
30
31pub fn parse(input: String) -> Result(Action, String) {
32 case string.trim(input) {
33 "flee" -> Ok(Flee)
34 "attack barehanded " <> slot -> {
35 use slot <- result.map(slot.parse(slot))
36 AttackBarehanded(slot)
37 }
38 "attack with weapon " <> slot -> {
39 use slot <- result.map(slot.parse(slot))
40 AttackWithWeapon(slot)
41 }
42 "heal " <> slot -> {
43 use slot <- result.map(slot.parse(slot))
44 Heal(slot)
45 }
46 "equip " <> slot -> {
47 use slot <- result.map(slot.parse(slot))
48 Equip(slot)
49 }
50 s -> Error("Couldn't parse action: " <> s)
51 }
52}