-1

I'm creating a Mastermind game and I want to compare two arrays of Colors. Both arrays contains 4 colors.

How to check if the first array contains any colors of the second array and how to check too if colors have same positions in array or not.

Any ideas?

I tried this way but it doesn't work everytime I don't know why:

Color[] colorsSelected = { Color.red, Color.red, Color.yellow, Color.green };
Color[] gameResult = { Color.red, Color.blue, Color.yellow, Color.green };

for (int i = 0; i < gameResult.length; i++) {      // go through all in second array
    if (Arrays.asList(colorsSelected).contains(gameResult[i]) && i == Arrays.asList(colorsSelected).indexOf(gameResult[i])) {
        System.out.println(colorsSelected[i]);
    }
}
Oleg Cherednik
  • 17,377
  • 4
  • 21
  • 35
Kiss
  • 1
  • 6
  • Do you know what `Stream` api is? Are you allowed to use it? – dbl Nov 17 '20 at 13:06
  • that's a kind of filter? No I never used it. I'm a beginner in java. Yes I'm allowed to used it ! How do you proceed in this case? @dbl – Kiss Nov 17 '20 at 13:13
  • http://idownvotedbecau.se/noattempt/ – Bonatti Nov 17 '20 at 13:28
  • @Bonatti yes I tried a few things that's why I m asking the question on stackoverflow .. – Kiss Nov 17 '20 at 13:31
  • Then, please [read this](https://stackoverflow.com/help/how-to-ask), and display what you attempted, what worked, what did not. It is time consuming, for people that want to help, to guess everything about your setup, your problem, and all the possible solutions, to be able to help you. RoboC's answer is the best, but is also very generic, this is a programming site, not a "how to do task x" site, and that creates a lot of redundant questions/answers, that overall wont help anyone else. To guarantee the quality of questions and answers, please, give as much information as possible – Bonatti Nov 17 '20 at 13:53
  • indexOf returns first matched index. You need to consider duplications. – hkn Nov 17 '20 at 14:05

4 Answers4

0

Write a method that returns index of searched element int indexOf(String[] colors, String search) like https://stackoverflow.com/a/4962420/51986.

Then loop prediction array. If prediction index equals search index you have an exact match. If prediction is found with a different index either it is in wrong order or a duplicate.

hkn
  • 1,453
  • 19
  • 21
0

You can use ArrayList. It has .contains() method. You can use this to confirm the existence of your specific color. .indexOf() method can be used to return index of the first occurrence of your specific color.

RoboC
  • 23
  • 7
0

Assuming the two arrays are as bellow, and considering the hkn's answer

import java.util.Arrays;

public class Test {
    public static void main(String[] args) {
        String arr1[] = {"red", "blue", "yellow", "green"};
        String arr2[] = {"red", "blue", "purple", "green"};

        for (int i = 0; i < arr2.length; i++) {      // go through all in second array
            if (Arrays.asList(arr1).contains(arr2[i]) && i == Arrays.asList(arr1).indexOf(arr2[i])) {  // if color is in first array and in the same position
                System.out.println(arr2[i]); // print that color
            }else{
                //do something for unmatched color
            };
        };
    }
}
  • I updated the post, it doesn't work everytime I don't know why. – Kiss Nov 17 '20 at 13:50
  • I checked the provided code..`colorsSelected ` needs to be ` colorSelected ` and other than that the code is working fine. Can I know when does the exceptions occur. – Chathumina Vimukthi Nov 17 '20 at 13:52
  • Provide the code that fails. Then it is easy for me to check :) – Chathumina Vimukthi Nov 17 '20 at 14:02
  • I can have two or more same colors in an array, so it only take the first one – Kiss Nov 17 '20 at 14:02
  • Check if this what you wanted. ``` Color[] colorSelected = {Color.red, Color.red, Color.yellow, Color.green}; Color[] gameResult = {Color.red, Color.red, Color.gray, Color.green}; for (int i = 0; i < gameResult.length; i++) { if(gameResult[i] == colorSelected[i]){ System.out.println(colorSelected[i]); } } ``` – Chathumina Vimukthi Nov 17 '20 at 14:11
  • yes it is, but the problem is that if I have a color not at the same order it doesn't work – Kiss Nov 17 '20 at 14:34
0
public class Foo {

    enum Color {
        red,
        blue,
        yellow,
        green,
        gray
    }

    public static void main(String... args) {
        Color[] colorSelected = { Color.red, Color.blue, Color.yellow, Color.green };
        Color[] gameResult = { Color.red, Color.blue, Color.gray, Color.green };
        boolean containsAnyColor = IS_CONTAIN_ANY_COLOR.test(colorSelected, gameResult);
        boolean equals = IS_EQUAL.test(colorSelected, gameResult);
        System.out.println("is contain any color: " + containsAnyColor);
        System.out.println("is equal: " + equals);
    }

    private static final BiPredicate<Color[], Color[]> IS_CONTAIN_ANY_COLOR = (actual, expected) -> {
        Set<Color> expectedUnique = Arrays.stream(expected).collect(Collectors.toSet());

        for (Color actualColor : actual)
            if (expectedUnique.contains(actualColor))
                return true;

        return false;
    };

    private static final BiPredicate<Color[], Color[]> IS_EQUAL = Objects::deepEquals;
}
Oleg Cherednik
  • 17,377
  • 4
  • 21
  • 35
  • It's nice but the "is_equal" shows me if all the colors matches and I need to know if colors matches separately – Kiss Nov 17 '20 at 14:46