diff --git a/config.json b/config.json index 57cdb2737..df5536b08 100644 --- a/config.json +++ b/config.json @@ -28,6 +28,20 @@ "strings" ] }, + { + "slug": "high-scores", + "uuid": "574d6323-5ff5-4019-9ebe-0067daafba13", + "core": false, + "unlocked_by": null, + "difficulty": 1, + "topics": [ + "classes", + "arrays", + "conditionals", + "loops", + "integers" + ] + }, { "slug": "reverse-string", "uuid": "2c8afeed-480e-41f3-ad58-590fa8f88029", diff --git a/exercises/high-scores/.meta/src/reference/java/HighScores.java b/exercises/high-scores/.meta/src/reference/java/HighScores.java new file mode 100644 index 000000000..2c213a5dc --- /dev/null +++ b/exercises/high-scores/.meta/src/reference/java/HighScores.java @@ -0,0 +1,32 @@ +import java.util.Arrays; +import java.util.Collections; +import java.util.OptionalInt; + +import static java.util.Arrays.copyOf; +import static java.util.Arrays.sort; + +class HighScores { + private int[] highScores; + public HighScores(int[] highScores) { + this.highScores = highScores.clone(); + } + + int[] scores() { + return highScores.clone(); + } + + int latest() { + return highScores[highScores.length - 1]; + } + + int personalBest() { + OptionalInt personalBest = Arrays.stream(highScores).max(); + return personalBest.orElse(0); + } + + Integer[] personalTopThree() { + Integer[] highestScores = Arrays.stream(highScores).boxed().toArray(Integer[]::new); + sort(highestScores, Collections.reverseOrder()); + return copyOf(highestScores, Math.min(highScores.length, 3)); + } +} diff --git a/exercises/high-scores/README.md b/exercises/high-scores/README.md new file mode 100644 index 000000000..b053fd569 --- /dev/null +++ b/exercises/high-scores/README.md @@ -0,0 +1,32 @@ +# High Scores +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 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. + +In this exercise, you're going to use and manipulate primitive and complex arrays. Java arrays are very versatile, and you'll find yourself using them again and again in problems both simple and complex. + +## Running the tests +To run the tests, run the appropriate command below in your terminal: + +```sh +gradle test +``` + +- Use gradlew.bat if you're on Windows + +In the test suites all tests but the first have been skipped. + +Once you get a test passing, you can enable the next one by removing the @Ignore("Remove to run test") annotation. + +## Submitting Exercises +Note that, when trying to submit an exercise, make sure the solution is in the $EXERCISM_WORKSPACE/java/high-scores directory. + +You can find your Exercism workspace by running exercism debug and looking for the line that starts with Workspace. + +For more detailed information about running tests, please see Running the Tests. + +## Source +Tribute to the eighties' arcade game Frogger + +## Submitting Incomplete Solutions +It's possible to submit an incomplete solution so you can see how others have completed the exercise. \ No newline at end of file diff --git a/exercises/high-scores/build.gradle b/exercises/high-scores/build.gradle new file mode 100644 index 000000000..019e5f323 --- /dev/null +++ b/exercises/high-scores/build.gradle @@ -0,0 +1,18 @@ +apply plugin: "java" +apply plugin: "eclipse" +apply plugin: "idea" + +repositories { + mavenCentral() +} + +dependencies { + testCompile "junit:junit:4.12" +} + +test { + testLogging { + exceptionFormat = 'full' + events = ["passed", "failed", "skipped"] + } +} diff --git a/exercises/high-scores/src/main/java/HighScores.java b/exercises/high-scores/src/main/java/HighScores.java new file mode 100644 index 000000000..be72588eb --- /dev/null +++ b/exercises/high-scores/src/main/java/HighScores.java @@ -0,0 +1,21 @@ +class HighScores { + public HighScores(int[] highScores) { + throw new UnsupportedOperationException("Delete this statement and write your own implementation."); + } + + int[] scores() { + throw new UnsupportedOperationException("Delete this statement and write your own implementation."); + } + + int latest() { + throw new UnsupportedOperationException("Delete this statement and write your own implementation."); + } + + int personalBest() { + throw new UnsupportedOperationException("Delete this statement and write your own implementation."); + } + + int[] personalTopThree() { + throw new UnsupportedOperationException("Delete this statement and write your own implementation."); + } +} diff --git a/exercises/high-scores/src/test/java/HighScoresTest.java b/exercises/high-scores/src/test/java/HighScoresTest.java new file mode 100644 index 000000000..be3be521f --- /dev/null +++ b/exercises/high-scores/src/test/java/HighScoresTest.java @@ -0,0 +1,63 @@ +import org.junit.Ignore; +import org.junit.Test; + +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; + +public class HighScoresTest { + + @Test + public void shouldDisplayListOfHighScores() { + HighScores highScores = new HighScores(new int[] { 30, 50, 20, 70 }); + assertArrayEquals(new int[] { 30, 50, 20, 70 }, highScores.scores()); + } + + @Test + @Ignore("Remove to run test") + public void shouldDisplayLatestAddedScore() { + HighScores highScores = new HighScores(new int[]{ 100, 0, 90, 30 }); + assertEquals(30, highScores.latest()); + } + + @Test + @Ignore("Remove to run test") + public void shouldDisplayPersonalBest() { + HighScores highScores = new HighScores(new int[] { 40, 100, 70 }); + assertEquals(100, highScores.personalBest()); + } + + @Test + @Ignore("Remove to run test") + public void shouldReturnTopThreePersonalBestFromListOfScores() { + HighScores highScores = new HighScores(new int[] { 10, 30, 90, 30, 100, 20, 10, 0, 30, 40, 40, 70, 70 }); + assertArrayEquals(new Integer[] { 100, 90, 70 }, highScores.personalTopThree()); + } + + @Test + @Ignore("Remove to run test") + public void shouldReturnTopThreeSortedHighestToLowest() { + HighScores highScores = new HighScores(new int[] { 20, 10, 30 }); + assertArrayEquals(new Integer[] { 30, 20, 10 }, highScores.personalTopThree()); + } + + @Test + @Ignore("Remove to run test") + public void shouldReturnPersonalTopWhenThereIsATie() { + HighScores highScores = new HighScores(new int[] { 40, 20, 40, 30 }); + assertArrayEquals(new Integer[] { 40, 40, 30 }, highScores.personalTopThree()); + } + + @Test + @Ignore("Remove to run test") + public void shouldReturnPersonalTopWhenThereIsLessThanThreeScores() { + HighScores highScores = new HighScores(new int[] { 30, 70 }); + assertArrayEquals(new Integer[] { 70, 30 }, highScores.personalTopThree()); + } + + @Test + @Ignore("Remove to run test") + public void shouldReturnPersonalTopWhenThereIsOnlyOneScore() { + HighScores highScores = new HighScores(new int[] {40}); + assertArrayEquals(new Integer[] {40}, highScores.personalTopThree()); + } +} diff --git a/exercises/settings.gradle b/exercises/settings.gradle index 0440c2bf4..86c6b04ba 100644 --- a/exercises/settings.gradle +++ b/exercises/settings.gradle @@ -41,6 +41,7 @@ include 'grep' include 'hamming' include 'hexadecimal' include 'hello-world' +include 'high-scores' include 'house' include 'isbn-verifier' include 'isogram'