-
-
Notifications
You must be signed in to change notification settings - Fork 759
[hangman] Sync tests & Update solution to match canonical rules #3109
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
Open
thibault2705
wants to merge
6
commits into
exercism:main
Choose a base branch
from
thibault2705:sync_hangman_tests
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
1e6e148
Synchronize tests & Update solution to match tests.toml
thibault2705 3a0e134
Merge branch 'main' into sync_hangman_tests
thibault2705 b38f266
Remove unused methods
thibault2705 bad3db3
Remove the RxJava dependency, unused instructions file & Update tests
thibault2705 f207633
End file with new line
thibault2705 5585c9e
Merge branch 'main' into sync_hangman_tests
thibault2705 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 was deleted.
Oops, something went wrong.
This file contains hidden or 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
94 changes: 50 additions & 44 deletions
94
exercises/practice/hangman/.meta/src/reference/java/Hangman.java
This file contains hidden or 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 |
|---|---|---|
| @@ -1,76 +1,82 @@ | ||
| import io.reactivex.Observable; | ||
|
|
||
| import java.util.*; | ||
| import java.util.ArrayList; | ||
| import java.util.LinkedHashSet; | ||
| import java.util.List; | ||
| import java.util.Set; | ||
|
|
||
| class Hangman { | ||
|
|
||
| Observable<Output> play( | ||
| Observable<String> words, | ||
| Observable<String> letters) { | ||
| return Observable | ||
| .combineLatest( | ||
| words, | ||
| letters.startWith(""), | ||
| (word, letter) -> new AbstractMap.SimpleEntry<>(word, letter)) | ||
| .scan( | ||
| Output.empty(), | ||
| (state, entry) -> { | ||
| System.out.println(state + " -> " + entry); | ||
| if (state == null || state.status != Status.PLAYING) { | ||
| return createNewGame(entry.getKey()); | ||
| } else { | ||
| return processNewLetter(state, entry.getValue()); | ||
| } | ||
| }) | ||
| .skip(1); // Skip the initial state | ||
| Output guess( | ||
| String word, | ||
| List<String> letters) { | ||
|
|
||
| Output state = createNewGame(word); | ||
|
|
||
| for (String letter : letters) { | ||
| if (state.state == Status.WIN) { | ||
| throw new IllegalStateException("cannot guess after the game is won"); | ||
| } | ||
|
|
||
| if (state.state == Status.LOSE) { | ||
| throw new IllegalStateException("cannot guess after the game is lost"); | ||
| } | ||
|
|
||
| state = processNewLetter(state, letter); | ||
| } | ||
|
|
||
| return state; | ||
| } | ||
|
|
||
| private static Output createNewGame(String word) { | ||
| return Output.initialState(word); | ||
| } | ||
|
|
||
| private static Output processNewLetter( | ||
| Output state, | ||
| String letter) { | ||
| private static Output processNewLetter(Output state, String letter) { | ||
| if (state.isLetterAlreadyPlayed(letter)) { | ||
| throw new IllegalArgumentException("Letter " + letter + " was already played"); | ||
| return processIncorrectGuess(state, letter); | ||
| } | ||
|
|
||
| if (state.isLetterInSecret(letter)) { | ||
| return processCorrectGuess(state, letter); | ||
| } else { | ||
| return processIncorrectGuess(state, letter); | ||
| } | ||
|
|
||
| return processIncorrectGuess(state, letter); | ||
| } | ||
|
|
||
| private static Output processCorrectGuess(Output state, String letter) { | ||
| Set<String> newGuess = new HashSet<>(state.guess); | ||
| newGuess.add(letter); | ||
| String discovered = Output.getGuessedWord(state.secret, newGuess); | ||
| Status newStatus = Output.isWin(state.secret, newGuess) ? Status.WIN : Status.PLAYING; | ||
| Set<String> newGuesses = new LinkedHashSet<>(state.guesses); | ||
| newGuesses.add(letter); | ||
|
|
||
| String discovered = Output.getGuessedWord(state.word, newGuesses); | ||
| Status newStatus = Output.isWin(state.word, newGuesses) ? Status.WIN : Status.ON_GOING; | ||
|
|
||
| return new Output( | ||
| state.secret, | ||
| state.word, | ||
| discovered, | ||
| newGuess, | ||
| newGuesses, | ||
| state.misses, | ||
| state.parts, | ||
| newStatus); | ||
| } | ||
|
|
||
| private static Output processIncorrectGuess(Output state, String letter) { | ||
| Set<String> newMisses = new HashSet<>(state.misses); | ||
| Set<String> newMisses = new LinkedHashSet<>(state.misses); | ||
| newMisses.add(letter); | ||
|
|
||
| List<Part> newParts = new ArrayList<>(state.parts); | ||
| newParts.add(order[newParts.size()]); | ||
| Status newStatus = Output.isLoss(newMisses) ? Status.LOSS : Status.PLAYING; | ||
| if (newParts.size() < order.length) { | ||
| newParts.add(order[newParts.size()]); | ||
| } | ||
|
|
||
| Status newStatus = newParts.size() >= order.length ? Status.LOSE : Status.ON_GOING; | ||
|
|
||
| return new Output( | ||
| state.secret, | ||
| state.discovered, | ||
| state.guess, | ||
| newMisses, | ||
| newParts, | ||
| newStatus); | ||
| state.word, | ||
| state.maskedWord, | ||
| state.guesses, | ||
| newMisses, | ||
| newParts, | ||
| newStatus); | ||
| } | ||
|
|
||
| static Part[] order = Part.values(); | ||
|
|
||
| } |
This file contains hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -4,5 +4,9 @@ enum Part { | |
| LEFT_ARM, | ||
| RIGHT_ARM, | ||
| LEFT_LEG, | ||
| RIGHT_LEG | ||
| RIGHT_LEG, | ||
| LEFT_EYE, | ||
| RIGHT_EYE, | ||
| NOSE, | ||
| MOUTH | ||
| } | ||
4 changes: 2 additions & 2 deletions
4
exercises/practice/hangman/.meta/src/reference/java/Status.java
This file contains hidden or 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 |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| enum Status { | ||
| PLAYING, | ||
| ON_GOING, | ||
| WIN, | ||
| LOSS | ||
| LOSE | ||
| } |
This file contains hidden or 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,40 @@ | ||
| # 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. | ||
|
|
||
| [2419ffe6-16d8-4059-856a-9a101998a418] | ||
| description = "Initially 9 failures are allowed and no letters are guessed" | ||
|
|
||
| [17c4296d-daab-44dc-8155-37c77caa52c1] | ||
| description = "After 10 failures the game is over" | ||
|
|
||
| [77c9ae1f-bbc4-4ed4-b67e-08110cbcfc17] | ||
| description = "Losing with several correct guesses" | ||
|
|
||
| [25101d8d-9874-405b-9825-193a14b69753] | ||
| description = "Feeding a correct letter removes underscores" | ||
|
|
||
| [8e6bd521-bc9b-458f-9cce-f57f4140c173] | ||
| description = "Feeding a correct letter twice counts as a failure" | ||
|
|
||
| [5e6971b7-5e5f-49c2-b85d-1dd6aeb53bd5] | ||
| description = "Guessing a repeated letter reveals all instances" | ||
|
|
||
| [a6c69d92-01ef-4b81-b9d9-801131e79bbb] | ||
| description = "Getting all the letters right makes for a win" | ||
|
|
||
| [2dc47994-b434-4a26-b70c-1eebeff77fe4] | ||
| description = "Winning on the last guess is still a win" | ||
|
|
||
| [52801d56-6963-494b-a901-5736e46ddc12] | ||
| description = "Guessing after a lose is error" | ||
|
|
||
| [29a874f3-a413-4e1b-9a60-6be018e70b60] | ||
| description = "Guessing after a win is error" |
This file contains hidden or 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 hidden or 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 |
|---|---|---|
| @@ -1,39 +1,32 @@ | ||
| import java.util.Collections; | ||
| import java.util.List; | ||
| import java.util.Set; | ||
|
|
||
| class Output { | ||
|
|
||
| public final String secret; | ||
| public final String discovered; | ||
| public final Set<String> guess; | ||
| static final int MAX_FAILURES = Part.values().length; | ||
|
|
||
| public final String word; | ||
| public final String maskedWord; | ||
| public final Set<String> guesses; | ||
| public final Set<String> misses; | ||
| public final List<Part> parts; | ||
| public final Status status; | ||
| public final Status state; | ||
| public final int remainingFailures; | ||
|
|
||
| Output( | ||
| final String secret, | ||
| final String discovered, | ||
| final Set<String> guess, | ||
| final String word, | ||
| final String maskedWord, | ||
| final Set<String> guesses, | ||
| final Set<String> misses, | ||
| final List<Part> parts, | ||
| final Status status) { | ||
| this.secret = secret; | ||
| this.discovered = discovered; | ||
| this.guess = Set.copyOf(guess); | ||
| final Status state) { | ||
| this.word = word; | ||
| this.maskedWord = maskedWord; | ||
| this.guesses = Set.copyOf(guesses); | ||
| this.misses = Set.copyOf(misses); | ||
| this.parts = List.copyOf(parts); | ||
| this.status = status; | ||
| } | ||
|
|
||
| static Output empty() { | ||
| return new Output( | ||
| null, | ||
| null, | ||
| Collections.emptySet(), | ||
| Collections.emptySet(), | ||
| Collections.emptyList(), | ||
| null); | ||
| this.state = state; | ||
| this.remainingFailures = Math.max(0, MAX_FAILURES - 1 - parts.size()); | ||
| } | ||
|
|
||
| } | ||
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.
Looking at the tests, I notice not all of these fields are checked. Do we need all of them? If not, consider removing the ones no longer needed.