In this exercise, you're implementing a way to keep track of the high scores for the most popular game in your local arcade hall.
You have 7 functions to implement, all related to returning and manipulating a map of high score data.
To make a new high score map, define the HighScore.new/0
function which doesn't take any parameters and returns a new, empty map of high scores.
HighScore.new()
# => %{}
To add a player to the high score map, define HighScore.add_player/3
, which is a function which takes 3 parameters:
- The first parameter is the map of scores.
- The second parameter is the name of a player as a string.
- The third parameter is the score as an integer. The parameter is optional, implement the third parameter with a default value of 0.
score_map = HighScore.new()
# => %{}
score_map = HighScore.add_player(score_map, "Dave Thomas")
# => %{"Dave Thomas" => 0}
score_map = HighScore.add_player(score_map, "José Valim", 486_373)
# => %{"Dave Thomas" => 0, "José Valim"=> 486_373}
To remove a player from the high score map, define HighScore.remove_player/2
, which takes 2 parameters:
- The first parameter is the map of scores.
- The second parameter is the name of the player as a string.
score_map = HighScore.new()
# => %{}
score_map = HighScore.add_player(score_map, "Dave Thomas")
# => %{"Dave Thomas" => 0}
score_map = HighScore.remove_player(score_map, "Dave Thomas")
# => %{}
To reset a player's score, define HighScore.remove_player/2
, which takes 2 parameters:
- The first parameter is the map of scores.
- The second parameter is the name of the player as a string, whose score you wish to reset.
score_map = HighScore.new()
# => %{}
score_map = HighScore.add_player(score_map, "José Valim", 486_373)
# => %{"José Valim"=> 486_373}
score_map = HighScore.reset_score(score_map, "José Valim")
# => %{"José Valim"=> 0}
To update a players score by adding to the previous score, define HighScore.update_player/2
, which takes 3 parameters:
- The first parameter is the map of scores.
- The second parameter is the name of the player as a string, whose score you wish to update.
- The third parameter is the score that you wish to add to the stored high score.
score_map = HighScore.new()
# => %{}
score_map = HighScore.add_player(score_map, "José Valim", 486_373)
# => %{"José Valim"=> 486_373}
score_map = HighScore.update_score(score_map, "José Valim", 5)
# => %{"José Valim"=> 486_378}
To get a list of players ordered by name, define HighScore.order_by_players/1
, which takes 1 parameter:
- The first parameter is the map of scores.
score_map = HighScore.new()
# => %{}
score_map = HighScore.add_player(score_map, "Dave Thomas", 2_374)
# => %{"Dave Thomas" => 2_374}
score_map = HighScore.add_player(score_map, "José Valim", 486_373)
# => %{"Dave Thomas" => 2_374, "José Valim"=> 486_373}
HighScore.order_by_players(score_map)
# => [{"Dave Thomas", 2_374}, {"José Valim", 486_373}]
To get a list of players ordered by scores in decreasing order, define HighScore.order_by_scores/1
, which takes 1 parameter:
- The first parameter is the map of scores.
score_map = HighScore.new()
# => %{}
score_map = HighScore.add_player(score_map, "Dave Thomas", 2_374)
# => %{"Dave Thomas" => 2_374}
score_map = HighScore.add_player(score_map, "José Valim", 486_373)
# => %{"Dave Thomas" => 2_374, "José Valim"=> 486_373}
HighScore.order_by_scores(score_map)
# => [{"José Valim", 486_373}, {"Dave Thomas", 2_374}]