I wrote a java program that does basically what wordle does. Given 2 3×3 rows, for example
// Answer
AAA
BBB
CCC
// Guess
AYY
AAA
ZZZ
If the guess matches the answer (in this case index [0][0]), mark it as “green”
If the guess does NOT match the answer at the exact position, but it IS valid (for example A at answer[1][0] does not match guess[1][0] but is an answer at guess[0][1]) would be counted as “yellow”
Here is the code I currently have. It works in this test case and all other test cases except for one. I can’t seem to catch my error.
import java.util.Scanner;
// Currently test case 6 not working
public class test {
public static void main(String[] args) throws FileNotFoundException {
Scanner io = new Scanner(System.in);
int green = 0, yellow = 0;
char[][] answer = new char[3][3];
char[][] guess = new char[3][3];
{
String tempString = io.next();
for (int i = 0; i < 3; i++) {
answer[0][i] = tempString.charAt(i);
}
tempString = io.next();
for (int i = 0; i < 3; i++) {
answer[1][i] = tempString.charAt(i);
}
tempString = io.next();
for (int i = 0; i < 3; i++) {
answer[2][i] = tempString.charAt(i);
}
tempString = io.next();
for (int i = 0; i < 3; i++) {
guess[0][i] = tempString.charAt(i);
}
tempString = io.next();
for (int i = 0; i < 3; i++) {
guess[1][i] = tempString.charAt(i);
}
tempString = io.next();
for (int i = 0; i < 3; i++) {
guess[2][i] = tempString.charAt(i);
}
//System.out.println(Arrays.deepToString(answer));
//System.out.println(Arrays.deepToString(guess));
}
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (answer[i][j] == guess[i][j]) {
answer[i][j] = (char) -1;
guess[i][j] = (char) -1;
green++;
}
}
}
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (answer[i][j] != guess[i][j]) {
char needToFind = guess[i][j];
for (int k = 0; k < 3; k++) {
for (int l = 0; l < 3; l++) {
if (answer[k][l] == needToFind) {
yellow++;
guess[i][j] = (char) -1;
answer[k][l] = (char) -1;
break;
}
}
}
//System.out.println("-----2"); debug here don't mind me
//System.out.println(Arrays.deepToString(answer));
//System.out.println(Arrays.deepToString(guess));
//System.out.println(yellow);
}
}
}
System.out.println(green);
System.out.println(yellow);
}
}
/*
plan
Get input using 2d arrays
Create 2 matrix arrays: answer and guess
Get the values into the array
Use for loop that loops 9 times
If the value at answer is the same at same index in guess, green++
Use another for loop 9 times, if the indexes are NOT the same (and is not -1), then find if another of the same value exists in another index, if so, change the answer index into -1 and yellow++;
*/
I’m not currently worried that much about the speed, it performs about as I expect for the simple implementation I’ve made.
Advertisement
Answer
This is what I came up with:
Pseudocode:
IF letter at CURRENT_INDEX -> "green"
ELSEIF letter anywhere in string array -> "yellow"
ELSE (not found) -> "red"
Code:
public static void main(String[] args) {
String[] guess = {"AYY","AAA","ZZZ"};
String[] answer = {"AAA","BBB","CCC"};
Stream<String> aWords = Arrays.asList(answer).stream(); // "AAABBBCCC"
String aFlat = aWords.collect(Collectors.joining());
String [][] color = new String[3][3];
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
String guessLetter = Character.toString((guess[i]).charAt(j));
if (Character.toString(answer[i].charAt(j)).equals(guessLetter)) {
color[i][j] = "green";
} else if (aFlat.contains(guessLetter)) {
color[i][j] = "yellow";
} else {
color[i][j] = "red";
}
}
}
System.out.println();
}
The output of the program is this:
[
[green, red, red], // for "AYY" vs "AAA"
[yellow, yellow, yellow], // for "AAA" vs "BBB"
[red, red, red] // for "ZZZ" vs "CCC"
]
The only letter that matches the letter at the current index is guess[0][0]
. guess[0][1]
and guess[0][2]
are red because the letter Y
cannot be found in the flattened aFlat
string.
guess [1][x]
is all yellows because the letter A
is found on the flattened aFlat
string, but not in the correct index location in the answer array.
Lastly, guess[2][x]
is red because the letter Z
is not found anywhere on the flattened aFlat
string.