Skip to content

Commit e1ed3d4

Browse files
authored
1746 implement high scores exercise (exercism#2272)
1 parent ec5df09 commit e1ed3d4

9 files changed

Lines changed: 252 additions & 0 deletions

File tree

config.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1918,6 +1918,14 @@
19181918
"practices": [],
19191919
"prerequisites": ["strings", "string-formatting"],
19201920
"difficulty": 5
1921+
},
1922+
{
1923+
"slug": "high-scores",
1924+
"name": "High Scores",
1925+
"uuid": "574d6323-5ff5-4019-9ebe-0067daafba13",
1926+
"practices": ["lists", "classes", "for-loops"],
1927+
"prerequisites" :["lists"],
1928+
"difficulty": 2
19211929
}
19221930
],
19231931
"foregone": []
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Instructions
2+
3+
Manage a game player's High Score list.
4+
5+
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.
6+
Your task is to write methods that return the highest score from the list, the last added score and the three highest scores.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"authors": [],
3+
"files": {
4+
"solution": [
5+
"src/main/java/HighScores.java"
6+
],
7+
"test": [
8+
"src/test/java/HighScoresTest.java"
9+
],
10+
"example": [
11+
".meta/src/reference/java/HighScores.java"
12+
]
13+
},
14+
"blurb": "Manage a player's High Score list.",
15+
"source": "Tribute to the eighties' arcade game Frogger"
16+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import java.util.ArrayList;
2+
import java.util.Comparator;
3+
import java.util.List;
4+
import java.util.Optional;
5+
import java.util.stream.Collectors;
6+
7+
class HighScores {
8+
9+
private List<Integer> highScores;
10+
11+
public HighScores(List<Integer> highScores) {
12+
this.highScores = new ArrayList<Integer>(highScores);
13+
}
14+
15+
List<Integer> scores() {
16+
return new ArrayList<Integer>(highScores);
17+
}
18+
19+
Integer latest() {
20+
return highScores.get(highScores.size() - 1);
21+
}
22+
23+
Integer personalBest() {
24+
Optional<Integer> personalBest = highScores.stream().max(Comparator.naturalOrder());
25+
return personalBest.orElse(0);
26+
}
27+
28+
List<Integer> personalTopThree() {
29+
return highScores.stream().sorted(Comparator.reverseOrder()).limit(3).collect(Collectors.toList());
30+
}
31+
32+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# This is an auto-generated file.
2+
#
3+
# Regenerating this file via `configlet sync` will:
4+
# - Recreate every `description` key/value pair
5+
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
6+
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
7+
# - Preserve any other key/value pair
8+
#
9+
# As user-added comments (using the # character) will be removed when this file
10+
# is regenerated, comments can be added via a `comment` key.
11+
12+
[1035eb93-2208-4c22-bab8-fef06769a73c]
13+
description = "List of scores"
14+
15+
[6aa5dbf5-78fa-4375-b22c-ffaa989732d2]
16+
description = "Latest score"
17+
18+
[b661a2e1-aebf-4f50-9139-0fb817dd12c6]
19+
description = "Personal best"
20+
21+
[3d996a97-c81c-4642-9afc-80b80dc14015]
22+
description = "Top 3 scores -> Personal top three from a list of scores"
23+
24+
[1084ecb5-3eb4-46fe-a816-e40331a4e83a]
25+
description = "Top 3 scores -> Personal top highest to lowest"
26+
27+
[e6465b6b-5a11-4936-bfe3-35241c4f4f16]
28+
description = "Top 3 scores -> Personal top when there is a tie"
29+
30+
[f73b02af-c8fd-41c9-91b9-c86eaa86bce2]
31+
description = "Top 3 scores -> Personal top when there are less than 3"
32+
33+
[16608eae-f60f-4a88-800e-aabce5df2865]
34+
description = "Top 3 scores -> Personal top when there is only one"
35+
36+
[2df075f9-fec9-4756-8f40-98c52a11504f]
37+
description = "Top 3 scores -> Latest score after personal top scores"
38+
39+
[809c4058-7eb1-4206-b01e-79238b9b71bc]
40+
description = "Top 3 scores -> Scores after personal top scores"
41+
42+
[ddb0efc0-9a86-4f82-bc30-21ae0bdc6418]
43+
description = "Top 3 scores -> Latest score after personal best"
44+
45+
[6a0fd2d1-4cc4-46b9-a5bb-2fb667ca2364]
46+
description = "Top 3 scores -> Scores after personal best"
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
apply plugin: "java"
2+
apply plugin: "eclipse"
3+
apply plugin: "idea"
4+
5+
// set default encoding to UTF-8
6+
compileJava.options.encoding = "UTF-8"
7+
compileTestJava.options.encoding = "UTF-8"
8+
9+
repositories {
10+
mavenCentral()
11+
}
12+
13+
dependencies {
14+
testImplementation "junit:junit:4.13"
15+
testImplementation "org.assertj:assertj-core:3.15.0"
16+
}
17+
18+
test {
19+
testLogging {
20+
exceptionFormat = 'full'
21+
showStandardStreams = true
22+
events = ["passed", "failed", "skipped"]
23+
}
24+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import java.util.List;
2+
3+
class HighScores {
4+
5+
public HighScores(List<Integer> highScores) {
6+
throw new UnsupportedOperationException("Delete this statement and write your own implementation.");
7+
}
8+
9+
List<Integer> scores() {
10+
throw new UnsupportedOperationException("Delete this statement and write your own implementation.");
11+
}
12+
13+
Integer latest() {
14+
throw new UnsupportedOperationException("Delete this statement and write your own implementation.");
15+
}
16+
17+
Integer personalBest() {
18+
throw new UnsupportedOperationException("Delete this statement and write your own implementation.");
19+
}
20+
21+
List<Integer> personalTopThree() {
22+
throw new UnsupportedOperationException("Delete this statement and write your own implementation.");
23+
}
24+
25+
}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
import java.util.Arrays;
2+
import org.junit.Test;
3+
4+
import static org.assertj.core.api.Assertions.assertThat;
5+
6+
public class HighScoresTest {
7+
8+
@Test
9+
public void shouldReturnListOfScores() {
10+
HighScores highScores = new HighScores(Arrays.asList(30, 50, 20, 70));
11+
assertThat(Arrays.asList(30, 50, 20, 70)).isEqualTo(highScores.scores());
12+
}
13+
14+
@Test
15+
@Ignore("Remove to run test")
16+
public void shouldReturnLatestAddedScore() {
17+
HighScores highScores = new HighScores(Arrays.asList(100, 0, 90, 30));
18+
assertThat(30).isEqualTo(highScores.latest());
19+
}
20+
21+
@Test
22+
@Ignore("Remove to run test")
23+
public void shouldReturnPersonalBest() {
24+
HighScores highScores = new HighScores(Arrays.asList(40, 100, 70));
25+
assertThat(100).isEqualTo(highScores.personalBest());
26+
}
27+
28+
@Test
29+
@Ignore("Remove to run test")
30+
public void shouldReturnPersonalTopThreeFromListOfScores() {
31+
HighScores highScores = new HighScores(Arrays.asList(10, 30, 90, 30, 100, 20, 10, 0, 30, 40, 40, 70, 70));
32+
assertThat(Arrays.asList(100, 90, 70)).isEqualTo(highScores.personalTopThree());
33+
}
34+
35+
@Test
36+
@Ignore("Remove to run test")
37+
public void shouldReturnPersonalTopThreeSortedHighestToLowest() {
38+
HighScores highScores = new HighScores(Arrays.asList(20, 10, 30));
39+
assertThat(Arrays.asList(30, 20, 10)).isEqualTo(highScores.personalTopThree());
40+
}
41+
42+
@Test
43+
@Ignore("Remove to run test")
44+
public void shouldReturnPersonalTopThreeWhenThereIsATie() {
45+
HighScores highScores = new HighScores(Arrays.asList(40, 20, 40, 30));
46+
assertThat(Arrays.asList(40, 40, 30)).isEqualTo(highScores.personalTopThree());
47+
}
48+
49+
@Test
50+
@Ignore("Remove to run test")
51+
public void shouldReturnPersonalTopWhenThereIsLessThanThreeScores() {
52+
HighScores highScores = new HighScores(Arrays.asList(30, 70));
53+
assertThat(Arrays.asList(70, 30)).isEqualTo(highScores.personalTopThree());
54+
}
55+
56+
@Test
57+
@Ignore("Remove to run test")
58+
public void shouldReturnPersonalTopWhenThereIsOnlyOneScore() {
59+
HighScores highScores = new HighScores(Arrays.asList(40));
60+
assertThat(Arrays.asList(40)).isEqualTo(highScores.personalTopThree());
61+
}
62+
63+
@Test
64+
@Ignore("Remove to run test")
65+
public void callingLatestAfterPersonalTopThree() {
66+
HighScores highScores = new HighScores(Arrays.asList(70, 50, 20, 30));
67+
highScores.personalTopThree();
68+
assertThat(30).isEqualTo(highScores.latest());
69+
}
70+
71+
@Test
72+
@Ignore("Remove to run test")
73+
public void callingScoresAfterPersonalTopThree() {
74+
HighScores highScores = new HighScores(Arrays.asList(30, 50, 20, 70));
75+
highScores.personalTopThree();
76+
assertThat(Arrays.asList(30, 50, 20, 70)).isEqualTo(highScores.scores());
77+
}
78+
79+
@Test
80+
@Ignore("Remove to run test")
81+
public void callingLatestAfterPersonalBest() {
82+
HighScores highScores = new HighScores(Arrays.asList(20, 70, 15, 25, 30));
83+
highScores.personalBest();
84+
assertThat(30).isEqualTo(highScores.latest());
85+
}
86+
87+
@Test
88+
@Ignore("Remove to run test")
89+
public void callingScoresAfterPersonalBest() {
90+
HighScores highScores = new HighScores(Arrays.asList(20, 70, 15, 25, 30));
91+
highScores.personalBest();
92+
assertThat(Arrays.asList(20, 70, 15, 25, 30)).isEqualTo(highScores.scores());
93+
}
94+
}

exercises/settings.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ include 'practice:hamming'
6262
include 'practice:hangman'
6363
include 'practice:hexadecimal'
6464
include 'practice:hello-world'
65+
include 'practice:high-scores'
6566
include 'practice:house'
6667
include 'practice:isbn-verifier'
6768
include 'practice:isogram'

0 commit comments

Comments
 (0)