vendredi 30 novembre 2018

Compare 2 arrays and distinguish same values in same position and same values in different positions in Java

I have hit a wall and need assistance. I am very new to Java so need to keep this basic to remain at my skill level. I have two Color[] arrays that contain 3 random colors. I am trying to identify colors in the same position and colors that are the same but in different positions in the array. This is the same logic for those familiar with the scoring of Mastermind. Example: answerColors[]{Color.Red, Color.Blue, Color.Blue} userColors[]{Color.Yellow,Color.Blue,Color.Red}

Results should display: same color/position = 1 (this would be the Blue at [1]) same color/ different position = 1 (this would be the Red) NOTE: since the Blue is in the same position([1]) in answerColors and userColors, the Blue[1] in userColors should not be considered when searching if the answerColor Blue at position [2] has a fellow Blue in userColor[] but just in a different position.

Example 2: answerColors[]{Color.Yellow, Color.Green, Color.Blue} userColors[]{Color.Yellow,Color.Blue,Color.Red} Results should display: same color/position = 1 (this would be the Yellow at [0]) same color/ different position = 1 (this would be the Blue)

I have seen a lot of posts that answer the same position questions but none that address the same color but different position when comparing two arrays NEEDING to take into account both (same color/ same position and same color/different position) in the same query so that a color doesn't get compared more than once (i.e. example#1 when the blue in the same position as another blue should not be considered when checking to see if the second blue in answerColor has the same color but different position in userColor)

here is my code that is not working....results should be: same color/same position = 1 same color/different position = 1

any help is appreciated!!!!!!

public void createScoreArray() {  // creates the array for     determining the values for the score panel (2 = same color/same position; 6 = not same color/same position; 1 = same color/different position ignoring those already in same color/same position
    answerColors[]{Color.Red, Color.Blue, Color.Blue};
    userColor[]{Color.Yellow, Color.Blue, Color.Red};
    int j = 0;
    scoreNumCheck = new int[3];
    for (int i = 0; i < answerColors.length; i++) {  // checking same color same position
        if (answerColors[i] == userColor[j]) {
            scoreNumCheck[i] = 2;
            System.out.println(scoreNumCheck[i]);
            j++;
         System.out.println("++++++++++++Score Pegs: " + Arrays.toString(scoreNumCheck));
        } else if (answerColors[i] != userColor[j]) {
            scoreNumCheck[i] = 6;
            j++;
        }
        System.out.println("--------- " + Arrays.toString(scoreNumCheck));


    }
    System.out.println("++++++++++++Score Pegs: " + Arrays.toString(scoreNumCheck));
    for (int s = 0; s < scoreNumCheck.length; s++) {
        if (scoreNumCheck[s] != 2) {
            for (int i = 0; i < answerColors.length; i++) {
                for (int u = 0; u < userColor.length; u++) {
                    if (userColor[u] == (answerColors[i])) {
                        scoreNumCheck[6] = 1;
                    }
                }
            }

        }
        System.out.println("2.  ++++++++++++Score Pegs: " + Arrays.toString(scoreNumCheck));
    }
}

Aucun commentaire:

Enregistrer un commentaire