posted 2 weeks ago
Well, the idea is simple, but not so easy to explain. The main concern is: when a digit has already been checked, then we should not check that digit any further. How do we make this sure? By srtting a processed digit to -1, a value that is not being used, so we can easily see that it can be skipt.

In the next, I work with the arrays guess[] and solution[], and so it does not matter who is guessing and who provided the solution.

So, let guess[] = {1, 2, 3, 1} and solution[] = {1, 1, 1, 2}.

First, we do the check of exactMatches, and that is what you do as well, only I set matching elements to -1. So, after the exactCheck, my arrays will be as follows:

guess[] = {-1, 2, 3, 1}, solution[] = {-1, 1, 1, 2}, and exactMatch = 1.

The we do the check of colorMatches (albiet that we use digits). First, we look at the -1 from guess[]. Since that element is -1, we skip it. Then we look at the second element of guess[], being 2. We check that to every element of solution[], and we find one match. So, colorMatches will be 1. Then we check element 3 of guess, and we find no matches in solution[]. And finally, we look at the last element of guess[], being 1. We see a 1 in solution[] at index 1, so colorMatch can be increased to 2. Now, we must break this loop, since otherwise we would incorrectly also find a match with the 1 from solution[] at index 3.

We are now finished, but suppose that we have larger arrays. To prevent from matching a possible third 1 in guess[] from matchin the 1 in solution[] at index 1 (we already processed that element) I also set it to -1. So, I end up with these arrays:

quess[] = {-1, 2, 3, 1} and solution[] = {-1, -1, 1, -1}, with exactMatch = 1 and colorMatch = 2.

As you see, I change the arrays guess[] and solution[], and that may lead to nasty consequences, So, befor we start all of this, we make copies of both guess[] and solution[], and work with these copies.

Then, last but not least: having calculated the values for 'exactMatches' and 'colorMatches', the question is how to report these. A method in Java can only return one value!

So, let's create a

So, having all this to our disposal, I have this method: