diff --git a/role-playing-game/src/role_playing_game.gleam b/role-playing-game/src/role_playing_game.gleam index f9df7a1..7e3757a 100644 --- a/role-playing-game/src/role_playing_game.gleam +++ b/role-playing-game/src/role_playing_game.gleam @@ -1,3 +1,4 @@ +import gleam/int import gleam/option.{type Option, None, Some} pub type Player { @@ -16,81 +17,21 @@ pub fn revive(player: Player) -> Option(Player) { 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), - )) + False -> Some(Player(..player, health: 100)) + True -> Some(Player(..player, 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, - ) - } - } + None -> #(Player(..player, health: player.health - cost |> int.max(0)), 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) + Some(mana) -> + case mana { + m if m < cost -> #(player, 0) + m -> #(Player(..player, mana: Some(m - cost)), cost * 2) } - // 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) - // } - } } }