import java.util.Arrays;

public class Main {

public static void main(String[] args) {
        /*
        System.out.println("Hello World!");
        int[] a = {5, 4, 3, 2, 1};
        mergeSort(a);
        */

        int[][] stuff = {
                {1, 2, 3},
                {4, 5, 6},
                {7, 8, 9}
        };

        System.out.println("Initial state of array:");
        for (int row = 0; row < stuff.length; row++) {
            System.out.println(Arrays.toString(stuff[row]));
        }
        System.out.println(' ');


        flipHorizontal(stuff);
        System.out.println("Horizontally flipped:");
        for (int row = 0; row < stuff.length; row++) {
            System.out.println(Arrays.toString(stuff[row]));
        }
        System.out.println(' ');

        flipVertical(stuff);
        System.out.println("Vertically flipped:");
        for (int row = 0; row < stuff.length; row++) {
            System.out.println(Arrays.toString(stuff[row]));
        }

    }

    // Swap left/right
    public static void flipHorizontal(int[][] arrayToFlip) {
        int columnIndexToStopAt = arrayToFlip[0].length / 2;

        for (int currentRowIndex = 0; currentRowIndex < arrayToFlip.length; currentRowIndex++) {
            for (int currentColumnIndex = 0; currentColumnIndex < columnIndexToStopAt; currentColumnIndex++) {

                int lastColumnIndex = arrayToFlip[0].length - 1;
                int oppositeColumnIndex = lastColumnIndex - currentColumnIndex;

                int temp = arrayToFlip[currentRowIndex][currentColumnIndex];

                arrayToFlip[currentRowIndex][currentColumnIndex] = arrayToFlip[currentRowIndex][oppositeColumnIndex];
                arrayToFlip[currentRowIndex][oppositeColumnIndex] = temp;

            }
        }
    }

    // Swap top/bottom
    public static void flipVertical(int[][] arrayToFlip) {
        int rowIndexToStopAt = arrayToFlip.length / 2;

        for (int currentRowIndex = 0; currentRowIndex < rowIndexToStopAt; currentRowIndex++) {
            for (int currentColumnIndex = 0; currentColumnIndex < arrayToFlip[0].length; currentColumnIndex++) {

                int lastRowIndex = arrayToFlip.length - 1;
                int oppositeRowIndex = lastRowIndex - currentRowIndex;

                int temp = arrayToFlip[currentRowIndex][currentColumnIndex];

                arrayToFlip[currentRowIndex][currentColumnIndex] = arrayToFlip[oppositeRowIndex][currentColumnIndex];
                arrayToFlip[oppositeRowIndex][currentColumnIndex] = temp;

            }
        }
    }

}
