From 0495faaa19daa2840c92105195b7ce0a8683d760 Mon Sep 17 00:00:00 2001 From: Caleb Fontenot Date: Fri, 20 Jan 2023 00:35:34 -0600 Subject: [PATCH] MP1 --- .../mp1_calebfontenot/MP1_CalebFontenot.java | 97 ++++---- .../mp1_calebfontenot/MarkouCode.java | 213 ++++++++---------- 2 files changed, 145 insertions(+), 165 deletions(-) diff --git a/Semester 2/Assignments/MP1_CalebFontenot/src/main/java/com/calebfontenot/mp1_calebfontenot/MP1_CalebFontenot.java b/Semester 2/Assignments/MP1_CalebFontenot/src/main/java/com/calebfontenot/mp1_calebfontenot/MP1_CalebFontenot.java index a888a67..485de87 100644 --- a/Semester 2/Assignments/MP1_CalebFontenot/src/main/java/com/calebfontenot/mp1_calebfontenot/MP1_CalebFontenot.java +++ b/Semester 2/Assignments/MP1_CalebFontenot/src/main/java/com/calebfontenot/mp1_calebfontenot/MP1_CalebFontenot.java @@ -11,8 +11,7 @@ import java.util.Scanner; */ public class MP1_CalebFontenot { - public static int[][] inputArray() - { + public static int[][] inputArray() { int m, n, i, j; // Create scanner Scanner input = new Scanner(System.in); @@ -43,8 +42,7 @@ public class MP1_CalebFontenot { return null; } - public static boolean isConsecutiveFour(int[] values) - { + 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] @@ -57,8 +55,7 @@ public class MP1_CalebFontenot { return false; } - public static void main(String[] args) - { //2965 + public static void main(String[] args) { //2965 int[][] horizontalTestArray = { // Horizontal {0, 1, 0, 3, 1, 6, 1}, {0, 1, 6, 8, 6, 0, 1}, @@ -143,8 +140,7 @@ public class MP1_CalebFontenot { //System.out.println(isConsecutiveFour(intArray1)); } - public static void printArray(int[][] array) - { + 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++) { @@ -157,41 +153,33 @@ public class MP1_CalebFontenot { } } - public static void printArray(int[] array) - { + 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); + public static boolean isConsecutiveFour(int[][] values) { + // Horizontal Checking 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..."); - } + boolean horizontalTest = horizontalMatch(values); // Vertical Checking System.out.println("Checking for Vertical matches..."); + boolean verticalTest = verticalMatch(values); + System.out.println("Checking for Diagonal matches..."); + boolean diagonalNorthwest = MarkouCode.topLeftTriangleDiagonalNothwest(values); + boolean diagonalMiddle = MarkouCode.bottomRightTriangleDiagonalNothwest(values); + System.out.println(diagonalNorthwest); + if (horizontalTest || verticalTest || diagonalNorthwest) { + System.out.println("Match found!"); + return true; + } + System.out.println("No match found."); + 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) { @@ -210,26 +198,33 @@ public class MP1_CalebFontenot { } 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) { + return false; + } + public static boolean horizontalMatch(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("i: " + i + " j: " + j); - _1DArray[numRows] = values[i][j]; - //printArray(_1DArray); - //System.out.println(values[rows][i] + " " + values[rows][j]); - ++i; - --j; + 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..."); - rows++; } - - System.out.println("No match found."); return false; } } diff --git a/Semester 2/Assignments/MP1_CalebFontenot/src/main/java/com/calebfontenot/mp1_calebfontenot/MarkouCode.java b/Semester 2/Assignments/MP1_CalebFontenot/src/main/java/com/calebfontenot/mp1_calebfontenot/MarkouCode.java index 5b0ae62..42a8a87 100644 --- a/Semester 2/Assignments/MP1_CalebFontenot/src/main/java/com/calebfontenot/mp1_calebfontenot/MarkouCode.java +++ b/Semester 2/Assignments/MP1_CalebFontenot/src/main/java/com/calebfontenot/mp1_calebfontenot/MarkouCode.java @@ -9,33 +9,28 @@ 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. + * 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) - { + 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) - { + 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) - { + for (int diagonalCounter = 0; diagonalCounter <= rowCount; ++diagonalCounter) { diagonalArray[diagonalCounter] = ar[i][j]; if (isConsecutiveFour(diagonalArray)) { - return true; + return true; } System.out.print(i + "," + j + " "); ++i; j--; - if (i == ar.length | j == ar[0].length) - { + if (i == ar.length | j == ar[0].length) { break; } } @@ -53,57 +48,57 @@ public class MarkouCode { * * @param ar */ - public static void bottomRightTriangleDiagonalNothwest(int[][] ar) - { + public static boolean bottomRightTriangleDiagonalNothwest(int[][] ar) { System.out.println("diagonal indexes bottom-triangle of array "); int rowCount = 0; int numColumns = 0; + int[] diagonalArray = new int[rowCount + 1]; if (ar[0].length == ar.length)//square table { numColumns = ar[0].length - 1; - } - else if (ar[0].length > ar.length)//wide table + } else if (ar[0].length > ar.length)//wide table { numColumns = ar.length - 1; - } - - else //narrow-width rectangle array + } else //narrow-width rectangle array { numColumns = ar[0].length; } - for (int columnCounter = 0; columnCounter < numColumns; ++columnCounter) - { + 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) - { + for (int diagonalCounter = 0; diagonalCounter <= rowCount; ++diagonalCounter) { System.out.print(i + "," + j + " "); + diagonalArray[diagonalCounter] = ar[i][j]; + if (isConsecutiveFour(diagonalArray)) { + return true; + } --i; j++; } rowCount++; System.out.println(""); - + return false; } //middle chunk of narrow array System.out.println("-----------------------"); - if (ar.length > ar[0].length) - { + 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) - { + 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) - { + for (int j = 0; j < ar[0].length; ++j) { System.out.print(rowIndex + "," + columnIndex + " "); + diagonalArray[diagonalCounter] = ar[i][j]; + if (isConsecutiveFour(diagonalArray)) { + return true; + } rowIndex++; columnIndex--; @@ -115,105 +110,95 @@ public class MarkouCode { } - public static boolean isConsecutiveFour(int[][] values) - { + public static boolean isConsecutiveFour(int[][] values) { return true; } - public static boolean isConsecutiveFour(int[] values) - { + + 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] - ) { + if (values[i] == values[i + 1] + && values[i + 1] == values[i + 2] + && values[i + 2] == values[i + 3]) { return true; - } - else { + } else { return false; } } return false; } - private static void printIndexesDiagonally(int ar[][]) - { + 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 - }, - }; + 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[][] 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 - }, - - }; + 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"); @@ -228,7 +213,7 @@ public class MarkouCode { System.out.println("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++"); System.out.println("\nNARROW array of size 10x3"); printIndexesDiagonally(ar3); -*/ - + */ + } }