Skip to content

[hangman] Sync tests & Update solution to match tests.toml#3109

Open
thibault2705 wants to merge 2 commits intoexercism:mainfrom
thibault2705:sync_hangman_tests
Open

[hangman] Sync tests & Update solution to match tests.toml#3109
thibault2705 wants to merge 2 commits intoexercism:mainfrom
thibault2705:sync_hangman_tests

Conversation

@thibault2705
Copy link
Copy Markdown
Contributor

pull request

What has been done:

  • Rename and simplify method play(Observable<String> words, Observable<String> letters) to guess(String word, Observable<String> letters)
  • Add, rename output params maskedWord, state, and remainingFailures to match canonical expectations
  • Introduced MAX_FAILURES = 10 based on canonical rules
  • Added error handling for guesses after win and lose states
  • Aligned test cases with canonical scenarios

This PR partially solves issues #2959.


Reviewer Resources:

Track Policies

@github-actions
Copy link
Copy Markdown
Contributor

This PR touches files which potentially affect the outcome of the tests of an exercise. This will cause all students' solutions to affected exercises to be re-tested.

If this PR does not affect the result of the test (or, for example, adds an edge case that is not worth rerunning all tests for), please add the following to the merge-commit message which will stops student's tests from re-running. Please copy-paste to avoid typos.

[no important files changed]

For more information, refer to the documentation. If you are unsure whether to add the message or not, please ping @exercism/maintainers-admin in a comment. Thank you!

class Hangman {

Observable<Output> play(Observable<String> words, Observable<String> letters) {
Observable<Output> guess(String word, Observable<String> letters) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this change is because we have removed the "consecutive games" test, which isn't in the problem specifications. To me, if we are going to change the first argument from Observable to a String, then I think we should also remove the other Observable usages. letters can become a list and the return could be Output directly. This would simplify the exercise and remove the RxJava dependency. The exercise's .docs/instructions.append.md should also be deleted if we do this.

The other alternative is to keep the "consecutive games" test case and keep using RxJava. We can have Java track specific tests.

Comment on lines +43 to +89
static Output initialState(final String secret) {
return new Output(
secret,
getGuessedWord(secret, Collections.emptySet()),
new LinkedHashSet<>(),
new LinkedHashSet<>(),
new ArrayList<>(),
Status.ON_GOING);
}

boolean isLetterAlreadyPlayed(final String letter) {
return guesses.contains(letter) || misses.contains(letter);
}

boolean isLetterInSecret(final String letter) {
return word.contains(letter);
}

static String getGuessedWord(String secret, Set<String> letters) {
return secret.chars()
.mapToObj(i -> String.valueOf((char) i))
.map(c -> letters.contains(c) ? c : "_")
.collect(joining());
}

static boolean isWin(String secret, Set<String> guessedLetters) {
return secret.chars()
.mapToObj(i -> String.valueOf((char) i))
.allMatch(guessedLetters::contains);
}

static boolean isLoss(List<Part> parts) {
return parts.size() >= MAX_FAILURES;
}

@Override
public String toString() {
return "Output{" +
"secret='" + word + '\'' +
", discovered='" + maskedWord + '\'' +
", guess=" + guesses +
", misses=" + misses +
", parts=" + parts +
", status=" + state +
", remainingFailures=" + remainingFailures +
'}';
}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do all these methods really need to be added? I don't see them used by the tests. If they needed by the student solution, students could write them.

import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.*;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please don't use the * import.

Suggested change
import java.util.*;
import java.util.Collections;
import java.util.List;
import java.util.Set;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants