/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 */
package com.calebfontenot.mp1_calebfontenot;

import java.util.Scanner;

/**
 *
 * @author caleb
 */
public class MP1_CalebFontenot {

    public static int[][] inputArray()
    {
        int m, n, i, j;
        // Create scanner
        Scanner input = new Scanner(System.in);
        System.out.print("Enter the number of rows: ");
        //taking row as input  
        m = input.nextInt();
        System.out.print("Enter the number of columns: ");
        //taking column as input  
        n = input.nextInt();
        // Declaring the two-dimensional matrix   
        int returnArray[][] = new int[m][n];
        // Read the matrix values   
        System.out.println("Enter the elements of the array: ");
        //loop for row  
        for (i = 0; i < m; i++) //inner for loop for column  
        {
            for (j = 0; j < n; j++) {
                returnArray[i][j] = input.nextInt();
            }
        }
        //accessing array elements   
        System.out.println("Elements of the array are: ");
        for (i = 0; i < m; i++) {
            for (j = 0; j < n; j++) {
                return returnArray;
            }
        }
        return null;
    }

    public static boolean isConsecutiveFour(int[] values)
    {
        for (int i = 0; i < values.length - 4; ++i) {
            if (values[i] == values[i + 1]
                    && values[i + 1] == values[i + 2]
                    && values[i + 2] == values[i + 3]) {
                return true;
            } else {
                return false;
            }
        }
        return false;
    }

    public static void main(String[] args)
    { //2965
        int[][] horizontalTestArray = { // Horizontal
            {0, 1, 0, 3, 1, 6, 1},
            {0, 1, 6, 8, 6, 0, 1},
            {5, 6, 2, 1, 8, 2, 9},
            {6, 5, 6, 1, 1, 9, 1},
            {1, 3, 6, 1, 4, 0, 7},
            {3, 3, 3, 3, 4, 0, 7}
        };

        int[][] verticalTestArray = { // Vertical
            {0, 1, 0, 3, 1, 6, 1},
            {0, 1, 6, 8, 6, 0, 1},
            {5, 5, 2, 1, 8, 2, 9},
            {6, 5, 6, 1, 1, 9, 1},
            {1, 5, 6, 1, 4, 0, 7},
            {3, 5, 3, 3, 4, 0, 7}
        };
        int[][] diagonalTestArray = { // Diagonal 1
            {0, 1, 0, 3, 1, 6, 1},
            {0, 1, 6, 8, 6, 0, 1},
            {9, 6, 2, 1, 8, 2, 9},
            {6, 9, 6, 1, 1, 9, 1},
            {1, 3, 9, 1, 4, 0, 7},
            {3, 3, 3, 9, 4, 0, 7}
        };
        // Create scanner
        Scanner input = new Scanner(System.in);
        int userInput;
        do {
            System.out.println("Do you want to enter an array, or use a predefined one?");
            System.out.println("1. Use predefined array.");
            System.out.println("2. Enter a new array.");
            System.out.println("-1. quit and exit program");
            System.out.print("Input: ");
            userInput = input.nextInt();
            if (userInput == 1) {
                System.out.println("Selected \"Use predefined array\"");
                System.out.println("Printing arrays...");
                System.out.println("1. horizontalTestArray");
                printArray(horizontalTestArray);
                System.out.println("2. verticalTestArray");
                printArray(verticalTestArray);
                System.out.println("3. diagonalTestArray");
                printArray(diagonalTestArray);
                System.out.print("Input: ");
                userInput = input.nextInt();
                if (userInput == 1) {
                    if (isConsecutiveFour(horizontalTestArray)) {
                        System.out.println("Testing method returns true");
                    } else {
                        System.out.println("Testing method returns false");
                    }
                }
                if (userInput == 2) {
                    if (isConsecutiveFour(verticalTestArray)) {
                        System.out.println("Testing method returns true");
                    } else {
                        System.out.println("Testing method returns false");
                    }
                }
                if (userInput == 3) {
                    if (isConsecutiveFour(diagonalTestArray)) {
                        System.out.println("Testing method returns true");
                    } else {
                        System.out.println("Testing method returns false");
                    }
                }
                userInput = 0;
            }
            if (userInput == 2) {
                System.out.println("Selected \"Enter new array\"");
                int[][] newArray = inputArray();
                if (isConsecutiveFour(newArray)) {
                    System.out.println("Testing method returns true");
                } else {
                    System.out.println("Testing method returns false");
                }
            }
        } while (userInput != -1);
        System.exit(0);
        //printArray(intArray1);
        //System.out.println(isConsecutiveFour(intArray1));
    }

    public static void printArray(int[][] array)
    {
        int rowCounter = 0;
        for (int x = 0; x < array.length; x++) {
            for (int y = 0; y < array[x].length; y++) {
                System.out.print(array[x][y] + " ");
                rowCounter++;
                if (rowCounter % (array.length + 1) == 0) {
                    System.out.println();
                }
            }
        }
    }

    public static void printArray(int[] array)
    {
        for (int i : array) {
            System.out.print(i + " ");
        }
    }

    public static boolean isConsecutiveFour(int[][] values)
    {
        int intCounter = 0, y = 0;
        // Horizontal checking
        // If the same value has been observed 4 times, return true
        //System.out.println("values[0].length: " + values[0].length);
        //System.out.println("values.length: " + values.length);
        System.out.println("Checking for Horizontal matches...");
        for (int rowIterate = 0; rowIterate < values.length; ++rowIterate) {
            y = 0;
            for (int x = 0; x < values[0].length - 1; ++x) {
                ++y;
                if (values[rowIterate][x] == values[rowIterate][y]) {
                    intCounter++;
                    System.out.println(values[rowIterate][x] + " " + values[rowIterate][y] + ",  intCounter: " + intCounter);
                } else {
                    intCounter = 0;
                    System.out.println(values[rowIterate][x] + " " + values[rowIterate][y] + ",  intCounter: " + intCounter);
                }
                if (intCounter == 3) {
                    System.out.println("Horizontal match!");
                    return true;
                }
            }
            System.out.println("Checking next line...");
        }
        // Vertical Checking
        System.out.println("Checking for Vertical matches...");
        for (int rowIterate = 0; rowIterate < values.length; ++rowIterate) {
            y = 0;
            for (int x = 0; x < values.length - 1; ++x) {
                ++y;
                if (values[x][rowIterate] == values[y][rowIterate]) {
                    intCounter++;
                    System.out.println(values[x][rowIterate] + " " + values[y][rowIterate] + ",  intCounter: " + intCounter);
                } else {
                    intCounter = 0;
                    System.out.println(values[x][rowIterate] + " " + values[y][rowIterate] + ",  intCounter: " + intCounter);
                }
                if (intCounter == 3) {
                    System.out.println("Vertical match!");
                    return true;
                }
            }
            System.out.println("Checking next line...");
        }
        System.out.println("Checking for Diagonal matches...");
        int rows = 0;
        for (int numColumns = 0; numColumns < values[0].length; numColumns++) {
            int i = 0;
            int j = numColumns;
            int[] _1DArray = new int[numColumns + 1];
            for (int numRows = 0; numRows <= rows; ++numRows) {

                System.out.println("i: " + i + " j: " + j);
                _1DArray[numRows] = values[i][j];
                //printArray(_1DArray);
                //System.out.println(values[rows][i] + " " + values[rows][j]);
                ++i;
                --j;
            }
            System.out.println("Checking next line...");
            rows++;
        }

        System.out.println("No match found.");
        return false;
    }
}
