Skip to content

Commit

Permalink
Write the code and the tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mpizenberg committed May 27, 2021
1 parent 59f60a2 commit 4671dc8
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 0 deletions.
28 changes: 28 additions & 0 deletions exercises/concept/role-playing-game/elm.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"type": "application",
"source-directories": [
"src"
],
"elm-version": "0.19.1",
"dependencies": {
"direct": {
"elm/core": "1.0.5",
"elm/json": "1.1.3",
"elm/parser": "1.1.0",
"elm/random": "1.0.0",
"elm/regex": "1.0.0",
"elm/time": "1.0.0"
},
"indirect": {}
},
"test-dependencies": {
"direct": {
"elm-explorations/test": "1.2.2",
"rtfeldman/elm-iso8601-date-strings": "1.1.3"
},
"indirect": {
"elm/html": "1.0.0",
"elm/virtual-dom": "1.0.2"
}
}
}
34 changes: 34 additions & 0 deletions exercises/concept/role-playing-game/src/RolePlayingGame.elm
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
module RolePlayingGame exposing (Player, castSpell, revive)


type alias Player =
{ level : Int
, health : Int
, mana : Maybe Int
}


revive : Player -> Maybe Player
revive player =
if player.health > 0 then
Nothing

else if player.level >= 10 then
Just { player | health = 100, mana = Just 100 }

else
Just { player | health = 100, mana = Nothing }


castSpell : Int -> Player -> ( Player, Int )
castSpell manaCost player =
case player.mana of
Nothing ->
( { player | health = max 0 (player.health - manaCost) }, 0 )

Just mana ->
if mana >= manaCost then
( { player | mana = Just (mana - manaCost) }, 2 * manaCost )

else
( player, 0 )
37 changes: 37 additions & 0 deletions exercises/concept/role-playing-game/tests/Tests.elm
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
module Tests exposing (tests)

import Expect
import RolePlayingGame exposing (Player, castSpell, revive)
import Test exposing (..)


tests : Test
tests =
describe "RolePlayingGame"
[ test "Attempting to revive a player that is alive should return Nothing" <|
\() -> Expect.equal (revive { level = 12, health = 42, mana = Just 7 }) Nothing
, test "Reviving a low level player resets its health to 100" <|
\() ->
Expect.equal (revive { level = 3, health = 0, mana = Nothing })
(Just { level = 3, health = 100, mana = Nothing })
, test "Reviving a high level player resets both its health and mana" <|
\() ->
Expect.equal (revive { level = 10, health = 0, mana = Just 14 })
(Just { level = 10, health = 100, mana = Just 100 })
, test "Casting a spell spends double the mana" <|
\() ->
Expect.equal (castSpell 9 { level = 10, health = 69, mana = Just 20 })
( { level = 10, health = 69, mana = Just 11 }, 18 )
, test "Attempting to cast a spell with insufficient mana does nothing" <|
\() ->
Expect.equal (castSpell 39 { level = 10, health = 69, mana = Just 20 })
( { level = 10, health = 69, mana = Just 20 }, 0 )
, test "Attempting to cast a spell without a mana pool decreases the player's health" <|
\() ->
Expect.equal (castSpell 7 { level = 5, health = 58, mana = Nothing })
( { level = 5, health = 51, mana = Nothing }, 0 )
, test "A player's health cannot go below 0" <|
\() ->
Expect.equal (castSpell 12 { level = 5, health = 6, mana = Nothing })
( { level = 5, health = 0, mana = Nothing }, 0 )
]

0 comments on commit 4671dc8

Please sign in to comment.