Skip to content

Commit

Permalink
Fix issue eclipse-collections#291 - add implementation to WordleEC.gu…
Browse files Browse the repository at this point in the history
…ess(String guess)
  • Loading branch information
RongjingH committed Sep 23, 2023
1 parent 0152f62 commit 817d8cc
Show file tree
Hide file tree
Showing 6 changed files with 224 additions and 3 deletions.
149 changes: 149 additions & 0 deletions .idea/codeStyles/Project.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions .idea/codeStyles/codeStyleConfig.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions .idea/jarRepositories.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,50 @@ public WordleEC(String string)
public String guess(String guess)
{
// TODO - Replace null with the code needed to satisfy the rules above.
return null;
CharAdapter hiddenChars = Strings.asChars(this.string);
CharAdapter guessChars = Strings.asChars(guess.toLowerCase());
MutableCharBag hiddenBag = hiddenChars.toBag();
MutableCharBag guessBag = guessChars.toBag();

char[] resChars = new char[guess.length()];

//If all letters and their positions match ,return guess with uppercase letters
if (guessChars.equals(hiddenChars)) {
return guess.toUpperCase();
}

for (int i = 0; i < guess.length(); i++) {
//Delete all char which is not contained by guessBag in hiddenBag
if (!guessBag.contains(hiddenChars.charAt(i))) {
hiddenBag.removeOccurrences(hiddenChars.charAt(i),hiddenBag.occurrencesOf(hiddenChars.charAt(i)));
}
//Delete all char which is not contained by hiddenBag in guessBag
if (!hiddenBag.contains(guessChars.charAt(i))) {
guessBag.removeOccurrences(guessChars.charAt(i),guessBag.occurrencesOf(guessChars.charAt(i)));
}
//Check the letter in guess
if (guessChars.charAt(i) == hiddenChars.charAt(i)) {
//Meet : " If a letter in the guess String matches a letter in the hidden word and the letter is in the
// same position, then replace the character with an uppercase letter."
resChars[i] = (char)(guessChars.charAt(i) - 'a' + 'A');
guessBag.remove(guessChars.charAt(i));
hiddenBag.remove(hiddenChars.charAt(i));
} else {
resChars[i] = '.';
}
}

int i = 0;
while(!hiddenBag.isEmpty() && !guessBag.isEmpty()) {
char curChar = guessChars.charAt(i);
if (resChars[i] == '.' && guessBag.contains(curChar)) {
resChars[i] = curChar;
guessBag.remove(curChar);
hiddenBag.remove(curChar);
}
i++;
}

return String.valueOf(resChars);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

public class WordleECTest
{
// @Test // Uncomment once guess is implemented for WordleEC
@Test // Uncomment once guess is implemented for WordleEC
// @Tag("SOLUTION")
public void matchWordWithGuess()
{
Expand Down

0 comments on commit 817d8cc

Please sign in to comment.