diff --git a/.idea/codeStyles/Project.xml b/.idea/codeStyles/Project.xml new file mode 100644 index 00000000..eac15c32 --- /dev/null +++ b/.idea/codeStyles/Project.xml @@ -0,0 +1,149 @@ + + + + \ No newline at end of file diff --git a/.idea/codeStyles/codeStyleConfig.xml b/.idea/codeStyles/codeStyleConfig.xml new file mode 100644 index 00000000..79ee123c --- /dev/null +++ b/.idea/codeStyles/codeStyleConfig.xml @@ -0,0 +1,5 @@ + + + + \ No newline at end of file diff --git a/.idea/jarRepositories.xml b/.idea/jarRepositories.xml new file mode 100644 index 00000000..712ab9d9 --- /dev/null +++ b/.idea/jarRepositories.xml @@ -0,0 +1,20 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml index 254bb97b..c0e33519 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -12,7 +12,10 @@ - + + + \ No newline at end of file diff --git a/wordle-kata-solutions/src/main/java/org/eclipse/collections/wordlekata/WordleEC.java b/wordle-kata-solutions/src/main/java/org/eclipse/collections/wordlekata/WordleEC.java index c6f13aaf..f283f439 100644 --- a/wordle-kata-solutions/src/main/java/org/eclipse/collections/wordlekata/WordleEC.java +++ b/wordle-kata-solutions/src/main/java/org/eclipse/collections/wordlekata/WordleEC.java @@ -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); } } diff --git a/wordle-kata-solutions/src/test/java/org/eclipse/collections/wordlekata/WordleECTest.java b/wordle-kata-solutions/src/test/java/org/eclipse/collections/wordlekata/WordleECTest.java index 7c0081b8..af22ab46 100644 --- a/wordle-kata-solutions/src/test/java/org/eclipse/collections/wordlekata/WordleECTest.java +++ b/wordle-kata-solutions/src/test/java/org/eclipse/collections/wordlekata/WordleECTest.java @@ -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() {