Skip to content

Commit

Permalink
Add high-scores exercise (#131)
Browse files Browse the repository at this point in the history
  • Loading branch information
ageron authored Oct 8, 2024
1 parent 112108b commit 969f58f
Show file tree
Hide file tree
Showing 8 changed files with 187 additions and 0 deletions.
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@
"prerequisites": [],
"difficulty": 1
},
{
"slug": "high-scores",
"name": "High Scores",
"uuid": "80043e33-2549-435d-8739-14cbfa337b17",
"practices": [],
"prerequisites": [],
"difficulty": 1
},
{
"slug": "leap",
"name": "Leap",
Expand Down
6 changes: 6 additions & 0 deletions exercises/practice/high-scores/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Instructions

Manage a game player's High Score list.

Your task is to build a high-score component of the classic Frogger game, one of the highest selling and most addictive games of all time, and a classic of the arcade era.
Your task is to write methods that return the highest score from the list, the last added score and the three highest scores.
13 changes: 13 additions & 0 deletions exercises/practice/high-scores/.meta/Example.roc
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module [latest, personalBest, personalTopThree]

Score : U64

latest : List Score -> Result Score [ListWasEmpty]
latest = List.last

personalBest : List Score -> Result Score [ListWasEmpty]
personalBest = List.max

personalTopThree : List Score -> List Score
personalTopThree = \scores ->
scores |> List.sortDesc |> List.takeFirst 3
18 changes: 18 additions & 0 deletions exercises/practice/high-scores/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"authors": [
"ageron"
],
"files": {
"solution": [
"HighScores.roc"
],
"test": [
"high-scores-test.roc"
],
"example": [
".meta/Example.roc"
]
},
"blurb": "Manage a player's High Score list.",
"source": "Tribute to the eighties' arcade game Frogger"
}
23 changes: 23 additions & 0 deletions exercises/practice/high-scores/.meta/template.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{%- import "generator_macros.j2" as macros with context -%}
{{ macros.canonical_ref() }}
{{ macros.header() }}

import {{ exercise | to_pascal }} exposing [latest, personalBest, personalTopThree]

{% for supercase in cases %}
## {{ supercase["description"] }}
{%- if supercase["cases"] -%}
{%- set cases = supercase["cases"] -%}
{%- else -%}
{%- set cases = [supercase] -%}
{%- endif -%}
{%- for case in cases %}
{%- if case["description"] != supercase["description"] %}
# {{ case["description"] }}
{%- endif %}
expect
result = {{ case["property"] }} {{ case["input"]["scores"] }}
result == {% if case["property"] != "personalTopThree" %}Ok {% endif %}{{ case["expected"] | to_roc }}

{% endfor -%}
{%- endfor %}
56 changes: 56 additions & 0 deletions exercises/practice/high-scores/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# This is an auto-generated file.
#
# Regenerating this file via `configlet sync` will:
# - Recreate every `description` key/value pair
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
# - Preserve any other key/value pair
#
# As user-added comments (using the # character) will be removed when this file
# is regenerated, comments can be added via a `comment` key.

[1035eb93-2208-4c22-bab8-fef06769a73c]
description = "List of scores"
comment = "Exercise is too simple for an opaque Scores type, and an identity function would be uninteresting"
include = false

[6aa5dbf5-78fa-4375-b22c-ffaa989732d2]
description = "Latest score"

[b661a2e1-aebf-4f50-9139-0fb817dd12c6]
description = "Personal best"

[3d996a97-c81c-4642-9afc-80b80dc14015]
description = "Top 3 scores -> Personal top three from a list of scores"

[1084ecb5-3eb4-46fe-a816-e40331a4e83a]
description = "Top 3 scores -> Personal top highest to lowest"

[e6465b6b-5a11-4936-bfe3-35241c4f4f16]
description = "Top 3 scores -> Personal top when there is a tie"

[f73b02af-c8fd-41c9-91b9-c86eaa86bce2]
description = "Top 3 scores -> Personal top when there are less than 3"

[16608eae-f60f-4a88-800e-aabce5df2865]
description = "Top 3 scores -> Personal top when there is only one"

[2df075f9-fec9-4756-8f40-98c52a11504f]
description = "Top 3 scores -> Latest score after personal top scores"
comment = "No need to check for immutability in Roc"
include = false

[809c4058-7eb1-4206-b01e-79238b9b71bc]
description = "Top 3 scores -> Scores after personal top scores"
comment = "No need to check for immutability in Roc"
include = false

[ddb0efc0-9a86-4f82-bc30-21ae0bdc6418]
description = "Top 3 scores -> Latest score after personal best"
comment = "No need to check for immutability in Roc"
include = false

[6a0fd2d1-4cc4-46b9-a5bb-2fb667ca2364]
description = "Top 3 scores -> Scores after personal best"
comment = "No need to check for immutability in Roc"
include = false
15 changes: 15 additions & 0 deletions exercises/practice/high-scores/HighScores.roc
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module [latest, personalBest, personalTopThree]

Score : U64

latest : List Score -> Result Score _
latest = \scores ->
crash "Please implement the 'latest' function"

personalBest : List Score -> Result Score _
personalBest = \scores ->
crash "Please implement the 'personalBest' function"

personalTopThree : List Score -> List Score
personalTopThree = \scores ->
crash "Please implement the 'personalTopThree' function"
48 changes: 48 additions & 0 deletions exercises/practice/high-scores/high-scores-test.roc
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# These tests are auto-generated with test data from:
# https://github.com/exercism/problem-specifications/tree/main/exercises/high-scores/canonical-data.json
# File last updated on 2024-10-06
app [main] {
pf: platform "https://github.com/roc-lang/basic-cli/releases/download/0.15.0/SlwdbJ-3GR7uBWQo6zlmYWNYOxnvo8r6YABXD-45UOw.tar.br",
}

main =
Task.ok {}

import HighScores exposing [latest, personalBest, personalTopThree]

## Latest score
expect
result = latest [100, 0, 90, 30]
result == Ok 30

## Personal best
expect
result = personalBest [40, 100, 70]
result == Ok 100

## Top 3 scores
# Personal top three from a list of scores
expect
result = personalTopThree [10, 30, 90, 30, 100, 20, 10, 0, 30, 40, 40, 70, 70]
result == [100, 90, 70]

# Personal top highest to lowest
expect
result = personalTopThree [20, 10, 30]
result == [30, 20, 10]

# Personal top when there is a tie
expect
result = personalTopThree [40, 20, 40, 30]
result == [40, 40, 30]

# Personal top when there are less than 3
expect
result = personalTopThree [30, 70]
result == [70, 30]

# Personal top when there is only one
expect
result = personalTopThree [40]
result == [40]

0 comments on commit 969f58f

Please sign in to comment.