Josh is working on a new role-playing game and needs your help implementing some of the mechanics.
Define a type union of String and Missing named StringOrMissing which can be used in later code.
Define a type union of Int64 and Nothing named IntOrNothing which can be used in later code.
The Player composite type should contain four fields, each having a type annotation and a default value:
name is a StringOrMissing, with a default of missinglevel is an Int64, with a default of 0health is an Int64, with a default of 100mana is an IntOrNothing, with a default of nothingjulia> defaultplayer = Player()
Player(missing, 0, 100, nothing)
julia> wealthyplayer = Player(mana=100)
Player(missing, 0, 100, 100)
julia> namedplayer = Player(name="Guilian", level=10)
Player("Guilian", 10, 100, nothing)Implement the introduce function.
Stealthy players may be hiding their name and will be introduced as "Mighty Magician".
Otherwise, just use the player's name to introduce them.
julia> introduce(Player(level=2, health=8))
"Mighty Magician"
julia> introduce(Player(name="Merlin", level=2, health=8))
"Merlin"The increment helper function has two methods, each with a different function signature.
The argument of each needs a type annotation to make the signature explicit.
The increment method for names should take a player's name.
missing return the title "The Great".This increment method returns the new name.
julia> player1 = Player(name="Ogre", level=5, health=49, mana=26)
Player("Ogre", 5, 49, 26)
julia> increment(player1.name)
"Ogre the Great"
julia> player2 = Player(level=32, mana=57)
Player(missing, 32, 100, 57)
julia> increment(player2.name)
"The Great"The increment method for mana should take a player's mana.
missing, return a value of 50.100.This increment method returns the updated mana.
julia> player3 = Player(level=3)
Player(missing, 3, 100, nothing)
julia> increment(player3.mana)
50
julia> player4 = Player("Goblin", level=15, health=49, mana=26)
Player("Goblin", 5, 49, 26)
julia> increment(player4.mana)
126The highest level in the game is 42. To recognize the accomplishment of players who achieve this level, a title is attached to their names. The title! function updates a player's name as follows.
increment function.In either case, the title! function returns the player's name.
julia> player1 = Player(level=42, health=12)
Player(missing, 42, 12, nothing)
julia> title!(player1)
"The Great"
julia> player1.name
"The Great"
julia> player2 = Player(name="Svengali", level=42, health=36, mana=54)
Player("Svengali", 42, 36, 54)
julia> title!(player2)
"Svengali the Great"
julia> player2.name
"Svengali the Great"
julia> player3 = Player(name="Rasputin", level=21, health=100, mana=54)
Player("Rasputin", 21, 100, 54)
julia> title!(player3)
"Rasputin"
julia> player3.name
"Rasputin"The revive! function should check that the player's character is indeed dead (their health has reached 0).
If the player is dead:
100 health.increment function.If the player is alive, nothing happens to the player
The revive! function should return the player.
julia> deadplayer1 = Player(level=2, health=0)
Player(missing, 2, 0, nothing)
julia> revive!(deadplayer1)
Player(missing, 2, 100, 50)
julia> deadplayer2 = Player(level=12, health=0, mana=5)
Player(missing, 12, 0, 5)
julia> revive!(deadplayer2)
Player(missing, 12, 100, 105)
julia> aliveplayer = Player(level=23, health=1)
Player(missing, 23, 1, nothing)
julia> revive!(aliveplayer)
Player(missing, 23, 1, nothing)Josh is working on a new role-playing game and needs your help implementing some of the mechanics.
Define a type union of String and Missing named StringOrMissing which can be used in later code.
Define a type union of Int64 and Nothing named IntOrNothing which can be used in later code.
The Player composite type should contain four fields, each having a type annotation and a default value:
name is a StringOrMissing, with a default of missinglevel is an Int64, with a default of 0health is an Int64, with a default of 100mana is an IntOrNothing, with a default of nothingjulia> defaultplayer = Player()
Player(missing, 0, 100, nothing)
julia> wealthyplayer = Player(mana=100)
Player(missing, 0, 100, 100)
julia> namedplayer = Player(name="Guilian", level=10)
Player("Guilian", 10, 100, nothing)Implement the introduce function.
Stealthy players may be hiding their name and will be introduced as "Mighty Magician".
Otherwise, just use the player's name to introduce them.
julia> introduce(Player(level=2, health=8))
"Mighty Magician"
julia> introduce(Player(name="Merlin", level=2, health=8))
"Merlin"The increment helper function has two methods, each with a different function signature.
The argument of each needs a type annotation to make the signature explicit.
The increment method for names should take a player's name.
missing return the title "The Great".This increment method returns the new name.
julia> player1 = Player(name="Ogre", level=5, health=49, mana=26)
Player("Ogre", 5, 49, 26)
julia> increment(player1.name)
"Ogre the Great"
julia> player2 = Player(level=32, mana=57)
Player(missing, 32, 100, 57)
julia> increment(player2.name)
"The Great"The increment method for mana should take a player's mana.
missing, return a value of 50.100.This increment method returns the updated mana.
julia> player3 = Player(level=3)
Player(missing, 3, 100, nothing)
julia> increment(player3.mana)
50
julia> player4 = Player("Goblin", level=15, health=49, mana=26)
Player("Goblin", 5, 49, 26)
julia> increment(player4.mana)
126The highest level in the game is 42. To recognize the accomplishment of players who achieve this level, a title is attached to their names. The title! function updates a player's name as follows.
increment function.In either case, the title! function returns the player's name.
julia> player1 = Player(level=42, health=12)
Player(missing, 42, 12, nothing)
julia> title!(player1)
"The Great"
julia> player1.name
"The Great"
julia> player2 = Player(name="Svengali", level=42, health=36, mana=54)
Player("Svengali", 42, 36, 54)
julia> title!(player2)
"Svengali the Great"
julia> player2.name
"Svengali the Great"
julia> player3 = Player(name="Rasputin", level=21, health=100, mana=54)
Player("Rasputin", 21, 100, 54)
julia> title!(player3)
"Rasputin"
julia> player3.name
"Rasputin"The revive! function should check that the player's character is indeed dead (their health has reached 0).
If the player is dead:
100 health.increment function.If the player is alive, nothing happens to the player
The revive! function should return the player.
julia> deadplayer1 = Player(level=2, health=0)
Player(missing, 2, 0, nothing)
julia> revive!(deadplayer1)
Player(missing, 2, 100, 50)
julia> deadplayer2 = Player(level=12, health=0, mana=5)
Player(missing, 12, 0, 5)
julia> revive!(deadplayer2)
Player(missing, 12, 100, 105)
julia> aliveplayer = Player(level=23, health=1)
Player(missing, 23, 1, nothing)
julia> revive!(aliveplayer)
Player(missing, 23, 1, nothing)