Java newbie and I'm stuck on something that seems really simple. I wrote an algorithm for a game I'm building and, using TDD, all of my tests pass, but the actual game is not returning the right feedback. Instead, whenever I guess a correct letter, it returns a "w" and should return a "b".
Background The game starts by generating a 4 character random code. The objective of the game is to enter a guess and try to match the secret code. The feedback class is supposed to tell me if my guess is hot or cold compared to the code.
I pass the guess and secret code to the class and calculate the feedback. The guess is a string that I use "split" to turn into a String array. The secret code is also a string Array that I randomly generate 4 different string values for. I tried checking to see if one element from both the guess and secret code are strings and got "true" on both.
Problem Problem is that my exact matches if statement never gets executed. I read that you should use equals() instead and also tried that but get an error.
Can anyone help?
Here is my Feedback class: public class Feedback {
public String[] guess;
public String[] secretCode;
public String[] feedback;
public String[] get(String[] guess, String[] secretCode) {
this.guess = Arrays.copyOf(guess, guess.length);
this.secretCode = Arrays.copyOf(secretCode, secretCode.length);
feedback = new String[this.secretCode.length];
findExactMatches();
findNearMatches();
findNoMatches();
sortFeedback();
return feedback;
}
public void findExactMatches() {
for (int i = 0; i < guess.length; i++) {
if (guess[i] == secretCode[i]) {
feedback[i] = "b";
guess[i] = "x";
secretCode[i] = "x";
}
}
}
public void findNearMatches() {
for (int i = 0; i < guess.length; i++) {
if ( Arrays.asList(secretCode).contains(guess[i]) && guess[i] != "x" ) {
feedback[i] = "w";
int matched_symbol_index = Arrays.asList(secretCode).indexOf(guess[i]);
secretCode[matched_symbol_index] = "x";
}
}
}
public void findNoMatches() {
for (int i = 0; i < guess.length; i++) {
if ( feedback[i] == null ) {
feedback[i] = " ";
}
}
}
public void sortFeedback() {
Arrays.sort(feedback);
}
}