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.
To run the tests, run the command dotnet test
from within the exercise directory.
Initially, only the first test will be enabled. This is to encourage you to solve the exercise one step at a time.
Once you get the first test passing, remove the Skip
property from the next test and work on getting that test passing.
Once none of the tests are skipped and they are all passing, you can submit your solution
using exercism submit HighScores.cs
For more detailed information about the C# track, including how to get help if you're having trouble, please visit the exercism.io C# language page.
Tribute to the eighties' arcade game Frogger
// This file was auto-generated based on version 4.0.0 of the canonical data.
using System.Collections.Generic;
using Xunit;
public class HighScoresTest
{
[Fact]
public void List_of_scores()
{
var sut = new HighScores(new List<int> { 30, 50, 20, 70 });
Assert.Equal(new List<int> { 30, 50, 20, 70 }, sut.Scores());
}
[Fact(Skip = "Remove to run test")]
public void Latest_score()
{
var sut = new HighScores(new List<int> { 100, 0, 90, 30 });
Assert.Equal(30, sut.Latest());
}
[Fact(Skip = "Remove to run test")]
public void Personal_best()
{
var sut = new HighScores(new List<int> { 40, 100, 70 });
Assert.Equal(100, sut.PersonalBest());
}
[Fact(Skip = "Remove to run test")]
public void Personal_top_three_from_a_list_of_scores()
{
var sut = new HighScores(new List<int> { 10, 30, 90, 30, 100, 20, 10, 0, 30, 40, 40, 70, 70 });
Assert.Equal(new List<int> { 100, 90, 70 }, sut.PersonalTopThree());
}
[Fact(Skip = "Remove to run test")]
public void Personal_top_highest_to_lowest()
{
var sut = new HighScores(new List<int> { 20, 10, 30 });
Assert.Equal(new List<int> { 30, 20, 10 }, sut.PersonalTopThree());
}
[Fact(Skip = "Remove to run test")]
public void Personal_top_when_there_is_a_tie()
{
var sut = new HighScores(new List<int> { 40, 20, 40, 30 });
Assert.Equal(new List<int> { 40, 40, 30 }, sut.PersonalTopThree());
}
[Fact(Skip = "Remove to run test")]
public void Personal_top_when_there_are_less_than_3()
{
var sut = new HighScores(new List<int> { 30, 70 });
Assert.Equal(new List<int> { 70, 30 }, sut.PersonalTopThree());
}
[Fact(Skip = "Remove to run test")]
public void Personal_top_when_there_is_only_one()
{
var sut = new HighScores(new List<int> { 40 });
Assert.Equal(new List<int> { 40 }, sut.PersonalTopThree());
}
}
using System;
using System.Collections.Generic;
using System.Linq;
public class HighScores
{
private List<int> list;
public HighScores(List<int> list)
{
this.list = list;
}
public List<int> Scores()
{
return this.list;
}
public int Latest()
{
return this.list[this.list.Count - 1];
}
public int PersonalBest()
{
int max = 0;
foreach(int elem in this.list) {
if (elem > max) {
max = elem;
}
}
return max;
}
public List<int> PersonalTopThree()
{
List<int> listCopy = new List<int>(this.list);
listCopy.Sort();
List<int> threeBest;
if (listCopy.Count <= 3) {
threeBest = listCopy;
}
else {
threeBest = listCopy.GetRange(listCopy.Count - 3,3);
}
threeBest.Reverse();
return threeBest;
}
}
A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.
Here are some questions to help you reflect on this solution and learn the most from it.
Level up your programming skills with 3,122 exercises across 52 languages, and insightful discussion with our volunteer team of welcoming mentors. Exercism is 100% free forever.
Sign up Learn More
Community comments