role-playing-game

This commit is contained in:
Alexander Heldt
2025-11-02 21:22:44 +01:00
parent 5fb5492306
commit 1de1e2fcb6
10 changed files with 409 additions and 0 deletions

View File

@@ -0,0 +1,96 @@
import gleam/option.{type Option, None, Some}
pub type Player {
Player(name: Option(String), level: Int, health: Int, mana: Option(Int))
}
pub fn introduce(player: Player) -> String {
case player.name {
option.None -> "Mighty Magician"
option.Some(name) -> name
}
}
pub fn revive(player: Player) -> Option(Player) {
case player.health <= 0 {
False -> None
True -> {
case player.level >= 10 {
False ->
Some(Player(
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) {
let damage = cost * 2
case player.mana {
None -> {
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) -> {
let mana_left = mana - cost
case mana_left {
ml if ml >= 0 -> #(
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)
// }
}
}
}