role-playing-game v2

This commit is contained in:
Alexander Heldt
2025-11-02 21:33:44 +01:00
parent 1de1e2fcb6
commit a454a8d40a

View File

@@ -1,3 +1,4 @@
import gleam/int
import gleam/option.{type Option, None, Some} import gleam/option.{type Option, None, Some}
pub type Player { pub type Player {
@@ -16,81 +17,21 @@ pub fn revive(player: Player) -> Option(Player) {
False -> None False -> None
True -> { True -> {
case player.level >= 10 { case player.level >= 10 {
False -> False -> Some(Player(..player, health: 100))
Some(Player( True -> Some(Player(..player, health: 100, mana: Some(100)))
name: player.name,
level: player.level,
health: 100,
mana: None,
))
True ->
Some(Player(
name: player.name,
level: player.level,
health: 100,
mana: Some(100),
))
} }
} }
} }
} }
// The `cast_spell` function takes as arguments an `Int` indicating how much mana the spell costs as well as a `Player`.
// It returns the updated player, as well as the amount of damage that the cast spell performs.
// A successful spell cast does damage equal to two times the mana cost of the spell.
// However, if the player has insufficient mana, nothing happens, the player is unchanged and no damage is done.
// If the player does not even have a mana pool, attempting to cast the spell must decrease their health by the mana cost of the spell and does no damage.
// Be aware that the players health cannot be below zero (0).
pub fn cast_spell(player: Player, cost: Int) -> #(Player, Int) { pub fn cast_spell(player: Player, cost: Int) -> #(Player, Int) {
let damage = cost * 2
case player.mana { case player.mana {
None -> { None -> #(Player(..player, health: player.health - cost |> int.max(0)), 0)
let health = player.health - cost
case health {
h if h > 0 -> #(
Player(
name: player.name,
level: player.level,
health:,
mana: player.mana,
),
0,
)
_ -> #(
Player(
name: player.name,
level: player.level,
health: 0,
mana: player.mana,
),
0,
)
}
}
Some(mana) -> { Some(mana) ->
let mana_left = mana - cost case mana {
case mana_left { m if m < cost -> #(player, 0)
ml if ml >= 0 -> #( m -> #(Player(..player, mana: Some(m - cost)), cost * 2)
Player(
name: player.name,
level: player.level,
health: player.health,
mana: Some(ml),
),
damage,
)
_ -> #(player, 0)
}
// let health = player.health - damage
// case health {
// h if h > 0 ->
// Player(name: player.name, level: player.level, health:, mana: todo)
// _ ->
// Player(name: player.name, level: player.level, health: 0, mana: mana)
// }
} }
} }
} }