-
-
Notifications
You must be signed in to change notification settings - Fork 21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add high-scores exercise #176
Merged
Merged
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{ | ||
"authors": [ | ||
"keiravillekode" | ||
], | ||
"files": { | ||
"solution": [ | ||
"high-scores.v" | ||
], | ||
"test": [ | ||
"run_test.v" | ||
], | ||
"example": [ | ||
".meta/example.v" | ||
] | ||
}, | ||
"blurb": "Manage a player's High Score list.", | ||
"source": "Tribute to the eighties' arcade game Frogger" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
module main | ||
|
||
struct HighScores { | ||
scores []int | ||
} | ||
|
||
// build a new HighScores | ||
pub fn HighScores.new(scores []int) HighScores { | ||
return HighScores{ | ||
scores: scores | ||
} | ||
} | ||
|
||
pub fn (mut high_scores HighScores) scores() []int { | ||
return high_scores.scores | ||
} | ||
|
||
pub fn (mut high_scores HighScores) latest() int { | ||
return high_scores.scores.last() | ||
} | ||
|
||
pub fn (mut high_scores HighScores) personal_best() int { | ||
mut sorted := high_scores.scores.clone() | ||
sorted.sort() | ||
return sorted.last() | ||
} | ||
|
||
pub fn (mut high_scores HighScores) personal_top_three() []int { | ||
mut sorted := high_scores.scores.clone() | ||
sorted.sort() | ||
if sorted.len > 3 { | ||
sorted.drop(sorted.len - 3) | ||
} | ||
return sorted.reverse() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# This is an auto-generated file. Regular comments will be removed when this | ||
# file is regenerated. Regenerating will not touch any manually added keys, | ||
# so comments can be added in a "comment" key. | ||
|
||
[1035eb93-2208-4c22-bab8-fef06769a73c] | ||
description = "List of scores" | ||
|
||
[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" | ||
|
||
[809c4058-7eb1-4206-b01e-79238b9b71bc] | ||
description = "Top 3 scores -> Scores after personal top scores" | ||
|
||
[ddb0efc0-9a86-4f82-bc30-21ae0bdc6418] | ||
description = "Top 3 scores -> Latest score after personal best" | ||
|
||
[6a0fd2d1-4cc4-46b9-a5bb-2fb667ca2364] | ||
description = "Top 3 scores -> Scores after personal best" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
module main | ||
|
||
struct HighScores { | ||
} | ||
|
||
// build a new HighScores | ||
pub fn HighScores.new(scores []int) HighScores { | ||
} | ||
|
||
pub fn (mut high_scores HighScores) scores() []int { | ||
} | ||
|
||
pub fn (mut high_scores HighScores) latest() int { | ||
} | ||
|
||
pub fn (mut high_scores HighScores) personal_best() int { | ||
} | ||
|
||
pub fn (mut high_scores HighScores) personal_top_three() []int { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
module main | ||
|
||
fn test_list_of_scores() { | ||
mut high_scores := HighScores.new([30, 50, 20, 70]) | ||
expect := [30, 50, 20, 70] | ||
assert high_scores.scores() == expect | ||
} | ||
|
||
fn test_latest_score() { | ||
mut high_scores := HighScores.new([100, 0, 90, 30]) | ||
assert high_scores.latest() == 30 | ||
} | ||
|
||
fn test_personal_best() { | ||
mut high_scores := HighScores.new([40, 100, 70]) | ||
assert high_scores.personal_best() == 100 | ||
} | ||
|
||
fn test_top_3_scores__personal_top_three_from_a_list_of_scores() { | ||
mut high_scores := HighScores.new([10, 30, 90, 30, 100, 20, 10, 0, 30, 40, 40, 70, 70]) | ||
expect := [100, 90, 70] | ||
assert high_scores.personal_top_three() == expect | ||
} | ||
|
||
fn test_top_3_scores__personal_top_highest_to_lowest() { | ||
mut high_scores := HighScores.new([20, 10, 30]) | ||
expect := [30, 20, 10] | ||
assert high_scores.personal_top_three() == expect | ||
} | ||
|
||
fn test_top_3_scores__personal_top_when_there_is_a_tie() { | ||
mut high_scores := HighScores.new([40, 20, 40, 30]) | ||
expect := [40, 40, 30] | ||
assert high_scores.personal_top_three() == expect | ||
} | ||
|
||
fn test_top_3_scores__personal_top_when_there_are_less_than_3() { | ||
mut high_scores := HighScores.new([30, 70]) | ||
expect := [70, 30] | ||
assert high_scores.personal_top_three() == expect | ||
} | ||
|
||
fn test_top_3_scores__personal_top_when_there_is_only_one() { | ||
mut high_scores := HighScores.new([40]) | ||
expect := [40] | ||
assert high_scores.personal_top_three() == expect | ||
} | ||
|
||
fn test_top_3_scores__latest_score_after_personal_top_scores() { | ||
mut high_scores := HighScores.new([70, 50, 20, 30]) | ||
high_scores.personal_top_three() | ||
assert high_scores.latest() == 30 | ||
} | ||
|
||
fn test_top_3_scores__scores_after_personal_top_scores() { | ||
mut high_scores := HighScores.new([30, 50, 20, 70]) | ||
high_scores.personal_top_three() | ||
expect := [30, 50, 20, 70] | ||
assert high_scores.scores() == expect | ||
} | ||
|
||
fn test_top_3_scores__latest_score_after_personal_best() { | ||
mut high_scores := HighScores.new([20, 70, 15, 25, 30]) | ||
high_scores.personal_best() | ||
assert high_scores.latest() == 30 | ||
} | ||
|
||
fn test_top_3_scores__scores_after_personal_best() { | ||
mut high_scores := HighScores.new([20, 70, 15, 25, 30]) | ||
high_scores.personal_best() | ||
expect := [20, 70, 15, 25, 30] | ||
assert high_scores.scores() == expect | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
question: is there a particular reason to do this as a struct instead of just as an array?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've just tried
and experienced compiler crashes:
https://github.com/keiravillekode/vlang/blob/b36ed1eea7ca15d748a3c3d43d6376be0b778a69/exercises/practice/high-scores/.meta/example.v
---- Testing... ------------------------------------------------------------------------------------------------- ----------------------------------------------------------------------------------------------------------------- ================== /tmp/v_1000/tsession_7ff928207740_42009345629388/run_test.6178438000952218471.tmp.c:22171: error: cannot convert 'struct array *' to 'struct array' ... ================== (Use `v -cg` to print the entire error message) builder error: ================== C error. This should never happen. This is a compiler bug, please report it using `v bug file.v`. https://github.com/vlang/v/issues/new/choose You can also use #help on Discord: https://discord.gg/vlang
I'll check if this happens with the latest compiler, and if so report a compiler bug.
Even if this approach works, I think high_scores.v should still use
struct
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When I tried the
type HighScores = []int
example solution with a recent V compiler, there were no compiler crashes.PR to update test runner: exercism/vlang-test-runner#39
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is a current compiler crash if we make a small change to the
personal_top_three()
example solution.See vlang/v#20075
The crash is avoided in the example solution uploaded for this PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The compiler bug has been fixed. :-)
I expect the fix will be in
weekly.2023.49weekly.2023.50There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great! Let's wait until the compiler update and once your exercism/vlang-test-runner#39 gets merged in, we can merge this one in too