public class PracticeQuiz2DArray { public static void main(String[] args) { //Create a 2D array that will analyze all elements in a 2D array of characters. //Write a method that will analyze this 2D array and return True if no element in the array //is adjacent to an element containing the same character. Your array must carry over from //the top to the bottom and left to right sides. You need only check up, down, left and right. //No diagonals. //ROY //GBR //OYG //RETURNS char[][] colors = { {'R', 'O', 'Y', 'G'}, {'B', 'I', 'V', 'R'}, {'O', 'Y', 'G', 'B'}, {'I', 'V', 'R', 'O'}, {'Y', 'G', 'B', 'I'}, {'V', 'R', 'O', 'Y'}, {'G', 'B', 'I', 'V'} }; // char[][] colors = { // {'R', 'P', 'G', 'Y'}, // {'G', 'B', 'R', 'G'}, // {'O', 'Y', 'G', 'Y'} // }; // char[][] colors = { // {'R', 'R', 'Y'}, // {'G', 'B', 'R'}, // {'O', 'Y', 'G'} // }; System.out.println(hasSameAdjacent(colors)); } public static boolean hasSameAdjacent(char[][] arr) { for(int r = 0; r < arr.length; r++) for(int c = 0; c < arr[0].length; c++) { //check to right if(c!=arr[0].length-1) { if(arr[r][c]==arr[r][c+1]) return true; } else if (arr[r][c]==arr[r][0]) return true; //check to left if(c!=0) { if(arr[r][c]==arr[r][c-1]) return true; } else if (arr[r][c]==arr[r][arr[0].length-1]) return true; //check above if(r!=0) { if(arr[r][c]==arr[r-1][c]) return true; } else if (arr[r][c]==arr[arr.length-1][c]) return true; //check below if(r!=arr.length-1) { if(arr[r][c]==arr[r+1][c]) return true; } else if(arr[r][c]==arr[0][c])return true; } return false; } }