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

/**
 *
 * @author caleb
 */
public class MarkouCode {
    
    /**
     * Traverses the parm array diagonally from top left towards the top and
     * prints the (i,j) indexes of the traversal.
     *
     * @param ar 2D array
     */
    public static boolean topLeftTriangleDiagonalNothwest(int[][] ar)
    {
        System.out.println("diagonal indexes top-triangle of array ");
        int rowCount = 0;
        for (int columnCounter = 0; columnCounter < ar[0].length; ++columnCounter)
        {
            int i = 0;
            int j = columnCounter;
            int[] diagonalArray = new int[rowCount + 1];
            for (int diagonalCounter = 0; diagonalCounter <= rowCount; ++diagonalCounter)
            {
                diagonalArray[diagonalCounter] = ar[i][j];
                if (isConsecutiveFour(diagonalArray)) {
                   return true;
                }
                System.out.print(i + "," + j + "   ");
                ++i;
                j--;
                if (i == ar.length | j == ar[0].length)
                {
                    break;
                }
            }
            rowCount++;

            System.out.println("");

        }
        return false;
    }

    /*Traverses the parm array diagonally from bottom right towrads the to
    //and prints the (i,j) indexes of the  traversal.

     * 
     * @param ar 
     */
    public static void bottomRightTriangleDiagonalNothwest(int[][] ar)
    {
        System.out.println("diagonal indexes bottom-triangle of array ");

        int rowCount = 0;
        int numColumns = 0;
        if (ar[0].length == ar.length)//square table
        {
            numColumns = ar[0].length - 1;
        }
        else if (ar[0].length > ar.length)//wide table
        {
            numColumns = ar.length - 1;
        }

        else //narrow-width rectangle array
        {
            numColumns = ar[0].length;
        }

        for (int columnCounter = 0; columnCounter < numColumns; ++columnCounter)
        {
            int i = ar.length - 1;
            int j = ar[0].length - 1 - columnCounter;
            for (int diagonalCounter = 0; diagonalCounter <= rowCount; ++diagonalCounter)
            {

                System.out.print(i + "," + j + "   ");
                --i;
                j++;
            }
            rowCount++;
            System.out.println("");

        }
        //middle chunk of narrow array

        System.out.println("-----------------------");
        if (ar.length > ar[0].length)
        {
            System.out.println("diagonal indexes middle part of array when the array "
                    + "is narrow ");

            for (int i = 1; i < ar.length - ar[0].length; ++i)
            {
                int rowIndex = i;
                int columnIndex = ar[0].length - 1;
                for (int j = 0; j < ar[0].length; ++j)
                {

                    System.out.print(rowIndex + "," + columnIndex + "      ");
                    rowIndex++;
                    columnIndex--;

                }

                System.out.println("");
            }
        }

    }

    public static boolean isConsecutiveFour(int[][] values)
    {
        return true;
    }
     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;
    }

    private static void printIndexesDiagonally(int ar[][])
    {
        topLeftTriangleDiagonalNothwest(ar);
        System.out.println("------------------------");
        bottomRightTriangleDiagonalNothwest(ar);
    }

    public static void main(String[] args)
    {
        int[][] ar1 =
        {
            {
                1, 1, 1, 1, 8
            },
            {
                1, 1, 1, 8, 9
            },
            {
                1, 1, 8, 1, 9
            },
            {
                1, 8, 1, 3, 2
            },
        };

        int[][] ar2 =
        {
            {
                1, 1, 1, 1, 1, 1, 1, 1
            },
            {
                1, 1, 1, 1, 1, 1, 1, 1
            },
            {
                1, 1, 1, 1, 1, 1, 1, 1
            },
            {
                1, 1, 1, 1, 1, 1, 1, 1
            },
            {
                1, 1, 1, 1, 1, 1, 1, 1
            },

        };

        int[][] ar3 =
        {
            {
                1, 1, 1
            },
            {
                1, 1, 1
            },
            {
                1, 1, 1
            },
            {
                1, 1, 1
            },
            {
                1, 1, 1
            },
            {
                1, 1, 1
            },
            {
                1, 1, 1
            },
            {
                1, 1, 1
            },
            {
                1, 1, 1
            },
            {
                1, 1, 1
            },

        };
        System.out.println(topLeftTriangleDiagonalNothwest(ar1));
        /*
        System.out.println("SQUARE array of size 4x4");
        printIndexesDiagonally(ar1);

        System.out.println("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
        System.out.println("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
        System.out.println("\nWIDE array of size 8x5");
        printIndexesDiagonally(ar2);

        System.out.println("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
        System.out.println("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
        System.out.println("\nNARROW array of size 10x3");
        printIndexesDiagonally(ar3);
*/
        
    }
}
