/home/caleb/ASDV-Java/Semester 2/Assignments/MP1_CalebFontenot/src/main/java/com/calebfontenot/mp1_calebfontenot/MP1_CalebFontenot.java |
nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt
package com.calebfontenot.mp1_calebfontenot;
import java.util.Scanner;
public class MP1_CalebFontenot {
public static int[][] inputArray() {
int m, n, i, j;
Scanner input = new Scanner(System.in);
System.out.print("Enter the number of rows: ");
m = input.nextInt();
System.out.print("Enter the number of columns: ");
n = input.nextInt();
int returnArray[][] = new int[m][n];
System.out.println("Enter the elements of the array: ");
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++) {
returnArray[i][j] = input.nextInt();
}
}
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) {
int[][] horizontalTestArray = {
{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 = {
{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[][] diagonalTestArray1 = {
{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}
};
int[][] diagonalTestArray2 = {
{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}
};
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. diagonalTestArray1");
printArray(diagonalTestArray1);
System.out.println("4. diagonalTestArray2");
printArray(diagonalTestArray2);
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(diagonalTestArray1)) {
System.out.println("Testing method returns true");
} else {
System.out.println("Testing method returns false");
}
}
if (userInput == 4) {
if (isConsecutiveFour(diagonalTestArray2)) {
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);
}
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) {
System.out.println("Checking for Horizontal matches...");
boolean horizontalTest = horizontalMatch(values);
System.out.println("Checking for Vertical matches...");
boolean verticalTest = verticalMatch(values);
System.out.println("Checking for Diagonal matches...");
boolean diagonalTest = diagonalMatch(values);
if (horizontalTest || verticalTest || diagonalTest) {
System.out.println("Match found!");
return true;
}
System.out.println("No match found.");
return false;
}
public static boolean diagonalMatch(int[][] array) {
for (int row = 0; row < array.length - 3; row++) {
for (int col = 0; col < array[row].length - 3; col++) {
if (array[row][col] == array[row + 1][col + 1] && array[row + 1][col + 1] == array[row + 2][col + 2]
&& array[row + 2][col + 2] == array[row + 3][col + 3]) {
System.out.println("Found four consecutive numbers diagonally at: " + row + "," + col);
return true;
}
if (array[row][col + 3] == array[row + 1][col + 2] && array[row + 1][col + 2] == array[row + 2][col + 1]
&& array[row + 2][col + 1] == array[row + 3][col]) {
System.out.println("Found four consecutive numbers diagonally at: " + row + "," + (col + 3));
return true;
}
}
}
return false;
}
public static boolean verticalMatch(int[][] values) {
int intCounter = 0, y = 0;
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...");
}
return false;
}
public static boolean horizontalMatch(int[][] values) {
int intCounter = 0, y = 0;
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...");
}
return false;
}
}